zoukankan      html  css  js  c++  java
  • WCF 动态调用(动态创建实例接口)

     
     

    很多时候,服务地址都不止一个的,这个时候就要动态去配置地址。配置Web.config,很麻烦

    下面就看看怎样实现动态调用WCF。

    首先看看动态创建服务对象的代码:

    [csharp] view plaincopy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.ServiceModel;  
    6. using System.ServiceModel.Channels;  
    7.   
    8.   
    9. /// <summary>  
    10. /// 动态调用WCF的工具类库  
    11. /// </summary>  
    12. public class InvokeContext  
    13. {  
    14.  
    15.     #region Wcf服务工厂  
    16.     public static T CreateWCFServiceByURL<T>(string url)  
    17.     {  
    18.         return CreateWCFServiceByURL<T>(url, "wsHttpBinding");  
    19.     }  
    20.   
    21.   
    22.     public static T CreateWCFServiceByURL<T>(string url, string bing)  
    23.     {  
    24.         if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");  
    25.         EndpointAddress address = new EndpointAddress(url);  //终结点地址
    26.         Binding binding = CreateBinding(bing);  返回一个包含在当前绑定中的已排序的绑定元素集合
    27.         ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);  //由通道工厂生成的通道类型。此类型必须
    28. //为 IOutputChannel 或 IRequestChannel。
    29.         return factory.CreateChannel();  //创建 TChannel 类型(类的泛型参数)的通道。
    30.     }  
    31.     #endregion  
    32.  
    33.     #region 创建传输协议  
    34.     /// <summary>  
    35.     /// 创建传输协议  
    36.     /// </summary>  
    37.     /// <param name="binding">传输协议名称</param>  
    38.     /// <returns></returns>  
    39.     private static Binding CreateBinding(string binding)  
    40.     {  
    41.         Binding bindinginstance = null;  
    42.         if (binding.ToLower() == "basichttpbinding")  
    43.         {  
    44.             BasicHttpBinding ws = new BasicHttpBinding();  
    45.             ws.MaxBufferSize = 2147483647;  
    46.             ws.MaxBufferPoolSize = 2147483647;  
    47.             ws.MaxReceivedMessageSize = 2147483647;  
    48.             ws.ReaderQuotas.MaxStringContentLength = 2147483647;  
    49.             ws.CloseTimeout = new TimeSpan(0, 10, 0);  
    50.             ws.OpenTimeout = new TimeSpan(0, 10, 0);  
    51.             ws.ReceiveTimeout = new TimeSpan(0, 10, 0);  
    52.             ws.SendTimeout = new TimeSpan(0, 10, 0);  
    53.   
    54.             bindinginstance = ws;  
    55.         }  
    56.         else if (binding.ToLower() == "netnamedpipebinding")  
    57.         {  
    58.             NetNamedPipeBinding ws = new NetNamedPipeBinding();  
    59.             ws.MaxReceivedMessageSize = 65535000;  
    60.             bindinginstance = ws;  
    61.         }  
    62.         else if (binding.ToLower() == "netpeertcpbinding")  
    63.         {  
    64.             NetPeerTcpBinding ws = new NetPeerTcpBinding();  
    65.             ws.MaxReceivedMessageSize = 65535000;  
    66.             bindinginstance = ws;  
    67.         }  
    68.         else if (binding.ToLower() == "nettcpbinding")  
    69.         {  
    70.             NetTcpBinding ws = new NetTcpBinding();  
    71.             ws.MaxReceivedMessageSize = 65535000;  
    72.             ws.Security.Mode = SecurityMode.None;  
    73.             bindinginstance = ws;  
    74.         }  
    75.         else if (binding.ToLower() == "wsdualhttpbinding")  
    76.         {  
    77.             WSDualHttpBinding ws = new WSDualHttpBinding();  
    78.             ws.MaxReceivedMessageSize = 65535000;  
    79.   
    80.             bindinginstance = ws;  
    81.         }  
    82.         else if (binding.ToLower() == "webhttpbinding")  
    83.         {  
    84.             //WebHttpBinding ws = new WebHttpBinding();  
    85.             //ws.MaxReceivedMessageSize = 65535000;  
    86.             //bindinginstance = ws;  
    87.         }  
    88.         else if (binding.ToLower() == "wsfederationhttpbinding")  
    89.         {  
    90.             WSFederationHttpBinding ws = new WSFederationHttpBinding();  
    91.             ws.MaxReceivedMessageSize = 65535000;  
    92.             bindinginstance = ws;  
    93.         }  
    94.         else if (binding.ToLower() == "wshttpbinding")  
    95.         {  
    96.             WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);  
    97.             ws.MaxReceivedMessageSize = 65535000;  
    98.             ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;  
    99.             ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;  
    100.             bindinginstance = ws;  
    101.         }  
    102.         return bindinginstance;  
    103.   
    104.     }  
    105.     #endregion  
    106.   
    107. }  


    IWCFserver 是通过 
    svcutil.exe http://localhost:8034/WCFserver.svc?wsdl
     自动生成的。
    [csharp] view plaincopy
     
    1. IWCFserver dpser = InvokeContext.CreateWCFServiceByURL<IWCFserver>(Public.getXmlElementValue("LocalDpPathologySliceServ"), "basicHttpBinding");  


                                               
     
     
  • 相关阅读:
    Jmeter分布测试
    SQL命令
    Linux执行命令时遇到的些问题
    Linux常用命令总结
    Jenkins与SVN持续集成
    在linux上创建slave节点
    内网域名配置方法
    Java中重写与重载的区别
    MongoDB基本使用
    MongoDB安装
  • 原文地址:https://www.cnblogs.com/qinge/p/4380876.html
Copyright © 2011-2022 走看看