Wednesday, November 18, 2009

Consuming WCF services with channel factory



Creating WCF services hosted on IIS server is a very common scenario.
Often service proxy files for a service are generated with help of Visual Studio or by using SvcUtil.exe
Generating proxy is very easy, but I read, that using channel factory is much better and more profesional aproach.

you can read about this on this site:
"...Now we may turn our attention to the client application. To begin, let me start off by reminding everyone that you shouldn't ever use "Add Service Reference" in Visual Studio for magical service client creation. The code is incredibly verbose, hard to manageable, edits are prone to being overwritten, and it's almost always used as an excuse to not actually learn WCF. There are few things worse than having to deal with people who thing they know a product simply because they know how to use a mouse. There are reasons why Juval Lowy, in all his books and talks, repeatedly tells people to avoid using this flawed feature. Fortunately, as professionals, we have the ability to understand how to do things without magic."

For me very important reason is, that I can use my data contract clases from WCF service in my client application. I don't lose functionality implemented in these classes.

Here is my WCF servicve and its client with channel factory

in web application project I inserted service file SVC:

Repository.svc


I skiped implementation of the service class. Important is, that this class implement interface defined in service contract. In this case it is IRepository

WCF configuration in the web application (my service host)





   1:  <service behaviorConfiguration="returnFaults" name="Services.BusinessLayer.Pakete.PaketRepository">
   2:      <endpoint binding="basicHttpBinding" bindingConfiguration="myBinding" contract="Tesa.Etv.Interface.TvAuftrag.ITvAuftragRepository"/>
   3:      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   4:  service>


Service implementation is hosted on specific web site for example on:

http://localhost/Test/Repository.svc

 now its time for a client.
because we don't generate service proxy file, we can reuse assembly with a contract from the server in a client application.
This way we declare service endpoint in clients configuration file:


   1:  <endpoint address="http://localhost/Test&Repository.svc"
   2:  behaviorConfiguration="Delegation" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding"
   3:  contract="Repository" name="BasicHttpBinding_IRepository" />


Channel factory as a property, to comunicate with service in a web client:




   1:  protected ITvAuftragRepository TvAuftragRepository
   2:  {
   3:      get
   4:      {
   5:          if (Session["_tvService"] == null)
   6:          {
   7:              Session["_tvService"] = new ChannelFactory("BasicHttpBinding_ITvAuftragRepository").CreateChannel();
   8:          }
   9:   
  10:          return Session["_tvService"] as ITvAuftragRepository;
  11:      }
  12:  }
this way wen can comnunicate with WCF from the client application, and reuse classes created for the server interfaces

No comments:

Post a Comment