2009-07-25 46 views
3

內的WCF服務。 我可以開始我的WCF服務在VS2008和在我的app.config導航基地址主機我有一個託管的Windows服務中WCF服務的一些麻煩Windows服務

<configuration> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="WCF.IndexerBehavior" 
     name="WCF.Indexer"> 
     <endpoint address="" binding="wsHttpBinding" contract="WCF.IIndexer"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" 
contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost/WCFService/Action/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WCF.IndexerBehavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

我可以看到它工作得很好,我得到的頁面說我創建瞭如何服務和代碼示例顯示使用它。現在

我的下一個步驟是創建一個Windows服務上面顯示我的WCF主辦。

我只是用TE窗口服務模板,它給了我一個Program.cs中和Service1.cs我改名爲WindowsServiceHost.cs。我有:

private ServiceHost host; 

     public WindowsServiceHost() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      try 
      { 
       var serviceType = typeof(Indexer.WCF.Indexer); 
       host = new ServiceHost(serviceType); 
       host.Open(); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 

     protected override void OnStop() 
     { 
      if (host != null) 
      { 
       host.Close(); 
      } 
     } 

一切都編譯好,我可以運行InstallUtil(我定義了一個安裝程序)。 該服務用於啓動和立即停止,而是禁用Windows Defender的擺脫了這一點。 現在在服務啓動(作爲網絡服務)和熬夜(我認爲),但是當我瀏覽到的基地址,我得到的頁面未找到。 另一個奇怪的事情是,當我試圖停止服務(這仍顯示爲正在運行),我得到:

錯誤1061:服務不能在這個時候

我已經竭盡所能,但我接受控制信息不知所措。

+0

您的服務是否作爲可以從命令行運行的獨立可執行文件運行,即作爲自託管應用程序運行?您必須先按照您所描述的方式將其作爲Windows服務運行才能正常工作。 要調試這種問題,在OnStart中引入時間延遲可能很有用,因此您可以在執行其餘代碼之前將Visual Studio調試器附加到服務進程。它還可以幫助將異常記錄到Windows事件日誌中,而不是將它們吞併。 – 2009-07-25 20:24:12

回答

3

不是100%確定是什麼原因真的是 - 只是爲了確認,在Windows中,我們自託管WCF服務服務所有的時間和一般工作完全正常。

兩個點,你可以嘗試 - 只是爲了獲得對動作的感覺,對這個問題的潛在線索:

1)我注意到你打開的ServiceHost與服務的只是類型 - 這樣的作品,但你可能還是要一個基地址添加甚至到了new ServiceHost()的呼喚 - 這樣的:

host = new ServiceHost(serviceType, 
         new Uri("http://localhost:8181/WCFService/Action/"); 

您可以導航到該地址並獲得服務頁面?

2)我注意到的另一件事是您的服務似乎打開主機之前被稱爲Indexer.WCF.Indexer如在typeof(指定),但在配置文件中,對<service>標籤name=只是「WCF.Indexer 」。

你能不能試圖更改標籤閱讀:

<service behaviorConfiguration="WCF.IndexerBehavior" 
     name="Indexer.WCF.Indexer"> 

這是否幫助?在瀏覽器中瀏覽時是否可以看到服務頁面?

馬克

0

嘗試刪除catch語句,有可能是你沒有看到

1

自託管HTTP在Windows服務可能需要註冊與HttpCfg.exe端點錯誤。看看here

相關問題