简体   繁体   中英

Self-hosted WCF service works with HTTP not with HTTPS

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);
            }

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM