2013-02-18 72 views
1

我已成功創建WCF self-hosted HTTP Web服務。我使用webserviceHost來創建此服務。我沒有對web.config文件進行任何更改。這裏是我的代碼:如何使用HTTPS訪問WCF WebServiceHost端點?

Instance.cs:

[OperationContract, WebInvoke(Method = "GET", UriTemplate = "/getstatus/", ResponseFormat = WebMessageFormat.Json)] 
    bool getstatus(); 

service.cs:

public bool getstatus() 
    { 
     return true; 
    } 

BindingWS.cs

void bindservice() 
    { 
     try 
     { 
      m_running = true; 
      // Start the host 
      m_serviceHost = new WebServiceHost(typeof(Swiper.cs), new Uri(http://localhost:8083")); 
      ServiceEndpoint ep = m_serviceHost.AddServiceEndpoint(typeof(Instace.cs), new WebHttpBinding(), ""); 
      m_serviceHost.Open(); 
      while (m_running) 
      { 
       // Wait until thread is stopped 
       Thread.Sleep(SleepTime); 
      } 

      // Stop the host 
      m_serviceHost.Close(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.Message); 
      if (m_serviceHost != null) 
      { 
       m_serviceHost.Close(); 
      } 
     } 
    } 

上面的代碼工作正常在運行HtppWCF 。但我怎麼轉換成HTTPS。我關注這麼多的博客,但沒有收到任何東西。可以做到嗎?

+0

只是改變地址? 'new Uri(https:// localhost:8083「)' – 2013-02-18 11:36:26

+0

不,因爲有很多事情要做'證書'。 – 2013-02-18 11:43:25

回答

1

基本上,您需要根據您在主機中使用的端口號手動註冊證書。下面是關於如何實現這一目標

http://msdn.microsoft.com/en-us/library/ms733791.aspx

UPDATE 21/2/13一些細節:

只要您註冊了您的域名證書上面的描述,你應該能夠得到這通過對上面的代碼進行一些調整而工作。以下是使用控制檯應用程序作爲主機的一些示例代碼。 HTH。

using (var serviceHost = new WebServiceHost(typeof(Swiper), new Uri("https://localhost:8083"))) 
     { 
      var secureWebHttpBinding = new WebHttpBinding(WebHttpSecurityMode.Transport) { Name = "secureHttpWeb" }; 
      serviceHost.AddServiceEndpoint(typeof(ISwiper), secureWebHttpBinding, ""); 
      serviceHost.Open(); 
      Console.WriteLine("Service running..."); 
      Console.WriteLine("Press any key to stop service."); 
      Console.ReadLine(); 

      // Stop the host 
      serviceHost.Close(); 
     } 
+0

我對處理證書沒有任何問題,但在上面的代碼中我必須做出什麼樣的改變 – 2013-02-18 10:16:21

+0

@AmitPal你可以發佈你的配置嗎?我假設你在主機應用程序中託管它? – stephenl 2013-02-20 03:00:27

+0

它可以在這裏獲得:http://pastebin.com/96x8Y4nU 順便說一句Web服務全部由程序創建(而不是來自配置文件)。 – 2013-02-20 06:41:11

相關問題