2014-03-13 231 views
0

我遵循此(http://msdn.microsoft.com/en-us/library/ms733069.aspx)鏈接並創建了服務和服務主機。 我向解決方案添加了一個webform客戶端項目。爲了檢查我的服務是否收到請求,我在服務中添加了一個日誌。 我選擇了我的主機和客戶端,通過設置多個啓動項目同時運行。 但我有一個問題,使我的服務和客戶端之間的溝通。我缺少配置中的東西?我沒有看到異常(儘管我選擇了CLR和JSRuntime異常,並且提供了託管調試幫助)。託管在Windows服務託管的WCF服務

這裏是我的服務配置

<?xml version="1.0"?> 
    <configuration> 
     <system.serviceModel> 
     <client/> 
     <behaviors> 
      <serviceBehaviors> 
      <behavior name="meta"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <services> 
      <service name="InboundMessage.Service.Operator" behaviorConfiguration="meta" > 
      <endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" name="Ws" />--> 
      <host> 
       <baseAddresses> 
       <add baseAddress = "http://IP/InboundMessage.Service/"/> 
       </baseAddresses> 
      </host> 
      </service> 
     </services> 
     <bindings> 
      <basicHttpBinding> 
      <binding name="InboundMessage.Service.Operator"/> 
      </basicHttpBinding> 
     </bindings> 
     </system.serviceModel> 
     <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
     </startup> 
    </configuration> 

服務主機:

 <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
     <system.serviceModel> 
     <behaviors> 
      <serviceBehaviors> 
      <behavior name="meta"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <diagnostics performanceCounters="ServiceOnly" /> 
     <services> 
      <service name="InboundMessage.Service.Operator" behaviorConfiguration="meta"> 
      <endpoint address="basic" binding="basicHttpBinding" contract="InboundMessage.Service.IOperator" name="basic"/> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
      <!--<endpoint address="" binding="wsHttpBinding" contract="IMetadataExchange" />--> 
      </service> 
     </services> 
     </system.serviceModel> 
     <system.web> 
     <compilation 
      debug="true" > 
     </compilation> 
     </system.web> 
     <system.webServer> 
     <directoryBrowse enabled="true"/> 
     </system.webServer> 
    </configuration> 

一個windowform客戶端配置:

 <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
     <configSections> 
     </configSections> 

     <system.web> 
     <compilation debug="true"></compilation> 
     </system.web> 
     <system.serviceModel> 
     <services> 
      <service behaviorConfiguration="meta" name="InboundMessage.Service.Operator"> 
      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
      <host> 
       <baseAddresses> 
       <add baseAddress="http://IP/InboundMessage.Service/"/> 
       </baseAddresses> 
      </host> 
      </service> 
     </services> 
     <bindings> 
     <basicHttpBinding>  
      <binding name="InboundMessage.Service.Operator"/> 
      </basicHttpBinding> 
     </bindings> 
     <behaviors> 
      <serviceBehaviors> 
      <behavior name="meta"> 
       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior>  
      </serviceBehaviors> 
     </behaviors> 
     </system.serviceModel> 
    </configuration>      

編輯: 二手Tim的評論安裝服務,但我安裝時遇到問題。 我打開另一個問題,謝謝蒂姆我有問題在我的本地機器上安裝服務。我打開另一個問題:Unable to install service using sc command

回答

0

想到幾件事情。

首先(我不是100%確定,但這是基於我的經驗),您無法通過Visual Studio將Windows服務作爲Windows服務運行。您需要構建該項目,然後按照您鏈接到的頁面上的指示進行安裝。其次,您只需要兩個配置文件,而不是三個 - Windows服務(這是服務的配置所在的位置)和一個客戶端。我不確定你有什麼作用(或者相信你有)服務主機配置文件。

第三,您的客戶端配置在<system.serviceModel>部分有一個服務條目 - 如果您的客戶端還託管服務,您只需要這些服務,但在您提出的問題中似乎不是這種情況。您應該刪除<services>部分,並添加一個<client>部分,像這樣:

<client> 
    <endpoint address="http://IP/InboundMessage.Service" 
      binding="basicHttpBinding" 
      bindingConfiguration="InboundMessage.Service.Operator" 
      contract="InboundMessage.Service.IOperator" /> 
</client> 

請注意,我用了bindingConfiguration屬性之上 - 不這樣做,你的客戶會使用默認basicHttpBinding(在你的情況並不重要因爲你沒有設置名稱以外的任何其他名稱,但是如果你設置了非默認值,你會想告訴客戶端使用哪個綁定配置)。

實際上,最簡單的方法(入門)是構建Windows服務,安裝並啓動它,然後在客戶端(WinForm)應用程序中添加服務引用。這將生成您需要的所有內容,您可以查看配置文件以查看所需的設置。

+0

感謝您的回覆。當你說「(在你的情況下,因爲你沒有設置除名稱以外的任何內容都不重要」,你能詳細說明你認爲我設置名稱的位置嗎?它是否在服務中?另一件事情,因爲我刪除了標籤客戶端是否需要刪除標籤? – HXD

+0

@HXD - 對不起,我在答案的這一部分並不十分清楚,我的意思是你在配置文件中定義了一個'basicHttpBinding'配置,命名爲「InboundMessage.Service.Operator」。您不會在端點中引用該綁定配置,因此不會使用它。由於您沒有爲「InboundMessage.Service.Operator」綁定配置指定任何值,因此無關緊要。例如,如果您指定了更大的郵件大小限制,那麼這很重要。是的,你也可以刪除''標籤。 – Tim

+0

謝謝蒂姆我有問題在我的本地機器上安裝服務。我打開另一個問題:http://stackoverflow.com/questions/22416910/unable-to-install-service-using-sc-command – HXD