WCF standalone service does not work with HTTP with HTTPS

Ok, so I host the WCF service in a console application.

all bindings are created programmatically, so no configuration settings.

I have a working service, if I use HttpTransportBindingElement , but as soon as I use HttpsTransportBindingElement then nothing works, the service does not appear in the browser, and the client application returns 405 (Method Not Allowed) CommunicationException

I tried setting SecurityBindingElement to my CustomBinding , but I'm not sure which option I should use.

SecurityBindingElement.CreateCertificateOverTransportBindingElement()

SecurityBindingElement.CreateAnonymousForCertificateBindingElement()

and etc.

Code for creating a host 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); } 
+7
c # wcf
source share
1 answer

I followed this blog http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx , which emphasized that for HTTPS you you must 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 my sample setup code is required for setting up a self-service WCF server with a software binding set. :)

+6
source

All Articles