There are 3 basic ways to create a WCF client:
-
Let Visual Studio generate your proxy. this auto generates code that connects to the service by reading the WSDL. if the service changes for any reason you have to regenerate it. the big advantage of this is that it is easy to set up-Vs has a wizard and it's all automatic. the disadvantage is that you're relying on vs to do all the hard work for you, and so you lose control.
-
use channelfactory
with a known interface. this relies on you having local interfaces that describe the service (the service contract ). the big advantage is that can manage change much more easily-you still have to recompile and fix changes, but now you're not regenerating code, you're referencing the new interfaces. commonly this is used when you control both server and client as both can be much more easily mocked for unit testing. however the interfaces can be written for any service, even rest ones-Take A Look At This Twitter API.
Write your own proxy-this is fairly easy to do, especially for rest services, usingHttpclient
OrWebClient
. This gives you the most fine grain control, but at the cost of lots of Service API being in strings. For instance:VaR content = new httpclient (). Get ("http://yoursite.com/resource/id"). content;
-If the details of the API change you won't encounter an error until runtime.
Personally I 've never liked Option 1-relying on the auto generated code is messy and loses too much control. plus it often creates serialisation issues-I end up with two identical classes (one in the server code, one auto generated) which can be tided up but is a pain.
Option 2 shoshould be perfect, but channels are a little too limiting-for instance they completely lose the content of HTTP errors. that said having interfaces that describe the service is much easier to code with and maintain.
Example for using channelfactory.
I find myself re-using this sort of snippet often, so I'm posting it here for all to enjoy.
The issue here is: using the built in proxy Generator for referencing WCF services from client code is OK, but a neater way to do it is shown below-this only works if the server and client code are both controlled by the author-its no good for publicly exposed web services where you need CT the user to query the WSDL or service metadata as this will always end up with a svcutil built proxy.
So what you do is, pull out your interface and your contract objects, to a 3rd Assembly, and reference that from your service code and also your client. then simply use the class shown below, from the client, plugging in the contract interface where the generic type is required. add a Config line pointing at the service endpoint. voila!
Public Class Servicewrapper <t>: idisposable Where T: Class {Channelfactory <T> Factory; Private T channel; Private Readonly Basichttpbinding binding; Private Readonly Endpointaddress endpoint; Private Readonly Object Lockobject = New Object (); Private Bool Disposed; Public Servicewrapper ( String Configname ){ If (Configurationmanager. etettings [configname] = Null ){ Throw New Configurationerrorsexception (configname + " Is not present in the config file " );} Binding = New Basichttpbinding (); endpoint = New Endpointaddress (configurationmanager. deleetask[ configname]); disposed = False ;} Public T channel { Get { If (Disposed ){ Throw New Objectdisposedexception ( " Resource servicewrapper < " + Typeof (T) + " > Has been disposed " );} Lock (Lockobject ){ If (Factory = Null ) {Factory = New Channelfactory <t> (Binding, endpoint); Channel = Factory. createchannel ();}} Return Channel ;}} Public Void Dispose () {dispose ( True ); GC. suppressfinalize ( This );} Public Void Dispose ( Bool Disposing ){ If (!Disposed ){ If (Disposing ){ Lock (Lockobject ){ If (Channel! = Null ) {(Iclientchannel) channel). Close ();} If (Factory! = Null ) {Factory. Close () ;}} Channel = Null ; Factory = Null ; Disposed = True ;}}}}
Usage:
1. Don't forget to wrap the service wrapper in a using statement so that the dispose gets called when it goes out of scope.
2. must use the same binding as the Service binding.
3. For convenience. You can share the service contract between service and client. Which compiled service contract to a DLL.
Private Const StringService_endpoint ="Serviceendpoint";...Using(VaRSw =NewServicewrapper <idataservice>(Service_endpoint) {mytype t= Sw. Channel. myfunction (3);}
Config:
<Appsettings><AddKey= "Serviceendpoint"Value= "Http: // localhost: 55757/dataservice. SVC" /> <! -- This endpoint address cocould be the service base URI + endpoint address. --></Appsettings>
See also
Loading the WCF configuration from different files on the client side