• c# 动态调用WCF方法笔记!


    //动态调用wcf方法
    string url = "http://localhost:54379/ServiceWCF.svc";
    IDoubleService proxy = WcfInvokeFactory.CreateServiceByUrl<IDoubleService>(url);
    int result = proxy.Add(1, 3);
     
    //wcf WCF服务工厂创建方法
    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
       }
  • 相关阅读:
    golang 简单的实现内 网 穿 透,用户访问本地服务。
    golang 创建一个简单的广播echo服务器
    golang 使用 protobuf 的教程
    golang语言中os包的学习与使用(文件,目录,进程的操作)
    【原】画流程图工具visio使用技巧汇总
    【改】IOS-百度地图API用点生成线路、导航、自定义标注 2013年11月更新
    【原】xcode5&IOS7及以下版本免证书真机调试记录
    【转】C++的拷贝构造函数深度解读,值得一看
    【转】c++中引用的全方位解读
    【转】self.myOutlet=nil、viewDidUnload、dealloc的本质剖析
  • 原文地址:https://www.cnblogs.com/jicheng/p/6244803.html
Copyright © 2020-2023  润新知