Dealing With Multiple EndPoints of a WCF Service

If you understand EndPoints well, you understand WCF well. Configuring EndPoints are vital for any WCF design. You can configure EndPoints either via code or in a web.config file. I have seen many times, developers are confused by EndPoints. I have come across the following common question about EndPoints:

  1. Can we have multiple EndPoints?
  2. Can we expose the same contracts on more than one EndPoint?
  3. How base address works with EndPoints?
  4. Can we have more than one base address?
  5. How to configure EndPoints in code.
  6. Do we need to configure an EndPoint in self-hosting?
  7. Can we have more than one service Contract?

And many more questions like the above.

To answer the above questions, let us start by understanding what EndPoints are.

Endpoints identify a WCF Service. An EndPoint is a combination of Address, Contract and Binding. Mathematically an EndPoint can be defined as below:



Where A, B and C are as follows:



Now it is a simple mathematical addition rule that if we change the value of any of A, B or C then we will get a new E. So for any reason if you change either of Address, Contract or Binding then a new EndPoint will be created. You may have multiple EndPoints of the same service.


  • If the same service is hosted on multiple addresses with the same Binding and the same Contract
  • If the same service with a different Contract on the same Address and for the same Binding
  • If the same service with a different Bindings on the same Address and for the same Contract.

We can say that as a service designer you may consider to create multiple EndPoints in the following scenarios.

  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 Binding Scenario

Now let us see how to expose a service on two different bindings using multiple EndPoints. For two different bindings, we will create two different EndPoints.

Let us say we have a service as below:

  1. public interface IService1  
  2. {  
  3. [OperationContract]  
  4. string message(string name);  
  5.   

And the service is implemented as below:

  1. public class Service1 : IService1  
  2. {  
  3. public string message(string name)  
  4. {  
  5. return "Hello " + name;  
  6. }  


We can configure multiple EndPoints as:

  1. <service name="MessagePatternDemo.Service1">  
  2. <endpoint name="ep1"  
  3. address="/ep1"  
  4. binding="basicHttpBinding"  
  5. contract="MessagePatternDemo.IService1"/>  
  6. <endpoint name="ep2"  
  7. address="/ep2"  
  8. binding="wsHttpBinding"  
  9. contract="MessagePatternDemo.IService1" />  
  10. <endpoint name="mex"  
  11. contract="IMetadataExchange"  
  12. address="mex"  
  13. binding="mexHttpBinding" />  
  14. </service>  

In the above configuration we are creating two EndPoints for the same service. The same service is exposed on different bindings. The client can access the same service, either on basicHttpBinding or wsHttpBinding. Both EndPoints are identified with different names like ep1 and ep2. In multiple EndPoints each EndPoint will be exposed on a different address with different names.

At the client side the same service can be consumed by passing an EndPoint name.

  1. Service1Client proxy1 = new Service1Client("ep1");  
  2. var message = proxy1.message("dj");  
  3. Console.WriteLine(message);  
  4. Console.ReadKey(true); 

We are passing ep1 as the name of the EndPoint, so the client will consume the service using basicHttpBinding. If you do not pass an EndPoint name at the time of proxy creation then the run time error will occur while calling the service.

In the above scenario, we exposed the same service on multiple bindings using two different EndPoints.

Multiple Address Scenario

There could be a scenario when you may want to expose the same service on multiple base addresses. You can have more than one base address in the service. The base address is created under the host as below:



You can add more than one base address using the add in baseAddresses section. The advantage of having a base address is that if we are moving the service from one server to another then only the base address needs to be changed and all EndPoints will work on the updated server. WCF allows us to provide multiple base addresses for each type of protocol. And at run time the corresponding endpoint will use the base address.



So you can expose IService1 on multiple EndPoints with more than one binding as below. We are exposing IService1 with basicHttpBinding and netTcpBinding both. At run time WCF will attach a corresponding base address to respective binding.

  1. <services>  
  2. <service name="MessagePatternDemo.Service1">  
  3. <endpoint name="ep1"  
  4. address="/ep1"  
  5. binding="basicHttpBinding"  
  6. contract="MessagePatternDemo.IService1"/>  
  7. <endpoint name="ep2"  
  8. address="/ep2"  
  9. binding="netTcpBinding"  
  10. contract="MessagePatternDemo.IService1"/>  
  11. <endpoint  
  12. contract="IMetadataExchange"  
  13. address="mex"  
  14. binding="mexHttpBinding" />  
  15. <endpoint address="mex1"  
  16. binding="mexTcpBinding"  
  17. contract="IMetadataExchange"/>  
  18.   
  19. <host>  
  20. <baseAddresses>  
  21. <add baseAddress="http://localhost:8081"/>  
  22. <add baseAddress="net.tcp://localhost:8082"/>  
  23. </baseAddresses>  
  24. </host>  
  25. </service>  
  26.   
  27. </services> 

You may want to host the same service on different servers, in that scenario the service will also have the same contract and binding whereas a different address.

Multiple Contract Scenario

There could be a scenario when you will need to expose multiple Service Contracts. Service Contracts will be exposed on separate EndPoints.



Let us say we have two services IService1 and IService2 as below:

  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4. [OperationContract]  
  5. string messagefromservice1(string name);  
  6.   
  7. }  
  8.   
  9. [ServiceContract]  
  10. public interface IService2  
  11. {  
  12. [OperationContract]  
  13. string messagefromservice2(string name);  
  14.   

And the service is implemented as below:

  1. public class Service1 : IService1, IService2  
  2. {  
  3. public string messagefromservice2(string name)  
  4. {  
  5. return "Hello " + name + " from service1";  
  6. }  
  7.   
  8. public string messagefromservice1(string name)  
  9. {  
  10. return "Hello " + name + " from service2";  
  11. }  

In the config file we have created multiple EndPoints exposing IService1 and IService2 as below:

  1. <services>  
  2. <service name="MessagePatternDemo.Service1">  
  3.   
  4. <endpoint name="ep1"  
  5. address="/ep1"  
  6. binding="basicHttpBinding"  
  7. contract="MessagePatternDemo.IService1"/>  
  8.   
  9. <endpoint name="ep2"  
  10. address="/ep2"  
  11. binding="basicHttpBinding"  
  12. contract="MessagePatternDemo.IService2"/>  
  13.   
  14. <endpoint  
  15. contract="IMetadataExchange"  
  16. address="mex"  
  17. binding="mexHttpBinding" />  
  18.   
  19.   
  20. <host>  
  21. <baseAddresses>  
  22. <add baseAddress="http://localhost:8081"/>  
  23. </baseAddresses>  
  24. </host>  
  25. </service>  
  26.   
  27. </services> 

At the client side while consuming a service you will get information that there are two service contracts as part of the service. As you see in the Add Service Reference dialog, there are two services listed and they are IService1 and IService2



To use these service you will need to create separate proxies. Both services can be consumed at the client as below:

  1. Service1Client proxy1 = new Service1Client();  
  2. var message = proxy1.messagefromservice1("dj");  
  3. Console.WriteLine(message);  
  4.   
  5. Service2Client proxy2 = new Service2Client();  
  6. var message1 = proxy2.messagefromservice2("dj");  
  7. Console.WriteLine(message1);  
  8.   
  9. Console.ReadKey(true); 

On running you will get output as below:



Having multiple EndPoints for services are sometimes essential for a good service design. You may want to expose the same service on different EndPoints for Internet users whereas for an Intranet user on a separate EndPoint. I hope this article is useful for you.

Up Next
    Ebook Download
    View all
    Learn
    View all