2015-01-15 41 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