Tuesday, January 6, 2015

Creating WCF service with multiple endpoint (one service, two endpoint)

Requirement - To have a single service, exposing two endpoints (basicHttpBinding, wsHttpBinding).

The web.config of the service needs to be updated with two endpoints. This web.config can be updated with "WCF configuration Editor" tool.

Here is the sample configuration -


<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding1">
          <readerQuotas maxDepth="99999" maxStringContentLength="99999"
            maxArrayLength="99999" maxBytesPerRead="99999" maxNameTableCharCount="99999" />
        </binding>
      </basicHttpBinding>
      <wsHttpBinding>
        <binding name="wsHttpBinding1">
          <readerQuotas maxDepth="99999" maxStringContentLength="99999"
            maxArrayLength="99999" maxBytesPerRead="99999" maxNameTableCharCount="99999" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="TestWCFServiceWSBinding.Service1">
        <endpoint address="/epWs" binding="wsHttpBinding"
          bindingConfiguration="wsHttpBinding1" name="EP_WsHttp" contract="TestWCFServiceWSBinding.IService1" />
        <endpoint address="/epBasic" binding="basicHttpBinding"
          bindingConfiguration="basicHttpBinding1" name="EP_BasicHttp"
          contract="TestWCFServiceWSBinding.IService1" />
        <host>
          <baseAddresses>
            <add baseAddress="http://MikePC:9650/Service1.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>




The key here is have a base address present and different relative address for both the endpoints (marked in red). While consuming this service, the client must mention which endpoint its referring by -

 ServiceReference1.Service1Client cl = new ServiceReference1.Service1Client("EP_WsHttp");
            var x = cl.GetData(5);

 ServiceReference1.Service1Client cl2 = new ServiceReference1.Service1Client("EP_BasicHttp");
            var y = cl2.GetData(5);


Once it is implmented, verify in Fiddler - Traffic for basicHttpBinding call will be in plain text while for wsHttpBinding, it will be encrypted.

No comments:

Post a Comment