Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

Developing and Consuming WCF Service - Separating Service, Contract, Proxy and Client

Rate me:
Please Sign up or sign in to vote.
1.65/5 (6 votes)
2 Mar 2010CPOL2 min read 31.4K   483   17   7
Describes how to develop and consume WCF service for large enterprize systems
Download Code - 174.4 KB

 

Few words about WCF

WCF is unified communication platform from Microsoft and most preferred choice for SOA development like earlier we have asmx webservice. WCF provide all the powerful cross platform communication needs and flexibility of hosting in IIS,Windows applications, Console applications, Windows service,MSMQ and various protocols support (like http, named pipes,TCP etc.)and bindings.

The problem area

In Visual studio, WCF service development is as easy as asmx webservice, but service,endpoint (.svc file) contract and configuration all jumbled in one project. And we have to maintain a large web.config file. Most of the WCF hosting and binding configurations are reside inside service configuration file. Traditionally we need to create proxy in each and every project to consume the WCF service, so if we need to consume WCF service in multiple project projects , than need to create same proxy class in different projects. It’s is code duplication. And if there is any change in the WCF service, we need to change the config file and proxy for each and every project.

Solution

If we have one proxy for all projects,separate out contract and service and maintain configuration on the hosting environment instead of within service, would be more suitable for large enterprise systems as it would be up to the host how WCF service would be exposed (configuration at the host point not at service side). I have developed a sample that decoupled service, contract, host and proxy from the client.

WCFSolution.png
Code Discussion

I have created 5 diffeent projects each one for Contract,service,Host,proxy and client.

Contracts and Service projects are class libraries and doesn't have any configuration file.
WCF Proxy is again a class library and client is a windows application, both proxy and client is having the almost the same config entry.
Service is exposing two simple methods GetPrice and GetProductName.

Here the proxy class is derived from the ClientBase<t> class and implementing the IAWContract(service contract) and calling the service through WCF inner channel.
We can also create Channel by using the CreateChannel method of channelFactory and call the service through created channel.

//WCF Proxy class

namespace WCFProxy
{
    public class Proxy:ClientBase<iawcontrproxy>,IAWContrProxy
    {
         public string GetProductName(int ProductID)
         {
               return Channel.GetProductName(ProductID);
         }
         public string GetPrice(int ProductID)
         {
             return Channel.GetPrice(ProductID);
         }
     }
}

<configuration>
    <system.serviceModel>
  <services>
             <service name="WCFService.AWService">
                      <endpoint address="net.tcp://localhost:9005/AWEndPoint" binding="netTcpBinding"   contract="WCFContracts.IAWContract" />
             </service>
    </services>
    </system.serviceModel>
</configuration>/>

//WCF hosting
public static void Main() {     

<t><iawcontrproxy>ServiceHost svcHost = new ServiceHost(typeof(WCFService.AWService));
    svcHost.Open();
    Console.ForegroundColor = ConsoleColor.DarkGreen;
    Console.WriteLine("Service is running...");
    Console.ReadLine();

}


<configuration>
    <system.serviceModel>
<client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint"> <client>        <client>
             <endpoint address="net.tcp://localhost:9005/AWEndPoint"
                binding="netTcpBinding" contract="WCFContracts.IAWContract" />
         </client>
 </system.serviceModel>
</configuration>
<client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint">
       <client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint"> <client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint"> <client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint"> <client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint">
<configuration> <system.servicemodel> <client> <endpoint contract="WCFContracts.IAWContract" binding="netTcpBinding" address="net.tcp://localhost:9005/AWEndPoint">
 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
Anees is working as Sr. Team Lead based in Delhi(India).He is Post graduated in Computer applications and science.

He is having around 11 years of design,analysis and coding experience in Sharepoint, ASP.NET, C#, VB.NET, SQL Server 2000/05, Reporting Services,Analysis Services,VB 6.0 and Crystal Reports.

Comments and Discussions

 
GeneralEasiest way to create and consume WCF Services in asp.net Pin
Lalit24rocks14-May-13 21:45
Lalit24rocks14-May-13 21:45 
GeneralMy vote of 1 Pin
Jeffrey Schaefer26-Nov-11 20:41
Jeffrey Schaefer26-Nov-11 20:41 
GeneralMy vote of 5 Pin
caseyvanduuren1-Aug-10 21:43
caseyvanduuren1-Aug-10 21:43 
GeneralConsuming proxy by SilverLight client Pin
vbsg21-Apr-10 5:04
vbsg21-Apr-10 5:04 
GeneralMy vote of 1 Pin
PCoffey3-Mar-10 3:02
PCoffey3-Mar-10 3:02 
GeneralMy vote of 1 Pin
Sacha Barber3-Mar-10 2:50
Sacha Barber3-Mar-10 2:50 
GeneralJSON-REST Pin
tornbladswe2-Mar-10 8:29
tornbladswe2-Mar-10 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.