Search code examples
c#.netwcfwcf-bindingwcf-security

HTTP & HTTP WCF Services


Can I have a WCF Service Project that has both HTTP (basic http binding) and HTTPS (basic http binding) bindings? For example, I would have:

https://localhost:44303/ServiceA.svc http://localhost:12345/ServiceB.svc

Would there be any benefit to putting them into separate service projects (and separate sites when we deploy the app)?


Solution

  • If you already have HTTP binding, you don't need to change code to add HTTPS binding. This is a big advantage of WCF. Instead of adding a separate site, you just add a new endpoint to the configuration file.

    Below is an example of configuration with both HTTP and HTTPS.
    You can see that there two named bindings: notSecureBinding and secureBinding, which correspond to HTTP and HTTPS.

      <bindings>
        <basicHttpBinding>
          <binding name="notSecureBinding"
                   maxBufferSize="2147483647"
                   maxReceivedMessageSize="2147483647">
            <security mode="None"/>
          </binding>
          <binding name="secureBinding"
                   maxBufferSize="2147483647"
                   maxReceivedMessageSize="2147483647">
            <security mode="Transport">
              <transport clientCredentialType="None"/>
            </security>
          </binding>
        </basicHttpBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name="StandardServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
            <serviceDebug includeExceptionDetailInFaults="true"/>
            <serviceAuthorization principalPermissionMode="None"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      <services>
        <service behaviorConfiguration="StandardServiceBehavior"
                 name="ServiceName">
          <endpoint address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="notSecureBinding"
                    contract="Namespace.IService"/>
          <endpoint address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="secureBinding"
                    contract="Namespace.IService"/>
          <endpoint address="mex"
                    binding="mexHttpBinding"
                    contract="IMetadataExchange"/>
        </service>
      </services>