C# 自托管WCF服务使用HTTP而不是HTTPS
好的,我在一个控制台应用程序中托管了一个WCF服务 所有绑定都是通过编程创建的,因此没有配置设置 只要我使用C# 自托管WCF服务使用HTTP而不是HTTPS,c#,wcf,C#,Wcf,好的,我在一个控制台应用程序中托管了一个WCF服务 所有绑定都是通过编程创建的,因此没有配置设置 只要我使用HttpTransportBindingElement,我就有一个正常工作的服务,但是只要我使用HttpTransportBindingElement,那么什么都不起作用,服务就不会显示在浏览器中,客户端应用程序返回一个405(不允许使用方法)通信异常 我已尝试将SecurityBindingElement设置为我的CustomBinding,但我不确定应该使用哪个选项 SecurityB
HttpTransportBindingElement
,我就有一个正常工作的服务,但是只要我使用HttpTransportBindingElement
,那么什么都不起作用,服务就不会显示在浏览器中,客户端应用程序返回一个405(不允许使用方法)通信异常
我已尝试将SecurityBindingElement
设置为我的CustomBinding
,但我不确定应该使用哪个选项
SecurityBindingElement.CreateCertificateOverTransportBindingElement()
SecurityBindingElement.CreateAnonymousForceCertificateBindingElement()
等等
创建主机的代码如下所示
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);
}
baseAddress=newURI(string.Format(“{0}://{1}”,strConnectionType,ConfigurationManager.AppSettings[“baseAddress]”);
ServiceHost主机=新的ServiceHost(类型(IMyService),基地址);
AddServiceEndpoint(typeof(MyService),binding,String.Empty);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpsGetEnabled=证书!=无效的
smb.HttpGetEnabled=证书==null;
host.Description.Behaviors.Add(smb);
ServiceDebugBehavior sdb=host.Description.Behaviors.Find();
如果(sdb==null)
{
host.Description.Behaviors.Add(新的ServiceDebugBehavior(){IncludeExceptionDetailInFaults=true});
}
其他的
{
如果(!sdb.IncludeExceptionDetailInFaults)
{
sdb.IncludeExceptionDetailInFaults=true;
}
}
如果(证书!=null)
{
host.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine,StoreName.My,X509FindType.FindByThumbprint,certificate.Thumbprint);
}
我关注了这个博客,它强调了为了让HTTPS工作,您需要将端口绑定到您正在使用的证书
processbindporttocertificate=newprocess()代码>
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}certhash={1}appid={{{{{2}}}”,端口,证书.指纹,Guid.NewGuid())代码>
bindPortToCertificate.Start()代码>
bindPortToCertificate.WaitForExit()代码>
一旦这样做了,一切都成功了
如果需要我的示例代码,请与我联系,以设置和配置以编程方式设置绑定的自托管WCF服务器。:) 什么是绑定
?此外,WCF服务的编程配置非常困难,为什么不使用配置文件?@ta.speot.is您能详细说明您的问题吗?@shankar damodaran谢谢您的编辑:)@ta.speot.is因为我很特别:)正如我所说,服务在HTTP上运行良好只是HTTPS的一个问题,我想这一定是我缺少的简单内容,我认为这与安全元素有关,而不是配置正确。这里回答了类似的问题