Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-hosted WCF service works with HTTP not with HTTPS

Tags:

c#

wcf

Ok so I am hosting a WCF service within a console application.

all bindings are created programatically, so no config settings.

I have a working service as long as I use HttpTransportBindingElement however as soon as I use HttpsTransportBindingElement then nothing works, the service does not display within the browser and the client application returns a 405 (Method Not Allowed) CommunicationException

I have tried setting a SecurityBindingElement to my CustomBinding but I am not sure which option I should be using.

SecurityBindingElement.CreateCertificateOverTransportBindingElement()

SecurityBindingElement.CreateAnonymousForCertificateBindingElement()

etc.

The code for the creation of the host is below

baseAddress = new Uri(string.Format("{0}://{1}", strConnectionType, ConfigurationManager.AppSettings["baseAddress"]));

            ServiceHost host = new ServiceHost(typeof(IMyService), baseAddress);

            host.AddServiceEndpoint(typeof(MyService), binding, String.Empty);

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpsGetEnabled = certificate != null;
            smb.HttpGetEnabled = certificate == null;

            host.Description.Behaviors.Add(smb);

            ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();

            if (sdb == null)
            {
                host.Description.Behaviors.Add(new ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
            }
            else
            {
                if (!sdb.IncludeExceptionDetailInFaults)
                {
                    sdb.IncludeExceptionDetailInFaults = true;
                }
            }


            if (certificate != null)
            {
                host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, StoreName.My, X509FindType.FindByThumbprint, certificate.Thumbprint);
            }
like image 595
Secret Squirrel Avatar asked Aug 05 '13 12:08

Secret Squirrel


1 Answers

I followed this blog http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx which highlighted that in order for HTTPS to work you need to bind the port to the certificate you are using.

Process bindPortToCertificate = new Process(); bindPortToCertificate.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "netsh.exe");

bindPortToCertificate.StartInfo.Arguments = string.Format("http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={{{2}}}", port, certificate.Thumbprint, Guid.NewGuid());

bindPortToCertificate.Start();

bindPortToCertificate.WaitForExit();

once this was done it all worked.

contact me if any requires my example code of setting up and configuring a self-hosted WCF server with bindings programatically set. :)

like image 108
Secret Squirrel Avatar answered Sep 28 '22 05:09

Secret Squirrel