3

I am unable to connect to my self-hosted WCF service running with WebHttp+HTTPS bindings. For various reasons, I configure the service entirely in code rather than using a config file, and I instantiate the service this way:

private ServiceHost CreateService()
{
    Type myServiceType = typeof(MyService);
    ServiceHost myService = new ServiceHost(myServiceType, new Uri(Constants.ServiceAddress));
    ContractDescription contract = ContractDescription.GetContract(myServiceType);

    WebHttpBinding httpsBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
    httpsBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

    ServiceEndpoint endpoint = myService.AddServiceEndpoint(myServiceType, httpsBinding, "MyService.svc");
    endpoint.Behaviors.Add(new WebHttpBehavior());

    ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
    metadataBehavior.HttpGetEnabled = true;
    metadataBehavior.HttpsGetEnabled = true;
    myService.Description.Behaviors.Add(metadataBehavior);

    myService.Credentials.ServiceCertificate.SetCertificate(
        StoreLocation.LocalMachine, 
        StoreName.My, 
        X509FindType.FindByThumbprint, 
        Constants.CertThumbprint);

    return myService;
}

When I run this code, the service is instantiated and started without error. The service claims to be open when I query it in code, and netstat shows that someone is listening on the appropriate port. I have a firewall exception which allows incoming connections on this port.

However, if I try to open the service endpoint address in the browser or the client, the connection instantly fails. Any clue why? Is there any configuration of the service host or the environment that I have forgotten?

EDIT:

There is no error message to report---no 404, 500, or other error. The browser behaves as if it is unable to open a connection to the target port. The server doesn't seem to even see the incoming connection.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169

1 Answers1

3

The problem turned out do be that I hadn't registered an SSL cert for my port. The following lines of code are non-functional:

myService.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine, 
    StoreName.My, 
    X509FindType.FindByThumbprint, 
    Constants.CertThumbprint);

Setting the service credentials is only relevant when you intend to use certificate authentication for clients. If you're using HTTPS, then you need to register an SSL cert for the port you're listening on. Issuing the following command resolves the issue:

netsh http add sslcert ipport=0.0.0.0:443 certhash=0b740a29f29f2cc795bf4f8730b83f303f26a6d5 appid={00112233-4455-6677-8899-AABBCCDDEEFF}

There is also an unmanaged interface for doing this, but no managed wrapper exists, so it's simplest to do this using the netsh program.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169