最近在做的一個項目中需要動態調用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());
