2016-09-13 92 views
0

我想在WPF應用程序內託管WCF服務,但我無法這樣做。在WPF應用程序中託管WCF服務

這裏是我的實現代碼:

ServiceHost host = null; 
using (host = new ServiceHost(typeof(WcfJsonTransferRestService.apiService))) 
      { 
       host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint1"); 
       host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint2"); 
       host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint3"); 

host.Open(); 

一切正常,運行良好,但該服務沒有啓動。

任何人都知道我的問題可能是什麼?

感謝

+0

你有沒有嘗試單步執行調試器中的代碼?是否引發錯誤?事件查看器中是否有任何內容? – Tim

+0

是的,一切都創造良好,但當我嘗試連接(使用高級REST客戶端),我得到一個錯誤,如果該服務沒有運行 – d199224

+0

從您的發佈代碼,它看起來像你託管的SOAP服務 - REST是不同於肥皂。你嘗試過使用'WebHttpBinding'而不是'WSHttpBinding'嗎? – Tim

回答

2

最有可能的問題是,你已經包裹在一個using語句創建和ServiceHost開幕。一旦using聲明結束(並且從發佈的代碼中不清楚它的位置),ServiceHost實例將被關閉。

換句話說,如果您關閉host.Open();這樣後using塊右:

using (host = new ServiceHost(typeof(WcfJsonTransferRestService.apiService))) 
{ 
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint1"); 
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint2"); 
    host.AddServiceEndpoint(typeof(WcfJsonTransferRestService.IApiService), new WebHttpBinding(), "http://localhost:3300/api/endpoint3"); 

    host.Open(); 
} 

主機將被關閉,應用程序將無法接收請求。通常,您需要在應用程序啓動時(或者在給定的事件中)打開主機,然後在應用程序退出時關閉主機。

+0

謝謝蒂姆!我不知道我是如何錯過它的! – d199224

+0

我認爲幾個樣本都有使用,它被複制到人民代碼中。 –

相關問題