In a recent project, you need to dynamically call the WCF address. Because there are many terminal servers, each terminal server is deployed with a WCF Service, the central server needs to call one or more of the WCF services to perform relevant operations from time to time. Therefore, it is unrealistic to add reference and configuration files, the following two methods are provided to dynamically call the WCF address:
1. Using the channelfactory class, although this method is complex, it has the highest flexibility:
Code:
1/* ============================================== ==========================================
2 * [function overview] The channelfactory class creates multiple end point listeners.
3 *
4 * Author: erichu time: 14:14:54
5 * file name: wcfchannelfactory
6 * CLR version: 4.0.30319.235
7 *
8 * modifier: Time:
9 * modification instructions:
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 // use channelfactory to create an independent channel for the WCF Client
25 /// </Summary>
26 public class wcfchannelfactory
27 {
28 public wcfchannelfactory ()
29 {
30}
31
32 /// <summary>
33 // execution method wshttpbinding
34 /// </Summary>
35 // <typeparam name = "T"> service interface </typeparam>
36 // <Param name = "Uri"> WCF address </param>
37 // <Param name = "methodname"> method name </param>
38 // <Param name = "ARGs"> parameter list </param>
39 public static object executemetod <t> (string Uri, string methodname, Params object [] ARGs)
40 {
41 // basichttpbinding binding = new basichttpbinding (); // exception: the remote server returns an error: (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 = 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 Method
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. maxcomputebattersize = 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 = 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}
Call example:
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. Simple method. If you do not consider too much, call it directly:
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 ());