8
votes

I'm trying to get a minimal WCF/https example working, but I can't access the service (surprisingly the code runs without errors).

  • I have created a root certificate:

    makecert -n "CN=Test" -r -sv Test.pvk TestCert.cer

  • I have added it to the Windows-Root certificates.
  • I have created a server certificate:

    makecert -sk TestCom -iv Test.pvk -n "CN=TestCom" -ic TestCert.cer -sr LocalMachine -ss my -sky exchange -pe

  • I have reserved a port for the SSL url:

    netsh http add urlacl url=https://+:15001/ user=Everyone

  • I have set the certificate hash to the port:

    netsh http add sslcert ipport=0.0.0.0:15001 certhash=XXX appid={76d71921-b40d-4bc8-8fcc-4315b595878e}

  • I have added TestCom to the etc/hosts file

Now I have created a simple C# project:

namespace ConsoleApplication7
{
    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        string SimpleMethod(string msg);
    }

    class SimpleService : ISimpleService
    {
        public string SimpleMethod(string msg)
        {
            return "Hello " + msg;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(SimpleService));
            try
            {
                host.Open();
                Console.ReadLine();
                host.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
}

with the following app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.web>
      <compilation debug="true"/>
    </system.web>
    <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviors_HTTPSServiceHost" >
          <serviceMetadata httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <wsHttpBinding>
        <binding name="BasicHttpBinding_HTTPSServiceHost">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service name="ConsoleApplication7.SimpleService" behaviorConfiguration="ServiceBehaviors_HTTPSServiceHost">
        <endpoint address=""  binding="wsHttpBinding" bindingConfiguration="BasicHttpBinding_HTTPSServiceHost"
                  contract="ConsoleApplication7.ISimpleService">
          <identity>
            <dns value="TestCom"/>
          </identity>
        </endpoint>
        <endpoint name="mex" address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="https://TestCom:51001/SimpleService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

</configuration>
2
Do the user "Jeder" have read rights to the certificates private key? - Huusom
Sorry, "Jeder" == "Everyone" - Harald
Here you are only hosting the service. You also need a client to call the service methods. - Kryptos
This is clear, if I try to open TestCom:51001/SimpleService in the browser nothing happens, if I try to open the URL with the WcfTestClient.exe it says can't access metadata ... - Harald

2 Answers

1
votes

I can't comment on this account so please open a dialogue below if this isn't the answer and can help narrow it down. Cross reference your set up with what I wrote here and you might spot something in your set up that I did not.

Only concern that i can see initially is

I have added it to the Windows-Root certificates.

A specified logon session does not exist. It may already have been terminated.

If this is the error, just check you imported the cert to the correct location.

Importing an Existing Certificate

  • Run mmc.exe.
  • Go to File-> Add/Remove Snap-In
  • Choose the Certificates snap-in.
  • Select Computer Account
  • Navigate to: Certificates (Local Computer)\Personal\Certificates
  • Right click the Certificates folder and choose All Tasks -> Import.
  • Follow the wizard instructions to select the certificate.

I have hit every issue possible with wsHttpBinding and have a personal wiki, I can help you better if there are any logs, where is it crashing at currently?

0
votes

Guess you need to include the policyVersion to make it run like below

<serviceMetadata httpGetEnabled="True"  policyVersion="Policy12"></serviceMetadata>