动态调用WCF地址 (使用ChannelFactory类)
来源:互联网 发布:nginx配置php域名访问 编辑:程序博客网 时间:2024/05/18 05:40
动态调用WCF地址 (使用ChannelFactory类)
最近在做的一个项目中需要动态调用WCF地址,因为有很多终端服务器,而每台终端服务器上都部署一个WCF服务,中央服务器需要不定时调用其中某个或者多个WCF服务执行相关操作,因此添加引用及配置文件配置的方法就不太现实,以下提供两种动态调用WCF地址的方法:
1. 使用ChannelFactory类,该方法虽然复杂了点,但灵活度最高:
code:
1/* ========================================================================2* 【本类功能概述】 ChannelFactory 类创建多个终结点侦听器
3*
4* 作者:EricHu 时间:2012/6/5 14:14:54
5* 文件名:WcfChannelFactory
6* CLR版本:4.0.30319.235
7*
8* 修改者: 时间:
9* 修改说明:
10* ========================================================================
11*/
12
13using System;
14using System.Collections.Generic;
15using System.Linq;
16using System.Text;
17using System.ServiceModel;
18using System.ServiceModel.Channels;
19using System.Reflection;
20
21namespace JJInn.Baselibray.Common
22 {
23///<summary>
24/// 使用ChannelFactory为wcf客户端创建独立通道
25///</summary>
26publicclass WcfChannelFactory
27 {
28public WcfChannelFactory()
29 {
30 }
31
32///<summary>
33/// 执行方法 WSHttpBinding
34///</summary>
35///<typeparam name="T">服务接口</typeparam>
36///<param name="uri">wcf地址</param>
37///<param name="methodName">方法名</param>
38///<param name="args">参数列表</param>
39publicstaticobject ExecuteMetod<T>(string uri, string methodName, paramsobject[] args)
40 {
41//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'.。
42 WSHttpBinding binding = new WSHttpBinding();
43 EndpointAddress endpoint = new EndpointAddress(uri);
44
45using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
46 {
47 T instance = channelFactory.CreateChannel();
48using (instance as IDisposable)
49 {
50try
51 {
52 Type type = typeof(T);
53 MethodInfo mi = type.GetMethod(methodName);
54return mi.Invoke(instance, args);
55 }
56catch (TimeoutException)
57 {
58 (instance as ICommunicationObject).Abort();
59throw;
60 }
61catch (CommunicationException)
62 {
63 (instance as ICommunicationObject).Abort();
64throw;
65 }
66catch (Exception vErr)
67 {
68 (instance as ICommunicationObject).Abort();
69throw;
70 }
71 }
72 }
73
74
75 }
76
77
78//nettcpbinding 绑定方式
79publicstaticobject ExecuteMethod<T>(string pUrl, string pMethodName,paramsobject[] pParams)
80 {
83 EndpointAddress address = new EndpointAddress(pUrl);
84 Binding bindinginstance = null;
85 NetTcpBinding ws = new NetTcpBinding();
86 ws.MaxReceivedMessageSize = 20971520;
87 ws.Security.Mode = SecurityMode.None;
88 bindinginstance = ws;
89using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
90 {
91 T instance = channel.CreateChannel();
92using (instance as IDisposable)
93 {
94try
95 {
96 Type type = typeof(T);
97 MethodInfo mi = type.GetMethod(pMethodName);
98return mi.Invoke(instance, pParams);
99 }
100catch (TimeoutException)
101 {
102 (instance as ICommunicationObject).Abort();
103throw;
104 }
105catch (CommunicationException)
106 {
107 (instance as ICommunicationObject).Abort();
108throw;
109 }
110catch (Exception vErr)
111 {
112 (instance as ICommunicationObject).Abort();
113throw;
114 }
115 }
116 }
122 }
123 }
124 }
调用示例:
1string uri = "http://localhost:9998/mywcf/Service";2object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
3 Console.WriteLine(o.ToString());
4 Console.ReadKey();
2.简单方法,不考虑太多,直接调用:
1 EndpointAddress address1 = new EndpointAddress("http://localhost:9998/mywcf/Service");2 ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1);
3 Console.WriteLine(service1.Add(12.0, 13.0).ToString());
0 0
- 动态调用WCF地址 (使用ChannelFactory类)
- 动态调用WCF地址 (使用ChannelFactory类)
- 使用ChannelFactory类实现动态调用WCF地址
- WCF - ChannelFactory
- WCF ChannelFactory
- DuplexChannelFactory 使用WCF创建订阅发布程序时,客户端点击订阅时出现“ChannelFactory.Endpoint 上的地址属性为空。ChannelFactory 的终结点必
- 客户端动态调用wcf引用地址
- 学习WCF之路4:ChannelFactory的使用
- ChannelFactory 异步调用
- 动态调用WCF的工具类库
- 动态调用WCF示例
- 动态调用WCF服务
- wcf 动态调用
- ChannelFactory<TChannel> 类
- Wince 上动态调用Wcf
- 动态配置Silverlight调用WCF
- WCF透明代理类,动态调用,支持async-awai
- WCF透明代理类,动态调用,支持async/await
- WebApp实时开源框架Clouda入门使用与记录
- 对于page rank的理解和java简单实现
- 变换数组使得奇偶数分开
- 浏览文件夹路径--获取路径
- 函数小记
- 动态调用WCF地址 (使用ChannelFactory类)
- cocos2d-x引入中文支持库并移植到android平台
- 电子邮件营销主题个性化优化趋势
- 实习以来完成的第一个小项目:Websphere MQ远程查看软件
- static inline内联函数
- Callback回调机制知识大全
- vmware ESX5性能监控
- Linux下高并发socket最大连接数所受的各种限制
- 无法向项目添加所有必需的程序包。无法从“C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages”安装以下程序包: