2015-01-15 42 views
0

我試圖在自託管的Windows服務上實現HTTPS。該服務是RESTful(或試圖成爲)。使用普通HTTP的服務工作正常。但是,當我切換到HTTPS時,它不會發送到該端口的任何HTTPS請求返回400錯誤並且不記錄/信息。HTTPS在自我託管的WCF上發生400錯誤

我看着這個參考https://pfelix.wordpress.com/2011/04/21/wcf-web-api-self-hosting-https-and-http-basic-authentication/

,尤其是這一個。 (詹姆斯奧斯本)。 http://blogs.msdn.com/b/james_osbornes_blog/archive/2010/12/10/selfhosting-a-wcf-service-over-https.aspx

使用後者,我能夠將證書綁定到端口,並使用他的測試控制檯和應用程序通過HTTPS進行通信。但是,該應用程序在客戶端和服務器上都有數據協定,而對於我而言,我希望使用網絡瀏覽器發送HTTPS請求,所以這不起作用。

簡而言之,我想通過HTTPS調用我的測試服務,並在有效負載/瀏覽器窗口中返回「SUCCESS」,而我得到一個沒有任何細節的400錯誤。我很確定證書綁定到端口,因爲我通過hTTPS在該端口上使用了一個測試服務器/客戶端,並且它工作正常。

這是我的服務器代碼。

private void StartWebService() 
{ 
    Config.ReadConfig(); 
    String port = Config.ServicePort; 
    eventLog1.WriteEntry("Listening on port" + port); 

    //BasicHttpBinding binding = new BasicHttpBinding(); 
    //binding.Security.Mode = BasicHttpSecurityMode.Transport; 


    // THESE LINES FOR HTTPS 
    Uri httpsUrl = new Uri("https://localhost:" + port + "/"); 
    host = new WebServiceHost(typeof(WebService), httpsUrl); 
    BasicHttpBinding binding = new BasicHttpBinding(); 
    binding.Security.Mode = BasicHttpSecurityMode.Transport; 

    // THIS IS FOR NORMAL HTTP 
    //Uri httpUrl = new Uri("http://localhost:" + port + "/"); 
    //host = new WebServiceHost(typeof(WebService), httpUrl); 
    //var binding = new WebHttpBinding(); // NetTcpBinding(); 

    host.AddServiceEndpoint(typeof(iContract), binding, ""); 
    ServiceDebugBehavior stp = host.Description.Behaviors.Find<ServiceDebugBehavior>(); 
    stp.HttpHelpPageEnabled = false; 

    host.Open(); 

} 

,這裏是WebService的

class WebService : iContract 
{ 

    public string TestMethod() 
    { 
     return "SUCCESS"; 
    } 
    public string HelloWorld() 
    { 
     return "SUCCESS"; 
    } 

這裏是iContract

[ServiceContract] 
    interface iContract 
    { 
     [OperationContract] 
     [WebGet] 
     string TestMethod(); 

     [WebInvoke(Method = "GET", 
      UriTemplate = "HelloWorld", 
      ResponseFormat = WebMessageFormat.Json, 
      BodyStyle = WebMessageBodyStyle.Wrapped)] 
     Stream HelloWorld(); 

回答

1

basicHttpBinding的習慣與REST服務工作。像使用WebHttpBinding一樣,將它用於HTTP端點。

+0

非常感謝,這是正確的解決辦法。現在工作。 – Rob