• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#动态调用WCF接口

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

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服务动态调用的使用场景。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
LINQ查询表达式(2)-在C#中编写LINQ查询发布时间:2022-07-13
下一篇:
递归下降的表达式解析器(C#)发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap