How to configure a single WCF Service to have multiple HTTP and HTTPS endpoints?

放肆的年华 提交于 2019-11-30 00:48:14

Follow these steps-

1) Write two endpoints for the service, one is for http and another for https.

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

    </service>
  </services>

2) Enable both httpGetEnabled="True" httpsGetEnabled="true" in serviceBehaviors.

<behaviors>

<serviceBehaviors>      
  <behavior name="MyServiceBehavior">
    <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true" />
  </behavior>      
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="WebBehavior">
    <webHttp/>
  </behavior>
</endpointBehaviors>

</behaviors>

3) Write two bindings configurations for http and https. For http give security mode="None" and for https give mode="Transport".

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

Check this link

If you're using Visual Studio 2010 and Web Application Project Deployment you can use the Web.config Transformation Syntax to point your service endpoint's bindingConfiguration to an https enabled binding configuration.

For me, I figured out I only had to replace two elements in the Web.config file. The endpoint's bindingConfiguration attribute and serviceMetadata's httpsGetEnabled should be set to true.

Here's the Web.config, in its default (debug) configuration:

<service name="Service"
            behaviorConfiguration="DefaultBehavior">
    <endpoint name="ServiceEndpoint"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding"
                contract="IService" />
    </service>
    ...
    <behaviors>
    <serviceBehaviors>
        <behavior name="DefaultBehavior">
            <serviceMetadata httpGetEnabled="True" />
            <serviceDebug includeExceptionDetailInFaults="True" />
        </behavior>
    </serviceBehaviors>
</behaviors>

Here's the Web.Release.config transformation file

<behaviors>
    <serviceBehaviors>
        <behavior>
            <serviceMetadata httpsGetEnabled="True" xdt:Transform="Replace" />
            <serviceDebug includeExceptionDetailInFaults="False" xdt:Transform="SetAttributes(includeExceptionDetailInFaults)"/>
        </behavior>
    </serviceBehaviors>
</behaviors>
<services>
    <service>
        <endpoint bindingConfiguration="SecureTransportBinding"
                    xdt:Transform="SetAttributes(bindingConfiguration)"/>
    </service>
</services>

Here's what my bindings look like, but they are pretty standard. notice the names that are used above:

<basicHttpBinding>
    <binding name="SecureTransportBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="Transport"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
    <binding name="BasicHttpBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
        <security mode="None"/>
        <readerQuotas maxStringContentLength="2147483647" maxArrayLength="2147483647"/>
    </binding>
</basicHttpBinding>

Here's a link to more about Web.config transformations:

http://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

There are lots of reasons you can get the error:

    Could not find a base address that matches scheme http for the endpoint
    with   binding WebHttpBinding. Registered base address schemes are [https].

Most of the reasons are from the Web.config settings, but it could be from IIS. I had the same problems, if you defended Endpoints with both http and https bindings, you have to create http and https binding for the website you created in IIS->Site->Bindings, otherwise you will get this error.

This is one reason why you can setup binding from configuration: To be able to use different settings in different environments.

The easiest solution for you is to use makecert to create test certificate for your development environment and use HTTPS on both development and production machine.

Another solution is to create installation package (msi) and let admin change enpoint setting during installation.

Edit:

Your requirement of having all 4 endpoints on both machines is achievable but in that case your service will be also exposed on HTTP in production and everybody who will have WSDL for the service will know about that.

bala

The default receive timeout is 10 minutes, so WCF client will be disconnected after the idle time exceeded that limitation. What can I do if the connection needs to be kept alive?

Solution #1:

Server provides a dummy operation for client calls it regularly to let it not idle.

Solution #2:

Enable reliableSession and set receiveTimeout and inactivityTimeout to “infinite” in both the client and server. The configuration snippet may like the following:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding" receiveTimeout="infinite">
        <reliableSession inactivityTimeout="infinite" enabled="true" />
      </binding>
    </wsHttpBinding>
  </bindings>
  <services>
  ...
  </services>
  ...
</system.serviceModel>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!