动态调用WCF地址 (应用ChannelFactory类)
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 uteMetod<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 uteMethod<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 }
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 uteMetod<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 uteMethod<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 = uteMetod<IService>(uri, "Add",12.0,13.0);
3 Console.WriteLine(o.ToString());
4 Console.ReadKey();
2 object o = uteMetod<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());

浙公网安备 33010602011771号