Monday, 7 January 2013

Host WCF on both HTTP & HTTPS

Hi,
We have created a WCF service for one of our project a while ago. Yesterday, for some reasons, the system administrator enabled the SSL on IIS. And our WCF service stopped working with the exception -

Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.


As usual, my suspect was the UriTemplate initially. I cross checked my service but everything was fine. When i gooogled this error, the symptoms(i.e. using UriTemplate with WebGet and enableWebScript tag in config file) for the error were not there in service.
So i concluded that the culprit must be the SSL. I requested them to disable the SSL for a moment and Viola.. It started working again perfectly. so my focus shifted from code error to binding with SSL. I came through this post . Just for the sake of my own records and quick reference, pasted the code here as well, as  posted by  

<system.serviceModel>  
  <services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">      
      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />      
    </service>
  </services>    
  <behaviors>    
    <serviceBehaviors>      
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>      
    </serviceBehaviors>    
    <endpointBehaviors>
      <behavior name="WebBehavior">
        <webHttp/>
      </behavior>
    </endpointBehaviors>    
  </behaviors>  
  <bindings>
    <webHttpBinding>
      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>
      <binding name="webBindingHTTPS">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>
    </webHttpBinding>
  </bindings>  
</system.serviceModel>

Apply the respective changes to your config accordingly and it works like charm. A big thanks to JayaKrishna.

Thanks
S. Shafique
Fokat RnD Team Member

2 comments: