动态调用WCF地址 (使用ChannelFactory类)
2012-06-05 15:45 Eric.Hu 阅读(3943) 评论(7) 编辑 收藏 举报最近在做的一个项目中需要动态调用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
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using System.Text;
17 using System.ServiceModel;
18 using System.ServiceModel.Channels;
19 using System.Reflection;
20
21 namespace JJInn.Baselibray.Common
22 {
23 /// <summary>
24 /// 使用ChannelFactory为wcf客户端创建独立通道
25 /// </summary>
26 public class WcfChannelFactory
27 {
28 public 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>
39 public static object ExecuteMetod<T>(string uri, string methodName, params object[] 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
45 using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
46 {
47 T instance = channelFactory.CreateChannel();
48 using (instance as IDisposable)
49 {
50 try
51 {
52 Type type = typeof(T);
53 MethodInfo mi = type.GetMethod(methodName);
54 return mi.Invoke(instance, args);
55 }
56 catch (TimeoutException)
57 {
58 (instance as ICommunicationObject).Abort();
59 throw;
60 }
61 catch (CommunicationException)
62 {
63 (instance as ICommunicationObject).Abort();
64 throw;
65 }
66 catch (Exception vErr)
67 {
68 (instance as ICommunicationObject).Abort();
69 throw;
70 }
71 }
72 }
73
74
75 }
76
77
78 //nettcpbinding 绑定方式
79 public static object ExecuteMethod<T>(string pUrl, string pMethodName,params object[] 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;
89 using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
90 {
91 T instance = channel.CreateChannel();
92 using (instance as IDisposable)
93 {
94 try
95 {
96 Type type = typeof(T);
97 MethodInfo mi = type.GetMethod(pMethodName);
98 return mi.Invoke(instance, pParams);
99 }
100 catch (TimeoutException)
101 {
102 (instance as ICommunicationObject).Abort();
103 throw;
104 }
105 catch (CommunicationException)
106 {
107 (instance as ICommunicationObject).Abort();
108 throw;
109 }
110 catch (Exception vErr)
111 {
112 (instance as ICommunicationObject).Abort();
113 throw;
114 }
115 }
116 }
122 }
123 }
124 }
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
13 using System;
14 using System.Collections.Generic;
15 using System.Linq;
16 using System.Text;
17 using System.ServiceModel;
18 using System.ServiceModel.Channels;
19 using System.Reflection;
20
21 namespace JJInn.Baselibray.Common
22 {
23 /// <summary>
24 /// 使用ChannelFactory为wcf客户端创建独立通道
25 /// </summary>
26 public class WcfChannelFactory
27 {
28 public 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>
39 public static object ExecuteMetod<T>(string uri, string methodName, params object[] 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
45 using (ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint))
46 {
47 T instance = channelFactory.CreateChannel();
48 using (instance as IDisposable)
49 {
50 try
51 {
52 Type type = typeof(T);
53 MethodInfo mi = type.GetMethod(methodName);
54 return mi.Invoke(instance, args);
55 }
56 catch (TimeoutException)
57 {
58 (instance as ICommunicationObject).Abort();
59 throw;
60 }
61 catch (CommunicationException)
62 {
63 (instance as ICommunicationObject).Abort();
64 throw;
65 }
66 catch (Exception vErr)
67 {
68 (instance as ICommunicationObject).Abort();
69 throw;
70 }
71 }
72 }
73
74
75 }
76
77
78 //nettcpbinding 绑定方式
79 public static object ExecuteMethod<T>(string pUrl, string pMethodName,params object[] 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;
89 using (ChannelFactory<T> channel = new ChannelFactory<T>(bindinginstance, address))
90 {
91 T instance = channel.CreateChannel();
92 using (instance as IDisposable)
93 {
94 try
95 {
96 Type type = typeof(T);
97 MethodInfo mi = type.GetMethod(pMethodName);
98 return mi.Invoke(instance, pParams);
99 }
100 catch (TimeoutException)
101 {
102 (instance as ICommunicationObject).Abort();
103 throw;
104 }
105 catch (CommunicationException)
106 {
107 (instance as ICommunicationObject).Abort();
108 throw;
109 }
110 catch (Exception vErr)
111 {
112 (instance as ICommunicationObject).Abort();
113 throw;
114 }
115 }
116 }
122 }
123 }
124 }
调用示例:
1 string uri = "http://localhost:9998/mywcf/Service";
2 object o = ExecuteMetod<IService>(uri, "Add",12.0,13.0);
3 Console.WriteLine(o.ToString());
4 Console.ReadKey();
2 object 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());
2 ServiceClient service1 = new ServiceClient(new WSHttpBinding(), address1);
3 Console.WriteLine(service1.Add(12.0, 13.0).ToString());
着意耕耘,自有收获。