【问题标题】:Self-hosted WCF service works with HTTP not with HTTPS自托管 WCF 服务适用于 HTTP 而不是 HTTPS
【发布时间】:2013-08-05 12:43:18
【问题描述】:

好的,我在控制台应用程序中托管 WCF 服务。

所有绑定都是以编程方式创建的,因此没有配置设置。

只要我使用HttpTransportBindingElement,我就有一个可用的服务,但是一旦我使用HttpsTransportBindingElement,就没有任何效果,该服务不会显示在浏览器中,并且客户端应用程序会返回一个405 (Method Not Allowed) CommunicationException

我尝试将SecurityBindingElement 设置为我的CustomBinding,但我不确定应该使用哪个选项。

SecurityBindingElement.CreateCertificateOverTransportBindingElement()

SecurityBindingElement.CreateAnonymousForCertificateBindingElement()

等等

创建主机的代码如下

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

【问题讨论】:

  • 什么是binding? WCF 服务的编程配置也太难了,为什么不使用配置文件呢?
  • @ta.speot.is 你能详细说明你的问题吗?
  • @shankar-damodaran 感谢您的编辑 :)
  • @ta.speot.is 因为我很特别与未正确配置的安全元素有关。

标签: c# wcf


【解决方案1】:

我关注了这个博客http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx,它强调为了让 HTTPS 工作,您需要将端口绑定到您正在使用的证书。

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

一旦完成,一切都会奏效。

如果需要我的示例代码,请与我联系,以设置和配置以编程方式设置绑定的自托管 WCF 服务器。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多