2010-04-05 40 views
8

我想獲得WCF-over-TCP服務工作。我在修改我自己的項目時遇到了一些問題,所以我想我會從包含在VS2008中的「基本」WCF模板開始。mexTcpBinding在WCF - IMetadataExchange錯誤

這裏是最初的WCF App.config中,當我運行該服務的WCF測試客戶端可以用它很好地工作:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.web> 
     <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
     <services> 
      <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior"> 
       <host> 
        <baseAddresses> 
         <add baseAddress="http://localhost:8731/Design_Time_Addresses/WcfTcpTest/Service1/" /> 
        </baseAddresses> 
       </host> 
       <endpoint address="" binding="wsHttpBinding" contract="WcfTcpTest.IService1"> 
        <identity> 
         <dns value="localhost"/> 
        </identity> 
       </endpoint> 
       <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
      </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="WcfTcpTest.Service1Behavior"> 
        <serviceMetadata httpGetEnabled="True"/> 
        <serviceDebug includeExceptionDetailInFaults="True" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
    </system.serviceModel> 
</configuration> 

這在所有的作品完美,沒有任何問題。

我想從HTTP將其更改爲TCP將是微不足道的:更改綁定到他們的TCP等價物和去除httpGetEnabled serviceMetadata元素:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.web> 
     <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
     <services> 
      <service name="WcfTcpTest.Service1" behaviorConfiguration="WcfTcpTest.Service1Behavior"> 
       <host> 
        <baseAddresses> 
         <add baseAddress="net.tcp://localhost:1337/Service1/" /> 
        </baseAddresses> 
       </host> 
       <endpoint address="" binding="netTcpBinding" contract="WcfTcpTest.IService1"> 
        <identity> 
         <dns value="localhost"/> 
        </identity> 
       </endpoint> 
       <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/> 
      </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="WcfTcpTest.Service1Behavior"> 
        <serviceDebug includeExceptionDetailInFaults="True" /> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
    </system.serviceModel> 
</configuration> 

但是當我運行這個我在WCF服務得到這個錯誤主機:

System.InvalidOperationException:無法在服務Service1實現的合同列表中找到合同名稱'IMetadataExchange'。將ServiceMetadataBehavior添加到配置文件或直接添加到ServiceHost以啓用對此合同的支持。

我覺得你不能使用TCP發送元數據,但這就是爲什麼有mexTcpBinding選項?

回答

20

那麼,如果你想有元數據 - TCP或HTTP - 你仍然需要包括serviceMetadata的行爲!

<behaviors> 
    <serviceBehaviors> 
     <behavior name="WcfTcpTest.Service1Behavior"> 
      <serviceDebug includeExceptionDetailInFaults="True" /> 
      <serviceMetadata /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 

當然,你不能有一個「HttpGetEnabled」就可以了 - 但行爲本身必須存在,以使元數據(並因此IMetadataExchange合同)的交換。

+0

謝謝!那樣做了。 我不認爲WCF的配置是正確的。 App.config是一個配置文件,我認爲當一個配置元素被刪除時,它僅僅意味着「我沒有明確設置任何配置」而不是「禁用此功能」。更好的方法是這樣的: Dai 2010-04-05 16:28:51

+1

@David:好的,這是一個人可能會爭論很長時間的選項。 WCF只是使用「如果它不存在,它不是主動」的方法。一旦你知道了,這很好,並且很有意義(你不必把它放在那裏,並設置active = false來禁用它 - 只是把它放在外面) – 2010-04-05 18:13:02