WCF如何通过契约加编码方式调用
WCF采用基于契约的服务调用方法,通过System.ServiceModel.ChannelFactory<TChannel>直接创建服务代理对象。
创建服务代理
public static T Create<T>(string endpointName)
{
if (string.IsNullOrEmpty(endpointName))
{
#region 默认配置,根据类名,赋值endpointName
#endregion
throw new ArgumentNullException("endpointName");
}
return (T)(new ServiceRealProxy<T>(endpointName).GetTransparentProxy());
}
public class ServiceRealProxy<T> : RealProxy
{
private readonly string _endpointName; public ServiceRealProxy(string endpointName)
: base(typeof(T))
{
if (string.IsNullOrEmpty(endpointName))
{
throw new ArgumentNullException("endpointName");
}
_endpointName = endpointName;
} public override IMessage Invoke(IMessage msg)
{
T channel = ChannelFactoryCreator.Create<T>(this._endpointName).CreateChannel();
var methodCall = (IMethodCallMessage)msg;
IMethodReturnMessage methodReturn = null;
object[] copiedArgs = Array.CreateInstance(typeof(object), methodCall.Args.Length) as object[];
methodCall.Args.CopyTo(copiedArgs, );
try
{
object returnValue = methodCall.MethodBase.Invoke(channel, copiedArgs);
methodReturn = new ReturnMessage(returnValue, copiedArgs, copiedArgs.Length, methodCall.LogicalCallContext, methodCall);
var communicationObject = channel as ICommunicationObject;
if (communicationObject != null) communicationObject.Close();
}
catch (Exception ex)
{
if (ex.InnerException is CommunicationException || ex.InnerException is TimeoutException)
{
var communicationObject = channel as ICommunicationObject;
if (communicationObject != null) communicationObject.Abort();
} methodReturn = ex.InnerException != null ? new ReturnMessage(ex.InnerException, methodCall) : new ReturnMessage(ex, methodCall);
} return methodReturn;
}
}
WCF如何通过契约加编码方式调用的更多相关文章
- WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载]
原文:WCF技术剖析之二十九:换种不同的方式调用WCF服务[提供源代码下载] 我们有两种典型的WCF调用方式:通过SvcUtil.exe(或者添加Web引用)导入发布的服务元数据生成服务代理相关的代码 ...
- Post方式调用wcf服务
我们平常在PC端调用WCF服务,只要知道WCF服务的地址,客户端直接添加引用服务就可以使用了,殊不知还有其他方式,其实,我们也可以 通过HTTP POST的方式调用WCF服务,这样就不用添加引用了,在 ...
- 以WCF安全认证方式调用通用权限管理系统获取基础信息资料
在B/S开发中,涉及到获取系统基础资料的问题,因为是在不同平台下的开发,采用了WCF方式获取. 下面是一个调用通用权限管理系统(吉日嘎拉)基础信息资料的一个demo供参考 调用原理图: web.con ...
- (转)将wcf 以webservice的方式调用
将wcf 以webservice的方式调用 问题:a公司使用wcf 发布服务(.net Framework 3.0 or 3.5),b公司需要使用a公司发布的服务 ,但b公司目前阶段只使用.net F ...
- WCF配置后支持通过URL进行http方式调用
最近遇到一个小型项目,主要就是通过手机写入NFC信息,思考许久后决定就写一个简单的CS程序来搞定这个问题,可是当涉及到手机和PC通信的时候首先考虑到的就是IIS,同时因为数据库是SQLite,思前想后 ...
- 知道WCF的地址用工厂通道方式快速调用WCF
知道WCF的地址用工厂通道方式快速调用WCF 1 using System; 2 using System.ServiceModel; 3 using System.ServiceModel.D ...
- 为什么一定要调用 setlocale 呢? 因为在 C/C++ 语言标准中定义了其运行时的字符集环境为 "C" ,也就是 ASCII 字符集的一个子集。使用setlocal改变整个应用程序的字符集编码方式(wcstombs使用前要设置 setlocale (LC_ALL, "chs"); )
setlocale 配置地域化信息. 语法: string setlocale(string category, string locale); 返回值: 字符串 函数种类: 操作系统与环境 内容 ...
- Matlab与C/C++联合编程之Matlab以MEX方式调用C代码(五)完整过程加示
如下为本人亲证代码: 一: 编译器的安装与配置(环境不同,显示结果不同) 要使用MATLAB编译器,用户计算机上应用事先安装与MATLAB适配的以下任何一种ANSI C/C++编译器: 5.0.6.0 ...
- AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)
使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...
随机推荐
- MonkeyRunner学习(1)测试连接
前期的环境搭建详见:http://www.cnblogs.com/peng-lan/p/5388488.html 1.打开模拟器 打开模拟器有两种方法,正常的在eclipse 中启动就不介绍了 另一种 ...
- (十)Linux内核中的常用宏container_of
Container_of在Linux内核中是一个常用的宏,用于从包含在某个结构中的指针获得结构本身的指针,通俗地讲就是通过结构体变量中某个成员的首地址进而获得整个结构体变量的首地址. Containe ...
- Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Android 项目中常用到的第三方组件
项目中常用到的第三方组件 1 社会化分享ShareSDK-Core-2.5.9.jarShareSDK-QQ-2.5.9.jarShareSDK-QZone-2.5.9.jarShareSDK-Sin ...
- 转:如何学习SQL(第二部分:从关系角度理解SQL)
转自:http://blog.163.com/mig3719@126/blog/static/285720652010950825538/ 6. 从关系角度理解SQL 6.1. 关系和表 众所周知,我 ...
- spring相关资源
1. 文档中英文 Spring Framework Reference Documentation http://docs.spring.io/spring/docs/4.1.7.RELEASE/sp ...
- 转!!常用的4种动态网页技术—CGI、ASP、JSP、PHP
1.CGI CGI(Common Gateway Interface,公用网关接口)是较早用来建立动态网页的技术.当客户端向Web服务器上指定的CGI程序发出请求时,Web服务器会启动一个新的进程 ...
- Dorado7检验器失效原因分析
应用场景: AutoForm1使用包含A.B两个字段的DataType1. A字段不允许为空,并且B字段取值true时A字段需要重新输入,B取其它值时A值不需重新录入. 实现方法:A字段添加了非空检验 ...
- 基于TBDS的flume异常问题排查过程
版权声明:本文由王亮原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/214 来源:腾云阁 https://www.qclou ...
- js上下滚屏效果,代码通过测试
这是html代码 <div class="box"> <div class="bcon"> <h1><b>领号实 ...