C#动态调用WCF接口

栏目: ASP.NET · 发布时间: 7年前

内容简介:C#动态调用WCF接口

这种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用。

string url = "http://localhost:3000/DoubleService.svc";
IDoubleService proxy = WcfInvokeFactory.CreateServiceByUrl<IDoubleService>(url);
int result = proxy.Add(1, 3);

需要用到的类

public class WcfInvokeFactory
   {
       #region WCF服务工厂
       public static T CreateServiceByUrl<T>(string url)
       {
           return CreateServiceByUrl<T>(url, "basicHttpBinding");
       }
 
       public static T CreateServiceByUrl<T>(string url, string bing)
       {
           try
           {
               if (string.IsNullOrEmpty(url)) throw new NotSupportedException("This url is not Null or Empty!");
               EndpointAddress address = new EndpointAddress(url);
               Binding binding = CreateBinding(bing);
               ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
               return factory.CreateChannel();
           }
           catch (Exception ex)
           {
               throw new Exception("创建服务工厂出现异常.");
           }
       }
       #endregion
 
       #region 创建传输协议
       /// <summary>
       /// 创建传输协议
       /// </summary>
       /// <param name="binding">传输协议名称</param>
       /// <returns></returns>
       private static Binding CreateBinding(string binding)
       {
           Binding bindinginstance = null;
           if (binding.ToLower() == "basichttpbinding")
           {
               BasicHttpBinding ws = new BasicHttpBinding();
               ws.MaxBufferSize = 2147483647;
               ws.MaxBufferPoolSize = 2147483647;
               ws.MaxReceivedMessageSize = 2147483647;
               ws.ReaderQuotas.MaxStringContentLength = 2147483647;
               ws.CloseTimeout = new TimeSpan(0, 30, 0);
               ws.OpenTimeout = new TimeSpan(0, 30, 0);
               ws.ReceiveTimeout = new TimeSpan(0, 30, 0);
               ws.SendTimeout = new TimeSpan(0, 30, 0);
 
               bindinginstance = ws;
           }
           else if (binding.ToLower() == "nettcpbinding")
           {
               NetTcpBinding ws = new NetTcpBinding();
               ws.MaxReceivedMessageSize = 65535000;
               ws.Security.Mode = SecurityMode.None;
               bindinginstance = ws;
           }
           else if (binding.ToLower() == "wshttpbinding")
           {
               WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
               ws.MaxReceivedMessageSize = 65535000;
               ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
               ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
               bindinginstance = ws;
           }
           return bindinginstance;
 
       }
       #endregion
   }

不需要任何配置文件,适合配置多个wcf,然后在后台win服务动态调用的使用场景。


以上所述就是小编给大家介绍的《C#动态调用WCF接口》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

流量池

流量池

杨飞 / 中信出版集团 / 2018-4 / 68.00

移动互联网时代,信息日益冗余,新闻速朽; 整体流量增长速度放缓,而竞争者数量高速增加; 流量呈现变少、变贵、欺诈频繁的现状; 品效合一的营销策略成为共识,而实现路径成为痛点; 多次开创各营销渠道效果之最的营销人、各种刷屏级营销事件操盘手、神州专车CMO杨飞,这一次倾囊相授,诚恳讲述如何实现流量获取、营销转化以及流量的运营和再挖掘。一起来看看 《流量池》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

MD5 加密
MD5 加密

MD5 加密工具