Configure Endpoints in WCF using config File


Endpoint is the combination of Address, Binding, and Contract in WCF. Every WCF service must associate with an Address, Binding, and Contract. Address is nothing but where it defines, Binding means how client can communicates with the service, and Contract means what WCF service do. The Endpoint graphical representation is as shown below.

                                                           

Every endpoint should have Address, Binding, and Contract; without any of these elements it should not called as endpoint.

Client interacts with WCF service through endpoint only that means endpoint is the interface for the WCF service. Every WCF service should contain minimum of one endpoint and maximum of any number of endpoints. All endpoints should have different Address, but it can contain same or different Binding and Contracts.

We can create Endpoint programmatically or we can place Endpoints in Config file, in this article we discuss about how to configure WCF Endpoints by using Config file.

Open Microsoft Visual Studio 2013 => Create WCF Service Application

Add Endpoint to Config file as shown below.

  <system.serviceModel>

    <services>

      <service name="WcfService1.Service1">

        <endpoint address="http://localhost:81/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="http://localhost:82/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="http://localhost:83/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="http://localhost:84/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

      </service>

    </services>   

  </system.serviceModel>

As shown above we added three Endpoints which have same binding and contract but differs in address. We can have different bindings also as shown below.

  <system.serviceModel>

    <services>

      <service name="WcfService1.Service1">

        <endpoint address="http://localhost:81/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="net.tcp//localhost:82/Service1" binding="netTcpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="https://localhost:83/Service1" binding="webHttpBinding" contract="WcfService1.IService1"></endpoint>

      </service>

    </services>   

  </system.serviceModel>

Here we have to remember one thing is Binding and Address should be match. That means binding type and Address protocol should match, for example if we are using netTcpBinding binding then the address should be with net.tcp/protocol. We can have same base address for all Endpoints, but full address has to be differ.

We can use the config file to customize the Binding through bindingConfiguration property of endpoint element as shown below.

<system.serviceModel>

    <services>

      <service name="WcfService1.Service1">

        <endpoint address="http://localhost:81/Service1" bindingConfiguration="CustomHTTPBinding" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="net.tcp//localhost:82/Service1" binding="netTcpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="https://localhost:83/Service1" binding="webHttpBinding" contract="WcfService1.IService1"></endpoint>

      </service>

    </services>

    <bindings>

      <wsHttpBinding>

        <binding name="CustomHTTPBinding" transactionFlow="true"></binding>

      </wsHttpBinding>

    </bindings>

  </system.serviceModel>

As shown above for the first endpoint we are using custom binding with the name CustomHTTPBinding and it defined under <bindings> element.

We can have default binding configuration to apply for all endpoints without mentioning bindingConfiguration name as shown below.

  <system.serviceModel>

    <services>

      <service name="WcfService1.Service1">

        <endpoint address="http://localhost:81/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="http//localhost:82/Service1" binding="wsHttpBinding" contract="WcfService1.IService1"></endpoint>

 

        <endpoint address="https://localhost:83/Service1" binding="webHttpBinding" contract="WcfService1.IService1"></endpoint>

      </service>

    </services>

    <bindings>

      <wsHttpBinding>

        <binding transactionFlow="true"></binding>

      </wsHttpBinding>

    </bindings>

  </system.serviceModel>

Only problem with default binding is when it defines with the named bindings, it may become difficult for human to understand.