WCF 动态调用(动态创建实例接口)

本文介绍如何使用动态方式调用WCF服务,并提供了创建服务对象和传输协议的示例代码,帮助开发者轻松实现服务的动态配置。
 
 

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

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

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

 

[csharp]  view plain copy
 
  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 plain copy
 
  1. IWCFserver dpser = InvokeContext.CreateWCFServiceByURL<IWCFserver>(Public.getXmlElementValue("LocalDpPathologySliceServ"), "basicHttpBinding");  


                                           
 
 

转载于:https://www.cnblogs.com/qinge/p/4380876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值