Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimal example of WCF using HTTPS

Tags:

c#

wcf

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>
like image 225
Harald Avatar asked May 19 '15 13:05

Harald


People also ask

What is WCF in .NET with example?

Windows Communication Foundation (WCF) is a framework for building service-oriented applications. Using WCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be part of a continuously available service hosted by IIS, or it can be a service hosted in an application.


1 Answers

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?

like image 137
TeamAliasName Avatar answered Oct 05 '22 21:10

TeamAliasName