Programmer and Consultant

Menu

Monthly Archives: May 2010

Post navigation

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

Block Diagram

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

IMyContract1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace MultipleEndpoints

{

[ServiceContract]

publicinterfaceIMyContract1

{

[OperationContract ]

string GetMessagefromIMyContract1();

}

}

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]

publicinterfaceIService1

{

[OperationContract]

string GetMessagefromIService1();

}

}

IMyContract2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

namespace MultipleEndpoints

{

[ServiceContract]

publicinterfaceIMyContract2

{

[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

{

publicclassService1 : IService1, IMyContract1, IMyContract2

{

publicstring GetMessagefromIService1()

{

return“Calling Default Contract IService1″;

}

publicstring GetMessagefromIMyContract1()

{

return“Calling My Contract IMyContract1″;

}

publicstring GetMessagefromIMyContract2()

{

return“Calling My Contract IMyContract2″;

}

}

}

Step4

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

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.

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.

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

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

In this article, I will explain how we could configure multiple binding for WCF service.

To see the video tutorial of hosting WCF Service in Console Application Click Here

While creating multiple endpoints for the service, make sure each end point is having unique address. If address is not unique, you will catch with run time error.

Mathematically, we can say

Scenario for Multiple End Points

1. Service wants to expose more than one type of binding.

2. Service wants to expose more than one contract on the same binding.

3. Service wants to expose same binding and contract on different addresses.

Multiple End Points could be configuring in Web.Config file.

Sample Service with multiple End Points

Step 1

Create a WCF Service Application. Open Visual studio and create new project selecting WCF Service Application project template. In VS2008, you will get WCF Service Application project template inside WEB tab whereas in VS2010, you will get WCF Service Application project template inside WCF tab.

Step 2

Delete the default code getting created for you by WCF. Delete all the code from IService1 and Service1. If you are using VS2008, comment out whole System.ServiceModel from Web.Config file. And if you are using VS2010 then comment out Multiple Host Binding.

Step 3

Contract is as below

Service implementation is

Step 4

To see the video tutorial of hosting WCF Service in Console Application Click Here

Create a console application to host the service.

For this,

1. Right click and add new project to your solution of Console type.

2. Add Reference of System.ServiceModel.

3. Add project reference of WCF Service Application created in Step 1.

4. Make this Console application as your startup project. To make this right click on console application and select make as startup project.

5. Right click on the console application then select add new item and then add new Application Configuration File.

Step 5

Configure the multiple end points here.

1. Add as many end points as you want in service tag.

2. Make sure none of the end points is having same address. Else you will get run time error.

3. Advisable is to use relative address. So for that add base address using Host tag.

So configuration file with multiple end points can look like,

Explanation

1. There are two end points getting exposed.

2. Relative address is being used to expose the end points.

3. Two end points are having their respective names as firstBinding and secondBinding.

Press F5 to run the host (Console) application.

Keep open this console running window. Do not close this window.

Step 6

Create a Console client. To do, open visual studio and create a new console application. Add Service Reference. Copy the base address from app.config of host console application (created in Step 5) and paste as Service URL.

You can see in above code, we are creating proxy twice by passing end point names respectively. If you want, you can pass address of respective endpoint also along with name of the end point.