SIGN UP MEMBER LOGIN:    
ARTICLE

Multiple Service Contracts in WCF Service

Posted by Dhananjay Kumar Articles | WCF with C# May 27, 2010
In this article, I will show how we can have multiple contracts in WCF service.
Reader Level:

Objective 

In this article, I will show you, how we can have multiple contracts in WCF service. 

Block Diagram 

1.gif

Above block diagram says 
  1. There are three contracts.
  2. All contracts are being implemented in same service class. 
  3. Each contract is exposed on different end points. 
To start with follow the below steps, 

Step 1: Create a WCF Service Application. 

Step 2

Already by default IService1 contract is being added. Add two more contracts. Right click on the project and select add new item and then choose Interface from Code tab. 

For my purpose, I am adding two more contracts called IMyContract1 and IMyContract2.  So now there are three service contracts in my service.  They are 
  1. IService1
  2. IMyContract1
  3. IMyContract2
IService1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MultipleEndpoints
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetMessagefromIService1();
    
}

IMyContract1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace MultipleEndpoints
{
   [ServiceContract]
    public  interface IMyContract1
    {
       [OperationContract ]
       string GetMessagefromIMyContract1();
    }
}

IMyContract2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace MultipleEndpoints
{
    [ServiceContract]
    public  interface IMyContract2
    {
        [OperationContract]
        string GetMessagefromIMyContract2();
    }
}

Step 3

Implement the contracts in same service class Service1.svc. All  the function is just returning a simple string. 

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MultipleEndpoints
{
    public class Service1 : IService1, IMyContract1, IMyContract2
    {
        public string GetMessagefromIService1()
        {
            return "Calling Default Contract IService1";
        }
        public string GetMessagefromIMyContract1()
        {
            return "Calling My Contract IMyContract1";
        }
        public string GetMessagefromIMyContract2()
        {
            return "Calling My Contract IMyContract2";
        }
    }
}

Step 4 

Now we need to configure the end points for the different contracts.  Just modify System.ServiceModel node in configuration file (Web.Config) as below 

2.gif 

If you see the above end points configuration, different contracts has been exposed on different endpoints.  Address of the end points is given as relative to base address. And each end point contains a unique name also.  Address is unique for each end point. 

Web.Config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web> 
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name ="Mg">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>    
    </behaviors>
    <services >
      <service name ="MultipleEndpoints.Service1"
               behaviorConfiguration ="Mg">
        <endpoint  name="firstBinding"
                   address ="/firstContract"
                   binding ="basicHttpBinding"
                   contract ="MultipleEndpoints.IService1" />
        <endpoint name="secondBinding"
                  address ="/secondContract"
                  binding ="basicHttpBinding"
                  contract ="MultipleEndpoints.IMyContract1"/>
        <endpoint name="thirdBinding"
                  address ="/thirdContract"
                  binding ="basicHttpBinding"
                  contract ="MultipleEndpoints.IMyContract2"/>
        <endpoint contract="IMetadataExchange"
                  binding="mexHttpBinding"
                  address="mex" />
        <host>
          <baseAddresses >
            <add baseAddress ="http://localhost:8180/Service1.svc"/>
          </baseAddresses>
        </host>
      </service >
    </services>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer
</configuration>

Step 5

Press F5 to run the service.  Once service is up and running, we are ready to consume the service in client. 

Step 6 

Create a Console application as client. Add the service reference of the service.  Now you can see, there are three proxy classes have been generated by WCF. Each class is corresponding to each service contracts exposed by the service through the end points. 

3.gif 
 
You can see three service contracts have been exposed to the clients. 
 
4.gif

Above block diagram says the all that, each contract is exposed on different end points.  And WCF created corresponding class for each contracts. 

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Service1Client p = new Service1Client("firstBinding");
            MyContract1Client q = new MyContract1Client("secondBinding");
            MyContract2Client r = new MyContract2Client("thirdBinding");
            Console.WriteLine(p.GetMessagefromIService1());
            Console.WriteLine(q.GetMessagefromIMyContract1());
            Console.WriteLine(r.GetMessagefromIMyContract2());
            Console.Read();
        }
    }
}

When you run, you will get output as 

5.gif

Thanks for reading. I hope article was useful. Happy Coding. 

erver'>
Login to add your contents and source code to this article
share this article :
post comment
 

Nice Article.

Posted by James Veronica Mar 23, 2012

Very Nice article...Tahnk you very much!!!!!!!!!!

Posted by Mandar Pandit Jul 20, 2011

Very Good. Please we would like to see more from you , writing such articles

Posted by srikiran nelli Feb 07, 2011

Hi,
Nice article.........

Posted by Jitendra Sampathirao Nov 12, 2010

can i write multiple contracts ie,Iservice,IMyContract,IMyContract2
in visualStudion.net3.5;

Posted by tovvala chandrashekar Jun 01, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor