2010-06-21 46 views
5

Tôi đã cấu hình ssl usign httpcfg set ssl, sau này tôi đã viết mã tiếp theo:Selfhosted WCF và SSL (Một lần nữa)

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

Hiện nay tôi đang nhận lỗi:


A commmunication error occurred: HTTP could not register URL https://+:8000/hello/. Another application has already registered this URL with HTTP.SYS. 

Truy vấn httpcfg.exe query ssl trả về thông tin tiếp theo:

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 
---------------------------------------------------------------------------- 

Cách khắc phục lỗi này? Cảm ơn.

Trả lời

0

Tôi đã tìm thấy giải pháp, vui lòng xem thread

Các vấn đề liên quan