2012-05-28 57 views
0

我想通過以下方式來開發一個WCF服務:通過TCP |來託管WCF服務構建項目

  • 一個類庫項目,將所有合同及其履行情況,稱爲WcfContracts。

  • WCF服務庫項目將具有配置部分,即它將只有app.config。該項目將引用類實例庫,該實例具有合同&其實現,即WcfContracts.This項目稱爲WcfServiceHosting。

  • 可以託管WCF服務的Windows服務。該服務將具有與WCF服務相同的app.config,並將僅用於託管WCF服務。我在服務項目中創建了一個新的app.config,並將app.config的內容從WCF服務庫複製到它。

  • 一個WPF客戶端應用程序,旨在與Windows服務中託管的服務交談。我將ServiceReference添加到WCF合同中。

  • 所有的項目都在同一個解決方案中。

但是在做上述操作時,我無法在客戶端項目中添加服務引用。如果我將合同&實現保留在相同的WCF服務庫中,則一切正常。

以下是我的WCF服務庫的app.config:

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

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 

    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="WcfServiceHosting.Service1Behavior" 
     name="WcfServiceHosting.CalculatorService"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 
      contract="**WcfServiceHosting.ICalculatorService**"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
      contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8523/CalculatorService" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WcfServiceHosting.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 

現在上面的作品就好了,因爲IcalculatorService和CalculatorService的駐留在同一裝配即WcfServiceHosting。

現在,如果我從WcfServiceHosting中刪除ICalculatService和CalculatorService並將其添加到WcfContracts,我無法添加服務引用。它在發現時不會顯示在參考對話框中。

以下是我的合同變更:

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

    <system.web> 
    <compilation debug="true" /> 
    </system.web> 

    <system.serviceModel> 
    <services> 
     <service behaviorConfiguration="WcfServiceHosting.Service1Behavior" 
     name="**WcfContracts.CalculatorService**"> 
     <endpoint address="" binding="netTcpBinding" bindingConfiguration="" 
      contract="WcfContracts.ICalculatorService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" 
      contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8523/CalculatorService" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="WcfServiceHosting.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="false" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 

以上不工作,你能告訴我爲什麼?我已經添加了必要的參考資料和全部。

謝謝, 邁克 -

+0

如果我已經解釋了這個權利,我認爲你缺少的是.config的不共享。正在執行的程序將加載它的配置,而不是任何衛星/引用程序集。 –

+0

您的意思是說Reference.cs中沒有代碼是通用的?請添加關於哪個項目引用哪些信息。這一點很重要。 –

回答

0

添加服務引用將無法看到未託管任何合同。您需要引用承載WCF服務的項目(或包含它們)的項目。

例如如果主機不在那裏包含配置,如何添加引用知道應該如何託管CalculatorServce(TCP,HTTP,NamedPipes,MSMQ)?