最近在做的一个项目中需要动态调用WCF地址,因为有很多终端服务器,而每台终端服务器上都部署一个WCF服务,中央服务器需要不定时调用其中某个或者多个WCF服务执行相关操作,因此添加引用及配置文件配置的方法就不太现实,以下提供两种动态调用WCF地址的方法:

1、使用ChannelFactory类,该方法虽然复杂了点,但灵活度最高:

code:

/* ========================================================================
* 【本类功能概述】 ChannelFactory 类创建多个终结点侦听器
*
* 作者:EricHu 时间:2012/6/5 14:14:54
* 文件名:WcfChannelFactory
* CLR版本:4.0.30319.235
*
* 修改者: 时间:
* 修改说明:
* ========================================================================
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Reflection; namespace JJInn.Baselibray.Common
{
/// <summary>
/// 使用ChannelFactory为wcf客户端创建独立通道
/// </summary>
public class WcfChannelFactory
{
public WcfChannelFactory()
{
} /// <summary>
/// 执行方法 WSHttpBinding
/// </summary>
/// <typeparam name="T">服务接口</typeparam>
/// <param name="uri">wcf地址</param>
/// <param name="methodName">方法名</param>
/// <param name="args">参数列表</param>
public static object ExecuteMetod<T>(string uri, string methodName, params object[] args)
{
//BasicHttpBinding binding = new BasicHttpBinding(); //出现异常:远程服务器返回错误: (415) Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'.。
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(uri); using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
{
T instance = channelFactory.CreateChannel();
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(methodName);
return mi.Invoke(instance, args);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
} } //nettcpbinding 绑定方式
public static object ExecuteMethod<T>(string pUrl, string pMethodName,params object[] pParams)
{
EndpointAddress address = new EndpointAddress(pUrl);
Binding bindinginstance = null;
NetTcpBinding ws = new NetTcpBinding();
ws.MaxReceivedMessageSize = ;
ws.Security.Mode = SecurityMode.None;
bindinginstance = ws;
using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
{
T instance = channel.CreateChannel();
using (instance as IDisposable)
{
try
{
Type type = typeof(T);
MethodInfo mi = type.GetMethod(pMethodName);
return mi.Invoke(instance, pParams);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception vErr)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}
}
}

调用示例:

string uri = "http://localhost:9998/mywcf/Service";
object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
Console.WriteLine(o.ToString());
Console.ReadKey();

2、简单方法,不考虑太多,直接调用:

EndpointAddress address1 = new EndpointAddress("http://localhost:9998/mywcf/Service");
ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1);
Console.WriteLine(service1.Add(12.0, 13.0).ToString());

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

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

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

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

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

  3. 动态调用WCF服务

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

  4. C#动态调用WCF接口

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

  5. 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址

    一.创建WCF服务器 1.创建WCF服务器的窗体应用程序 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→Windows→Windows窗体应用程序 ...

  6. 动态调用wcf接口服务

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

  7. 记录:Web无引用无配置方式动态调用WCF服务

    这几年一直用WebApi较多,最近项目中有个需求比较适合使用WCF,以前也用过JQuery直接调用Wcf的,但是说实话真的忘了… 所以这次解决完还是花几分钟记录一下 WCF服务端:宿主在现有Win服务 ...

  8. C#动态调用WCF接口(3)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...

  9. C#动态调用WCF接口(2)

    如何使用 1.第一种方式比较简单,而且也是大家喜欢的,因为不需要任何配置文件就可解决,只需知道服务契约接口和服务地址就可以调用. 2.使用Invoke的方式,但是需要在调用客户端配置WCF,配置后在I ...

随机推荐

  1. ASP.NET MVC C#知识点提要

    ASP.NET MVC C#知识点提要 本篇博文主要对asp.net mvc开发需要撑握的C#语言知识点进行简单回顾,尤其是C# 3.0才有的一些C#语言特性.对于正在学asp.net mvc的童鞋, ...

  2. PHP5.3 里面数组的的实现方式

    typedef struct _Bucket { char *key; void *value; struct _Bucket *next; } Bucket; typedef struct _Has ...

  3. UVA 216 - Getting in Line

    216 - Getting in Line Computer networking requires that the computers in the network be linked. This ...

  4. lucene 从2.4.0—3.6.0—4.3.1版本升级

    一.从2.4升级到3.6 替换原因:由于使用IBM的jdk导致了查询出现不稳定现象,原因无法找到,只好升级版本,毕竟版本很低 1)替换中文分词器,由原来的MMAnaylze替换为IKAnaylze 2 ...

  5. NodeJs+Express实现简单的Web增删改查

    前一段时间,公司组织了一次NodeJs的技术分享,自己有幸去听了听,第一次接触NodeJs,后来经过自己学习和探索,完成了一个很简单的Web演示项目,在这里和初学者做以分享,开发工具:WebStorm ...

  6. SQL注入(二)

    5.限制输入长度 如果在Web页面上使用文本框收集用户输入的数据,使用文本框的MaxLength属性来限制用户输入过长的字符也是一个很好的方法,因为用户的输入不够长,也就减少了贴入大量脚本的可能性.程 ...

  7. 【Excel】Excel筛选迟点时间的公式

    效果是这样: 方法: 在B列第2排,输入=if(HOUR(A2) >=9,"迟点","") 然后就是复制,粘贴整个列就OK了.不想复制也可以按住的右下角那 ...

  8. shell 分支/循环

    ==)); then patern="Update" else patern="Read" fi in "-h") ] then helpI ...

  9. MFC中CListCtrl说明

    转载:http://blog.csdn.NET/lhy2199/article/details/5177032 listctrl默认view 风格为report CListCtrl类封装"列 ...

  10. 【锋利的Jquery】读书笔记六

    ajax优点缺点 json格式的严格 { "people": [ { "firstName": "Brett", "lastNam ...