zoukankan      html  css  js  c++  java
  • 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#之Raw Socket实现网络封包监视
    es6Promise及小程序Promise用法
    在微信小程序的JS脚本中使用Promise来优化函数处理
    小程序踩过的一个小坑---解析二维码decodeURIComponent() url解码
    js json转url参数
    微信小程序-实现分享(带参数)
    php中的匿名函数和闭包(closure)
    微信小程序之回调函数
    php AES cbc模式 pkcs7 128位加密解密(微信小程序)
    微信小程序,开发中几个重要的知识点(加密解密,转发,进入场景,session_key)
  • 原文地址:https://www.cnblogs.com/tuyile006/p/6999627.html
Copyright © 2011-2022 走看看