自托管 WCF 和 SSL(再次)
我已经配置了 ssl usign httpcfg set ssl
,在此之后我编写了下一个代码:
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
namespace SelfHost
{
internal class Program
{
private static void Main(string[] args)
{
string addressHttps = String.Format("https://{0}:8000/hello", Dns.GetHostEntry("").HostName);
var wsHttpBinding = new WSHttpBinding();
wsHttpBinding.Security.Mode = SecurityMode.Transport;
wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var serviceHost = new ServiceHost(typeof (HelloWorldService),new Uri(addressHttps));
Type endpoint = typeof (IHelloWorldService);
serviceHost.AddServiceEndpoint(endpoint, wsHttpBinding, "MyService");
serviceHost.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName, "myhostname");
serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
X509CertificateValidationMode.PeerOrChainTrust;
var smb = new ServiceMetadataBehavior();
// smb.HttpGetEnabled = true;
smb.HttpsGetEnabled = true;
// smb.HttpsGetUrl = new Uri(addressHttps);
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
serviceHost.Description.Behaviors.Add(smb);
try
{
serviceHost.Open();
string address = serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening @ {0}", address);
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
Console.WriteLine();
}
catch (Exception exc)
{
Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
#region IHelloWorldService Members
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
#endregion
}
}
当前我收到错误:
A commmunication error occurred: HTTP could not register URL https://+:8000/hello/. Another application has already registered this URL with HTTP.SYS.
查询 httpcfg.exe query ssl
返回下一个信息:
IP : 0.0.0.0:8000
Hash : 8e3d19c8778713e544fdd7 f5d8213d37a1757ca
Guid : {06a1ec10-73cc-4a57-828f-17dc7dd444d3}
CertStoreName : MY
CertCheckMode : 0
RevocationFreshnessTime : 0
UrlRetrievalTimeout : 0
SslCtlIdentifier :
SslCtlStoreName :
Flags : 0
----------------------------------------------------------------------------
IP : myip:8000
Hash : 8e3d19c8778713e544fdd7 f5d8213d37a1757ca
Guid : {50bf66e2-807e-4651-b7af-e25fc2a25cac}
CertStoreName : MY
CertCheckMode : 0
RevocationFreshnessTime : 0
UrlRetrievalTimeout : 0
SslCtlIdentifier :
SslCtlStoreName :
Flags : 0
----------------------------------------------------------------------------
如何解决这个错误? 谢谢。
I've configured ssl usign httpcfg set ssl
, after this I've written next code:
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;
namespace SelfHost
{
internal class Program
{
private static void Main(string[] args)
{
string addressHttps = String.Format("https://{0}:8000/hello", Dns.GetHostEntry("").HostName);
var wsHttpBinding = new WSHttpBinding();
wsHttpBinding.Security.Mode = SecurityMode.Transport;
wsHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
var serviceHost = new ServiceHost(typeof (HelloWorldService),new Uri(addressHttps));
Type endpoint = typeof (IHelloWorldService);
serviceHost.AddServiceEndpoint(endpoint, wsHttpBinding, "MyService");
serviceHost.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName, "myhostname");
serviceHost.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
X509CertificateValidationMode.PeerOrChainTrust;
var smb = new ServiceMetadataBehavior();
// smb.HttpGetEnabled = true;
smb.HttpsGetEnabled = true;
// smb.HttpsGetUrl = new Uri(addressHttps);
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
serviceHost.Description.Behaviors.Add(smb);
try
{
serviceHost.Open();
string address = serviceHost.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening @ {0}", address);
Console.WriteLine("Press enter to close the service");
Console.ReadLine();
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("A commmunication error occurred: {0}", ce.Message);
Console.WriteLine();
}
catch (Exception exc)
{
Console.WriteLine("An unforseen error occurred: {0}", exc.Message);
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IHelloWorldService
{
[OperationContract]
string SayHello(string name);
}
public class HelloWorldService : IHelloWorldService
{
#region IHelloWorldService Members
public string SayHello(string name)
{
return string.Format("Hello, {0}", name);
}
#endregion
}
}
Currently I'm receiving error:
A commmunication error occurred: HTTP could not register URL https://+:8000/hello/. Another application has already registered this URL with HTTP.SYS.
The query httpcfg.exe query ssl
returns next info:
IP : 0.0.0.0:8000
Hash : 8e3d19c8778713e544fdd7 f5d8213d37a1757ca
Guid : {06a1ec10-73cc-4a57-828f-17dc7dd444d3}
CertStoreName : MY
CertCheckMode : 0
RevocationFreshnessTime : 0
UrlRetrievalTimeout : 0
SslCtlIdentifier :
SslCtlStoreName :
Flags : 0
----------------------------------------------------------------------------
IP : myip:8000
Hash : 8e3d19c8778713e544fdd7 f5d8213d37a1757ca
Guid : {50bf66e2-807e-4651-b7af-e25fc2a25cac}
CertStoreName : MY
CertCheckMode : 0
RevocationFreshnessTime : 0
UrlRetrievalTimeout : 0
SslCtlIdentifier :
SslCtlStoreName :
Flags : 0
----------------------------------------------------------------------------
How to fix this error?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案,请参阅线程
I have found solution, please see thread