Failed to connect to offline WCF service over HTTPS

I am unable to connect to my self served WCF service running with WebHttp + HTTPS bindings. For various reasons, I configure the service entirely in code, rather than using a config file, and create 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 created and started without error. The service states that it is open when I request it in code, but it netstat

shows that someone is listening on the corresponding port. I have a firewall exception that allows incoming connections on this port.

However, if I try to open the service endpoint address in the browser or client, the connection will fail instantly. Why? Is there any service or environment host configuration I forgot?

EDIT

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

+3


source to share


1 answer


The problem turned out to be that I did not have an SSL certificate registered for my port . The following lines of code are non-functional:

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

      

Configuring service credentials only makes sense if you're going to use certificate authentication for clients. If you are using HTTPS, you need to register an SSL certificate for the port you are listening to. Issuing the following command fixes the problem:



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 this, but there is no managed wrapper, so the easiest way to do this is with a program netsh

.

+2


source







All Articles