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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Channels; /// <summary>
/// 动态调用WCF的工具类库
/// </summary>
public class InvokeContext
{ #region Wcf服务工厂
public static T CreateWCFServiceByURL<T>(string url)
{
return CreateWCFServiceByURL<T>(url, "wsHttpBinding");
} public static T CreateWCFServiceByURL<T>(string url, string bing)
{
if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
EndpointAddress address = new EndpointAddress(url);
Binding binding = CreateBinding(bing);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
return factory.CreateChannel();
}
#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 = ;
ws.MaxBufferPoolSize = ;
ws.MaxReceivedMessageSize = ;
ws.ReaderQuotas.MaxStringContentLength = ;
ws.CloseTimeout = new TimeSpan(, , );
ws.OpenTimeout = new TimeSpan(, , );
ws.ReceiveTimeout = new TimeSpan(, , );
ws.SendTimeout = new TimeSpan(, , ); bindinginstance = ws;
}
else if (binding.ToLower() == "netnamedpipebinding")
{
NetNamedPipeBinding ws = new NetNamedPipeBinding();
ws.MaxReceivedMessageSize = ;
bindinginstance = ws;
}
else if (binding.ToLower() == "netpeertcpbinding")
{
NetPeerTcpBinding ws = new NetPeerTcpBinding();
ws.MaxReceivedMessageSize = ;
bindinginstance = ws;
}
else if (binding.ToLower() == "nettcpbinding")
{
NetTcpBinding ws = new NetTcpBinding();
ws.MaxReceivedMessageSize = ;
ws.Security.Mode = SecurityMode.None;
bindinginstance = ws;
}
else if (binding.ToLower() == "wsdualhttpbinding")
{
WSDualHttpBinding ws = new WSDualHttpBinding();
ws.MaxReceivedMessageSize = ; bindinginstance = ws;
}
else if (binding.ToLower() == "webhttpbinding")
{
//WebHttpBinding ws = new WebHttpBinding();
//ws.MaxReceivedMessageSize = 65535000;
//bindinginstance = ws;
}
else if (binding.ToLower() == "wsfederationhttpbinding")
{
WSFederationHttpBinding ws = new WSFederationHttpBinding();
ws.MaxReceivedMessageSize = ;
bindinginstance = ws;
}
else if (binding.ToLower() == "wshttpbinding")
{
WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
ws.MaxReceivedMessageSize = ;
ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
bindinginstance = ws;
}
return bindinginstance; }
#endregion }
IWCFserver 是通过 

svcutil.exe http://localhost:8034/WCFserver.svc?wsdl
IWCFserver dpser = InvokeContext.CreateWCFServiceByURL<IWCFserver>(Public.getXmlElementValue("LocalDpPathologySliceServ"), "basicHttpBinding");

WCF 动态调用(1)的更多相关文章

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

    很多时候,服务地址都不止一个的,这个时候就要动态去配置地址.配置Web.config,很麻烦 下面就看看怎样实现动态调用WCF. 首先看看动态创建服务对象的代码: using System; usin ...

  2. 使用接口的方式调用远程服务 ------ 利用动态调用服务,实现.net下类似Dubbo的玩法。

    分布式微服务现在成为了很多公司架构首先项,据我了解,很多java公司架构都是 Maven+Dubbo+Zookeeper基础上扩展的. Dubbo是Alibaba开源的分布式服务框架,它最大的特点是按 ...

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

    //动态调用wcf方法 string url = "http://localhost:54379/ServiceWCF.svc"; IDoubleService proxy = W ...

  4. C#动态调用WCF接口,两种方式任你选。

    写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项目时用到了WCF. 从这 ...

  5. 动态调用webservice及WCF服务

    动态调用web服务,该方法只针对Web service, WCF的服务不行,如果是WCF的就通过工具直接生产代理类,把代理类配置到调用的项目中,通过配置客户端的终结点动态的取实现: 通过Svcutil ...

  6. 动态调用WCF服务

    动态调用WCF服务,只需要提供*.svc地址, 1:命名空间: using System.ServiceModel.Channels;using System.ServiceModel; 2:创建访问 ...

  7. 动态调用wcf接口服务

    1.url:http://localhost:8002/名称.svc/basic(.svc结尾) 2.需要引用的命名空间System.ServiceModel 3.调用代码: public class ...

  8. WCF入门及在WinForm中动态调用

    一.WCF入门 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序,删除系统生成的两个文件IService1.cs与Service1.svc, 添加自定义的WCF[服务文 ...

  9. C#动态调用WCF接口

    C#动态调用WCF接口 写在前面 接触WCF还是它在最初诞生之处,一个分布式应用的巨作. 从开始接触到现在断断续续,真正使用的项目少之又少,更谈不上深入WCF内部实现机制和原理去研究,最近自己做一个项 ...

随机推荐

  1. [BZOJ2823][BZOJ1336][BZOJ1337]最小圆覆盖(随机增量法)

    算法介绍网上有很多,不解释了. 给出三点坐标求圆心方法:https://blog.csdn.net/liyuanbhu/article/details/52891868 记得先random_shuff ...

  2. [TC-FindingFriends]Finding Friends

    题目大意: 给定一个长度为$n(n\le10^5)$的数列$A(A_i\le10^9)$,求最小的$k$满足存在一个长度至少为$m(m\le n)$的子串,对于串中的每一个数$A_i$,都至少存在一个 ...

  3. Python学习实践 | speech智能语音模块

    最近的生活.学习节奏很是容易被打断,终于,在今天,既实习结束之后,夏令营也结束了. 前几天,一个人在复习地很累的时候,又重新将Python捡了起来,看了挺多的知识点. 真是太有意(wu)思(liao) ...

  4. Scala实战高手****第6课 :零基础实战Scala集合操作及Spark源码解析

    本课内容1.Spark中Scala集合操作鉴赏2.Scala集合操作实战 --------------------------------------------------------------- ...

  5. js发送验证码(倒计时)

    需求:在用户点击获取验证码后60s内不能再次获取 代码 //倒计时60秒 var countdown=60; //显示获取验证码倒计时的文本标签 var _generate_code =$(" ...

  6. HTML5 Boilerplate笔记(2)(转)

    最近看到了HTML5 Boilerplate模版,系统的学习与了解了一下.在各种CSS库.JS框架层出不穷的今天,能看到这么好的HTML模版,感觉甚爽.写篇博客,推荐给大家使用.   一:HTML5 ...

  7. 在iPhone上取消APP订阅

    1.在iPhone上打开APP Store,在精品推荐那个界面滚动到底部. 2.点击Apple ID那一栏,如图: 3.点击查看Apple ID,如图: 4.点击管理,如图: 5.关掉此选项,如图: ...

  8. 【Linux】ubuntu或linux网卡配置/etc/network/interfaces

    转自:http://gfrog.net/2008/01/config-file-in-debian-interfaces-1/   青蛙准备写一个系列文章,介绍一些Debian/Ubuntu里面常用的 ...

  9. ORACLE查看并修改最大连接数的具体步骤

      第一步,在cmd命令行,输入sqlplus 第二步,根据提示输入用户名与密码 1. 查看processes和sessions参数 SQL> show parameter processes ...

  10. python获取系统信息,

    1 操作系统系统综合信息cur_sysinfo=platform.uname()浏览器信息cur_browserinfo=self.driver.capabilities['version']浏览器新 ...