博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#动态调用WCF接口(3)
阅读量:6071 次
发布时间:2019-06-20

本文共 3415 字,大约阅读时间需要 11 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.ServiceModel.Channels;using System.Reflection;namespace wcf.wcfbase {    ///    /// 使用ChannelFactory为wcf客户端创建独立通道    ///    public class WcfChannelFactory    {        public WcfChannelFactory()        {        }        ///        /// 执行方法 WSHttpBinding        ///        ///
服务接口
///wcf地址 ///方法名 ///参数列表 public static object ExecuteMetod
(string uri, string methodName, paramsobject[] args) { //BasicHttpBinding binding = new BasicHttpBinding(); //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。 WSHttpBinding binding = new WSHttpBinding(); EndpointAddress endpoint = new EndpointAddress(uri); using (ChannelFactory
channelFactory = new ChannelFactory
(binding, endpoint)) { T instance = channelFactory.CreateChannel(); using (instance as IDisposable) { try { Type type = typeof(T); MethodInfo mi = type.GetMethod(methodName); return mi.Invoke(instance, args); } catch (TimeoutException) { (instance as ICommunicationObject).Abort(); throw; } catch (CommunicationException) { (instance as ICommunicationObject).Abort(); throw; } catch (Exception vErr) { (instance as ICommunicationObject).Abort(); throw; } } } } //nettcpbinding 绑定方式 public static object ExecuteMethod
(string pUrl, string pMethodName,paramsobject[] pParams) { EndpointAddress address = new EndpointAddress(pUrl); Binding bindinginstance = null; NetTcpBinding ws = new NetTcpBinding(); ws.MaxReceivedMessageSize = 20971520; ws.Security.Mode = SecurityMode.None; bindinginstance = ws; using (ChannelFactory
channel = new ChannelFactory
(bindinginstance, address)) { T instance = channel.CreateChannel(); using (instance as IDisposable) { try { Type type = typeof(T); MethodInfo mi = type.GetMethod(pMethodName); return mi.Invoke(instance, pParams); } catch (TimeoutException) { (instance as ICommunicationObject).Abort(); throw; } catch (CommunicationException) { (instance as ICommunicationObject).Abort(); throw; } catch (Exception vErr) { (instance as ICommunicationObject).Abort(); throw; } } } } //调用示例: string uri = "http://localhost:9998/mywcf/Service"; object o = ExecuteMetod
(uri, "Add",12.0,13.0); Console.WriteLine(o.ToString()); Console.ReadKey(); //简单方法,不考虑太多,直接调用: EndpointAddress address1 = new EndpointAddress("http://localhost:9998/mywcf/Service"); ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1); Console.WriteLine(service1.Add(12.0, 13.0).ToString()); } }

 

转载地址:http://ubfgx.baihongyu.com/

你可能感兴趣的文章
边缘控制平面Ambassador全解读
查看>>
Windows Phone 7 利用计时器DispatcherTimer创建时钟
查看>>
程序员最喜爱的12个Android应用开发框架二(转)
查看>>
vim学习与理解
查看>>
DIRECTSHOW在VS2005中PVOID64问题和配置问题
查看>>
MapReduce的模式,算法以及用例
查看>>
《Advanced Linux Programming》读书笔记(1)
查看>>
zabbix agent item
查看>>
一步一步学习SignalR进行实时通信_7_非代理
查看>>
AOL重组为两大业务部门 全球裁员500人
查看>>
字符设备与块设备的区别
查看>>
为什么我弃用GNOME转向KDE(2)
查看>>
Redis学习记录初篇
查看>>
爬虫案例若干-爬取CSDN博文,糗事百科段子以及淘宝的图片
查看>>
Web实时通信技术
查看>>
第三章 计算机及服务器硬件组成结合企业运维场景 总结
查看>>
IntelliJ IDEA解决Tomcal启动报错
查看>>
默认虚拟主机设置
查看>>
七周五次课(1月26日)
查看>>
Linux系统一些系统查看指令
查看>>