2010-06-17 88 views
0

我創建了測試自託管的wcf應用程序,並嘗試添加支持https。服務器應用程序的 代碼是:WCF服務:WSHttpBinding

using System; 
using System.Security.Cryptography.X509Certificates; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using System.ServiceModel.Security; 

namespace SelfHost 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string addressHttp = String.Format("http://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName); 
      Uri baseAddress = new Uri(addressHttp); 
      WSHttpBinding b = new WSHttpBinding(); 
      b.Security.Mode = SecurityMode.Transport; 
      b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate; 
      Uri a = new Uri(addressHttp); 
      Uri[] baseAddresses = new Uri[] { a }; 
      ServiceHost sh = new ServiceHost(typeof(HelloWorldService), baseAddresses); 
      Type c = typeof(IHelloWorldService); 
      sh.AddServiceEndpoint(c, b, "hello"); 
      sh.Credentials.ServiceCertificate.SetCertificate(
       StoreLocation.LocalMachine, 
       StoreName.My, 
       X509FindType.FindBySubjectName,"myCert"); 
      sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode = 
      X509CertificateValidationMode.PeerOrChainTrust; 
      try 
      { 
       sh.Open(); 

       string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri; 
       Console.WriteLine("Listening @ {0}", address); 
       Console.WriteLine("Press enter to close the service"); 
       Console.ReadLine(); 
       sh.Close(); 
      } 
      catch (CommunicationException ce) 
      { 
       Console.WriteLine("A commmunication error occurred: {0}", ce.Message); 
       Console.WriteLine(); 
      } 
      catch (System.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 
    { 
     public string SayHello(string name) 
     { 
      return string.Format("Hello, {0}", name); 
     } 
    } 
} 

什麼名字(地址),我應該伸到線

sh.AddServiceEndpoint(c, b, "hello"); 

因爲「你好」是不正確的?

謝謝。

+0

你說的 「不正確」 是什麼意思?你會得到一個錯誤 - 如果是這樣,它是什麼?你沒有得到一個錯誤,但服務沒有按預期工作? – 2010-06-17 12:37:58

+0

是的,我得到文本錯誤: 無法找到與綁定WSHttpBinding的端點匹配scheme https的基地址。註冊的基地址方案是[http]。 – jitm 2010-06-17 12:39:58

回答

0
sh.AddServiceEndpoint(c, b, "https://xxxx:xx/service"); 
+0

Like string addressHttp = String.Format(「http:// {0}:8002/hello」,System.Net.Dns.GetHostEntry(「」)。HostName);只有變更是https? – jitm 2010-06-17 12:38:53

0

基本上,AddServiceEndpoint中的第三個參數是服務的地址。

如果您已經定義了基地址(因爲您有- http://{0}:8002/hello),它是一個相對地址 - 它將被添加到相應協議的基地址。

你的情況

因此,加入此服務端點,你會得到在端點:

http://{0}:8002/hello/hello 

您可以連接到端點,跟我們的服務?

或者你也可以定義一個完全指定的地址 - 如果你沒有任何基地址,這個特別有用。如果指定完整地址,則將使用該地址(覆蓋定義的基址)。因此,如果您使用:

AddServiceEndpoint(c, b, "http://server:8888/HelloService") 

那麼您的服務將可以在該特定的URL訪問 - 無論您之前定義的基地址如何。

更新:感謝您的評論。是的,如果您將安全模式定義爲「傳輸」,那麼您需要爲所有地址使用https://

定義基地址:

string addressHttp = String.Format("https://{0}:8002/hello", System.Net.Dns.GetHostEntry("").HostName); 

或壓倒一切的一個完全合格的地址:

AddServiceEndpoint(c, b, "https://server:8888/HelloService") 
+0

謝謝,目前服務器啓動沒有錯誤,但如果我嘗試綁定客戶端到服務器,我收到下一個錯誤: 下載'https:// server:8002/hello'時出錯。 底層連接已關閉:發送時發生意外錯誤。 驗證失敗,因爲遠程方關閉了傳輸流。 元數據包含無法解析的引用:'https:// server:8002/hello'。 繼續在下一條評論中顯示錯誤消息... – jitm 2010-06-17 12:53:33

+0

將HTTP請求發送到https:// server:8002/hello時發生錯誤。這可能是由於在HTTPS情況下服務器證書未使用HTTP.SYS正確配置。這也可能是由於客戶端和服務器之間的安全綁定不匹配造成的。 底層連接已關閉:發送時發生意外錯誤。 驗證失敗,因爲遠程方關閉了傳輸流。 如果服務在當前解決方案中定義,請嘗試構建解決方案並再次添加服務引用。 如何解決它? – jitm 2010-06-17 12:54:08

+0

如果我做httpcfg查詢ssl 我可以看到可配置的HTTP.SYS -------------------------------- ---------------------------------------------- IP:0.0 .0.0:8002 哈希:1111111111111111111111111111111111111 GUID:{00000000-0000-0000-0000-000000000000} CertStoreName:(空) CertCheckMode:0 RevocationFreshnessTime:0 UrlRetrievalTimeout:0 SslCtlIdentifier:(空) SslCtlStoreName:( null) 標記:0 – jitm 2010-06-17 13:04:48