What is Endpoint, Endpoints in WCF, C#

 

WCF service is combination of Address, Binding and Contract. Address defines where the service hosted, Binding defines how can to communicate with service and finally Contract defines what the WCF service does. By considering all these we can easily remember as ABC of the service. In WCF, Endpoint is nothing but combination of all these three elements Address, Binding and Contract.(ABC of the service). 

Every Endpoint should have all these three elements and service host eposes the endpoints to client. Every WCF service can have at least one endpoint and One WCF service can have any number of end points, but Address should be different for each endpoint. Single WCF service, all endpoints can have same or different binding and can expose same or different contract, but should be differ in address. 

You can create WCF endpoints programmatically or we can configure endpoints in configuration files. Here we discuss about both options. 

Configure Endpoint in Configuration file 

Create simple WCF service application SampleWCFService to know how to configure endpoint through configuration file.

After creating the WCF application you can find default endpoints in the Web.config file under <system.serviceModel> element as shown below.

 

<system.serviceModel> 

    <services> 

      <service name="SampleWCFService.Service1" behaviorConfiguration="SampleWCFService.Service1Behavior"> 

        <!-- Service Endpoints -->

 

        <endpoint address="" binding="wsHttpBinding" contract="SampleWCFService.IService1">

 

          <!-- 

              Upon deployment, the following identity element should be removed or replaced to reflect the  

              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity  

              automatically. 

          --> 

          <identity> 

             <dns value="localhost"/> 

          </identity>

 

        </endpoint>

 

        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

 

      </service>

 

    </services>

 

    <behaviors> 

     ......... 

     ......... 

    </behaviors>

 

  </system.serviceModel>

 

As shown above we have two endpoints with wsHttpBinding binding and mexHttpBinding binding. wsHttpBinding binding endpoint does not have any address, so it will take default host address. 

While  specifying the service name for <services...> element and contract name for endpoint, you have to provide full name that means you have to include namespace also. 

You can include multiple endpoints for your WCF service as shown below under <services> element.

 

<system.serviceModel> 

    <services>

      <service name="SampleWCFService.Service1" behaviorConfiguration="SampleWCFService.Service1Behavior">

 

        <endpoint address="http://localhost/SampleWCFService" binding="wsHttpBinding" contract="SampleWCFService.IService1" /> 

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

 

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

        <endpoint address="net.tcp://localhost:81/SampleWCFService" binding="netTcpBinding" contract="SampleWCFService.IService1" />       

 

      </service> 

    </services> 

    <behaviors> 

     ......... 

     ......... 

    </behaviors>

 

  </system.serviceModel>

 

As shown above, we have four endpoints for our WCF service. First two points have same binding and Contract but differ in base address. Second twp endpoints have binding netTcpBinding and again differ in base address.

 

Programmatically Create WCF Endpoints 

We can also create the WCF Endpoints programmatically. By using this method also we can create multiple Endpoints for single WCF service, but rules applies as same( all endpoints should be differ in address). Here we adds the endpoints to the WCF Service instance programmatically instead of configuring in Configuration file. 

We have to include System.ServiceModel.Channels namespace to add WCF endpoints programmatically as shown below.

 

           ServiceHost host = new ServiceHost(typeof(SampleWCFService.Service1));

 

           Binding wsBinding = new WSHttpBinding(); 

           Binding tcpBinding = new NetTcpBinding(); 

 

            host.AddServiceEndpoint(typeof(SampleWCFService.IService1), wsBinding, http://localhost/SampleWCFService); 

 

            host.AddServiceEndpoint(typeof(SampleWCFService.IService1), wsBinding, http://localhost:81/SampleWCFService);

 

            host.AddServiceEndpoint(typeof(SampleWCFService.IService1), tcpBinding, "net.tcp://localhost/SampleWCFService"); 

            host.AddServiceEndpoint(typeof(SampleWCFService.IService1), tcpBinding, "net.tcp://localhost:81/SampleWCFService");

 

As shown above, we created the instance for our WCF service through ServiceHost class and by using AddServiceEndpoint() method of ServiceHost class we added the end point. For predefined bindings we can use predefined class to create the binding(for eg: WSHttpBinding, NetTcpBinding....). As you observed here also we created four endpoints for WCF service but differ in address and we included the namespace SampleWCFService for service name and contract fields. Through programmatically creating endpoints method, we can omit the namespace if we add the endpoints within the namespace.

Add comment