Reader Level:
ARTICLE

Configuring Multiple End Points for WCF Service

Posted by Dhananjay Kumar Articles | WCF with C# May 25, 2010
In this article, I will explain how we could configure multiple binding for WCF service.
  • 0
  • 0
  • 55349

HTML clipboard

Note: Same Article from Author is published on his personal blog also. You can find article here

Objective

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

image1.gif


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.

image2.gif

Mathematically, we can say

image3.gif

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.

image4.gif

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

image5.gif

Service implementation is

image6.gif

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.

    image7.gif
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,

image8.gif

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.

image9.gif

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.

image10.gif


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.

image11.gif

When you press F5, you will get below output.

image12.gif

For your Reference full source code is given below,

WCF Service Application

Contract (IService1.cs)

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

    [ServiceContract]
publicinterfaceIService1
    {
 
        [OperationContract]
stringGreetingMessage(string Name);
    }

}

Service1.svc.cs

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

namespaceMultipleEndpoints
{

publicclassService1 : IService1
    {
publicstringGreetingMessage(string name)
        {
return"You are Called From " + name;
        }
 
    }
}

Hosting Console Application

Program.cs

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

namespace ConsoleApplication1
{
classProgram
    {
staticvoid Main(string[] args)
        {

ServiceHost host = newServiceHost(typeof(Service1));
host.Open();
Console.WriteLine("Service is up and running");
Console.WriteLine("To Close Service Press any Key ");
Console.ReadKey();
host.Close();
 
        }
    }
}

App.Config

<?xmlversion="1.0"encoding="utf-8" ?>
<configuration>
<
system.serviceModel>
<
behaviors>
<
serviceBehaviors>
<
behaviorname ="Mg">
<serviceMetadatahttpGetEnabled="true"/>
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
</
serviceBehaviors>
</
behaviors>
<
services>
<
servicename ="MultipleEndpoints.Service1"
behaviorConfiguration ="Mg">
<endpointname="firstBinding"
address ="/MyFirstBindingAddress"
binding ="basicHttpBinding"
contract ="MultipleEndpoints.IService1" />
<endpointname="secondBinding"
address ="/MySecondBindingAddress"
binding ="basicHttpBinding"
contract ="MultipleEndpoints.IService1"/>
<endpointcontract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
<host>
<
baseAddresses>
<
addbaseAddress ="http://localhost:8181/Service1.svc"/>
</baseAddresses>
</
host>
</
service>
</
services>
</
system.serviceModel>
</
configuration>

Client Console application

Program.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
using ConsoleApplication2.ServiceReference1;

namespace ConsoleApplication2
{
classProgram
    {
staticvoid Main(string[] args)
        {
Service1Client proxy1 = null;
            proxy1 = newService1Client("firstBinding");
Console.WriteLine(proxy1.GreetingMessage("First End Point"));
            proxy1 = newService1Client("secondBinding");
Console.WriteLine(proxy1.GreetingMessage("Second End Point"));          

Service1Client proxy = null;
proxy = newService1Client("firstBinding",
"http://localhost:8181/Service1.svc/MyFirstBindingAddress");
Console.WriteLine(proxy.GreetingMessage("First End Point calling with Address"));
proxy = newService1Client("secondBinding",
"http://localhost:8181/Service1.svc/MySecondBindingAddress");
Console.WriteLine(proxy.GreetingMessage("Second End Point Calling with Address"));
Console.ReadLine();
 
        }
    }
}

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

Article Extensions
Contents added by vamsidhar on May 06, 2011
Hello, Thats really nice. But let whic generating proxy class i want to generate proxy for only one contract. How can we do that?
COMMENT USING

Trending up