There are basically two methods for a customer to call the WCF Service. Of course, they all need to be programmed ......
1. Use proxy
In general, svcutil.exe is used to obtain the metadata of the WCF Service and generate a proxy class that can be directly called by the client. The general syntax is as follows:
Svcutil.exe http: // localhost: 9000/myservice. svc/out proxy. CS
Svcutil.exe http: // localhost: 9001/MEX/out proxy. CS
For the followingCodeThe client proxy class will generate the corresponding call class.
Server:
[Servicecontract (namespace = "mynamespace")] interface imycontract {[operationcontract] void mymethod ();} class myservice: imycontract {public void mymethod (){....}}
Client proxy:
[Servicecontract (namespace = "mynamespace")] interface imycontract {[operationcontract (Action = "mynamespace/imycontract/mymethod", replyaction = "mynamespace/imycontract/mymethodresponse"] void mymethod ();} public partial class mycontractclient: clientbase <imycontract>, imycontract {public mycontractclient () {} public mycontractclient (string endpointname): Base (endpointname) {} public mycontractclient (binding, endpointaddress remoteaddress): Base (binding, remoteaddress) {} public void mymethod () {channel. mymethod ();}}
Although the Code is automatically generated, it is also necessary to take a look at it. Proxy will also generate the imycontract interface for the client to be out of use. Theoretically, we can use the same interface defined by the server to transparently call the public method of the service. The basis of this is that the actual operation class of the WCF client proxy class implements the imycontract class, and the Implementation class encapsulates all the details of the WCF client-server communication.ProgramYou do not need to create the connection, set the connection, open the connection, and call the method. Just like writing the server code, call the server method directly with imycontract reference. This is also the benefit of "interface-based programming" that comrades often hear.
Before calling the proxy class, you also need to configure the client's WCF information. These things cannot be solidified in the proxy class because they may change. Therefore, it is appropriate to write them in the configuration file, it is also relatively simple, basically directly copy from the server side, and then modify it to OK!
<System. servicemodel> <client name = "mynamespace. myservice "> <endpoint contract =" mynamespace. imycontract "binding =" wshttpbinding "address =" http: // localhost: 8000/myservice "/> </client> </system. servicemodel>
Then you can create a proxy instance:
Mycontractclient proxy = new mycontractclient (); // if multiple endpoints are configured, specify the endpoint name proxy. mymethod (); proxy. Close ();
However, I prefer the following method, which looks the same as writing the server code:
Imycontract myservice = new mycontractclient (); myservice. mymethod (); myservice. Close ();
Let's take a look at the contractclient (binding, endpointaddress remoteaddress) constructor. We can know that you can set the endpoint information in addition to the configuration file, and you can also change the information. However, compared with the previous method, this method is more like hard coding and is not recommended. You can find this method.
2. Use Channel
There is a bad thing about the proxy class. When the server contract changes, the proxy needs to be regenerated, which does not seem to be a big problem, however, I did encounter a similar exception during the development process, indicating that the server and client class definitions do not match... Channelfactory is flexible if no proxy class is required. I will not talk about the details here. It is easy to use the code first.
Similarly, After configuring the client endpoint:
Channelfactory <imycontract> factory = new channelfactory <imycontract> (); imycontract proxy = factory. createchannel (); Using (proxy as idisposable) {proxy. mymethod ();}
If no endpoint is configured, you can do it by programming:
Binding binding = new nettcpbinding (); endpointaddress Address = new endpointaddress ("net. TCP: // localhost: 9000 "); imycontract proxy = channelfactory <imycontract>. createchannel (binding, address); Using (proxy as idisposable) {proxy. mymethod ();}
In fact, it is basically the same, because looking at the above example using proxy, the final call is also a channel...