自托管 WCF 和 SSL(再次)

发布于 2024-09-06 10:32:01 字数 3790 浏览 0 评论 0原文

我已经配置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-09-13 10:32:01

我找到了解决方案,请参阅线程

I have found solution, please see thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文