Configuring Multiple End points for WCF Service

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

i1

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.

i3

Mathematically, we can say

i2

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.

i4

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

clip_image007

Service implementation is

clip_image009

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.

clip_image011

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,

clip_image013

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.

clip_image015

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.

clip_image017

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.

clip_image019

When you press F5, you will get below output.

clip_image021

For your Reference full source code is given below,

WCF Service Application

Contract (IService1.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

{

[ServiceContract]

public interface IService1

{

[OperationContract]

string GreetingMessage(string Name);

}

}

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

{

public string GreetingMessage(string name)

{

return “You are Called From “ + name;

}

}

}

Hosting Console Application

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.ServiceModel;

using MultipleEndpoints;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

ServiceHost host = new ServiceHost(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

<?xml version=1.0 encoding=utf-8 ?>

<configuration>

<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 =/MyFirstBindingAddress

binding =basicHttpBinding

contract =MultipleEndpoints.IService1 />

<endpoint name=secondBinding

address =/MySecondBindingAddress

binding =basicHttpBinding

contract =MultipleEndpoints.IService1/>

<endpoint contract=IMetadataExchange

binding=mexHttpBinding

address=mex />

<host>

<baseAddresses >

<add baseAddress =http://localhost:8181/Service1.svc/>

</baseAddresses>

</host>

</service >

</services>

</system.serviceModel>

</configuration>

Client Console application

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using ConsoleApplication2.ServiceReference1;

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

Service1Client proxy1 = null;

proxy1 = new Service1Client(“firstBinding”);

Console.WriteLine(proxy1.GreetingMessage(“First End Point”));

proxy1 = new Service1Client(“secondBinding”);

Console.WriteLine(proxy1.GreetingMessage(“Second End Point”));

Service1Client proxy = null;

proxy = new Service1Client(“firstBinding”,

http://localhost:8181/Service1.svc/MyFirstBindingAddress&#8221;);

Console.WriteLine(proxy.GreetingMessage(“First End Point calling with Address”));

proxy = new Service1Client(“secondBinding”,

http://localhost:8181/Service1.svc/MySecondBindingAddress&#8221;);

Console.WriteLine(proxy.GreetingMessage(“Second End Point Calling with Address”));

Console.ReadLine();

}

}

}

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

8 responses to “Configuring Multiple End points for WCF Service”

  1. Hi,
    My problem is to have multiple instances of a singleton pair contract / object instantiated in one single windows service.
    Currently when I change shared data in a http://localhost:8090/singleton also has repercussions on all parallel instances http://localhost:809n/singleton.
    It is possible hosting many isolated singleton in a single windows service with the same contract / class across multiple endpoints keeping data separate between the n instances?
    Also AppDomain not solve the problem…

    MAIN :

    For i As Integer = 0 To 7
    hdom(i) = AppDomain.CreateDomain(“HostDomain” & i.ToString)
    hdom(i).DoCallBack(AddressOf HostDomain)
    Next

    Private Sub HostDomain()
    Dim client As New C4PlatinumWcfWS.PlatinumService
    Dim Host As ServiceHost = Nothing
    count += 1
    Host = New ServiceHost(client, New Uri (“http://localhost:907” & _
    count.ToString(“0”) & _
    “/Singleton”))

    Host.AddServiceEndpoint(GetType(C4PlatinumWcfWS.IPlatinumService), _
    New BasicHttpBinding(BasicHttpSecurityMode.None), “”)
    Dim ar As IAsyncResult = Host.BeginOpen(Nothing, Nothing)
    ar.AsyncWaitHandle.WaitOne()
    Host.EndOpen(ar)
    End Sub

    Thanks.

  2. Hello Dhananjay,
    excellent article! helped me a lot!!
    Thanks again.
    I am having a doubt if you can help me. Can you help me to know in what scenario I will need to have multiple endpoints with different address with having same binding mechanism and contract.
    Thanks again,.

  3. I’m trying to setup endpoints with completely different endpoint addresses. they do not use the same host. When I try to preview the service, I get “no protocol matches the given address….” Do you have any suggestions?

  4. Hi ,

    Could you explain me more what you are trying to achieve ?

    What I understand is that ,

    1. you have different end points with different address ?

  5. it is gooooood

  6. Great post.
    I had to make to some changes to get this up and running.

    Here are the changes that I had to do.
    WCF Side
    ————-
    Web.config
    ————–

    Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in
    machine.config.comments usually located in
    \Windows\Microsoft.Net\Framework\v2.x\Config
    –>

    <!—->

    <!—->
    <!—->
    <!—->
    <!—->

    <!–
    The section enables configuration
    of the security authentication mode used by
    ASP.NET to identify an incoming user.
    –>

    <!–
    The section enables configuration
    of what to do if/when an unhandled error occurs
    during the execution of a request. Specifically,
    it enables developers to configure html error pages
    to be displayed in place of a error stack trace.

    –>

    <!–
    Uncomment this section to enable the authentication service. Include
    requireSSL="true" if appropriate.

    –>
    <!–
    Uncomment these lines to enable the profile service, and to choose the
    profile properties that can be retrieved and modified in ASP.NET AJAX
    applications.

    –>
    <!–
    Uncomment this section to enable the role service.

    –>

    <!–

    –>

    Client Console application
    ———————————-
    App.Config
    —————

    Hosting Console Application
    ————————————-
    App.Config
    —————

    Thanks,
    Nandha

  7. Sapan Kumar Singh

    Hello Sir, Your article is very good and I learned. I have some query..
    Suppose..

    We have a two Interface.

    IA and IB

    One Servcie Class

    Service:IA,IB
    {

    }

    Now My end Point is

    First End Point To expose Service Contract IA

    Now I want To Expose Servcie Contarct IB on Same Port.

    Sir My Question is ..

    1.We can expose more Contract on a Same Port?. Means If we expose more than one contarct on a single Port then It will be Two Point?

    2. If i want to Change Servcie Class then it is possible on same port.

    3. If I want to expose same contarct with two differnet Bindings the it will be expose on same port or different port?

    Please Reply..

    Thanks

  8. Dhananjay Kumar,

    Nice article.
    I have one question.
    My code is working with multiple endpoints perfect.
    If any changes in the contract, every time I am generating unique endpoints dynamically and exporting. it is working fine.
    The issue is, client can able to access the service with both old and new bindings
    Once new endpoints exposed, the old endpoints should not work.
    Please help me hot to delete already exported endpoint

    Thanks
    Raam

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com