1
votes

I have a windows service that hosts my wcf service.

The app.config is:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="RestWCFServiceLibrary.Service1Behavior" name="RestWCFServiceLibrary.RestWCFServiceLibrary">
        <endpoint address="" binding="webHttpBinding" contract="RestWCFServiceLibrary.IRestWCFServiceLibrary" behaviorConfiguration="web">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8888/RestWCFServiceLibrary/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestWCFServiceLibrary.Service1Behavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        <CorsSupport/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  <extensions>
            <behaviorExtensions>
                <add name="CorsSupport" type="WebHttpCors.CorsSupportBehaviorElement, WebHttpCors, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
            </behaviorExtensions>
        </extensions>
  </system.serviceModel>

</configuration>

My problem is that if I have my website using https:// it fails to make the http call because of CORS. The https website makes an ajax GET request to the localhost.

Now I am trying to change my windows service to https but everywhere I see some command line ssl bindings. Is there a different way I can change my wcf self hosted windows service to use https?

What do I need to do in order to get this http service migrated over to https.

Please provide example of what needs to be modified in my app.config.

3

3 Answers

1
votes

I stumbled upon similar problem so what i did was used WCF Configuration tool to write a App.Config for me and inside endpoints I selected mexhttpsbinding and yaa the https binding worked..

0
votes

Let's just add an https endpoint. The following configuration works properly over both http and https.

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="mybehavior" name="WcfService1.Service1">
        <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev"></endpoint>
        <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webbev" bindingConfiguration="myhttpsbinding"></endpoint>
        <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:11010"/>
            <add baseAddress="https://localhost:11011"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="myhttpsbinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" sendTimeout="00:10:00" receiveTimeout="00:10:00">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" />
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mybehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webbev">
          <webHttp />
        </behavior>
      </endpointBehaviors>
</behaviors>

Since https protocol is protected by the certificate, we are supposed to bind the certificate to the https port for https endpoint. (we could specify the certificate in IIS binding module instead of CMD if hosting the service in IIS)

netsh http add sslcert ipport=0.0.0.0:11011 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}

Execute CMD with administrator privileges and ensure that the certificate is installed on the local machine certificate store (certlm.msc). Certhash parameter specifies the thumbprint of the certificate. The appid parameter is a GUID that can be used to identify the owning application(located in the project.csproj file)

<ProjectGuid>{56FDE5B9-3821-49DB-82D3-9DCE376D950A}</ProjectGuid>

https://docs.microsoft.com/en-us/windows/desktop/http/add-sslcert
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-configure-a-port-with-an-ssl-certificate

Feel free to contact me if there is anything I can help with.

-3
votes

Https only works on port 443. so you better have your virtual host created in your SSL script tag in your server configuration. Or you can also proxy pass your request http:// port(8888) to https:// (port:443)