2012-04-13 113 views
9

我對如何暴露WCF端點有些困惑暴露net.tcp端點

我有一個tcp端點和一個mex tcp端點。

<service name="MessageReaderService.MessageReaderService"> 
    <endpoint name="NetTcpReaderService" 
     address="ReaderService" 
     binding="netTcpBinding" bindingConfiguration="" 
     contract="Contracts.IMessageReaderService" /> 
    <endpoint name="netTcpMex" 
     address="mex" 
     binding="mexTcpBinding" bindingConfiguration="" 
     contract="IMetadataExchange" /> 
    <host> 
     <baseAddresses> 
      <add baseAddress="net.tcp://localhost:8082" /> 
     </baseAddresses> 
    </host> 
</service> 

當我嘗試在服務主機我得到下面的異常運行此:

合同名稱「IMetadataExchange接口」無法通過該服務MessageReaderService實現合同 的列表中找到。將ServiceMetadataBehavior添加到
配置文件或直接添加到ServiceHost以啓用對此合同的支持。

所以我從這個錯誤得出結論,我需要添加一個服務行爲來公開元數據。

所以我添加的行爲:

<behavior name="ServiceBehavior"> 
    <serviceMetadata httpGetEnabled="true"/>     
</behavior> 

但後來我得到一個不同的錯誤:

的ServiceMetadataBehavior的HttpGetEnabled屬性設置爲true,並且 HttpGetUrl屬性是相對地址,但沒有http基地址。 提供一個http基地址或將HttpGetUrl設置爲一個絕對地址。

  1. 所以現在我有實際添加另一個端點(HTTP)暴露在mexhttpbinding元數據?
  2. 有沒有一種簡單的方法通過tcp公開端點?

回答

16

兩件事情:

(1)一旦你定義的服務行爲,你當然也必須應用它的服務!

<service name="MessageReaderService.MessageReaderService" 
     behaviorConfiguration="ServiceBehavior"> 

(2)你並不需要一個HTTP端點 - 你不需要有一個HTTP URL - 只是這樣定義該服務行爲:

<behavior name="ServiceBehavior"> 
    <serviceMetadata /> 
</behavior> 

你的元數據現在可用通過mexTcpBinding端點 - 您無法使用HTTP瀏覽它,但客戶端肯定可以連接到它並使用它!

您可以通過使用WCF Test Client並打算要麼

net.tcp://localhost:8082  (the base address) 

或在這兩種情況下

net.tcp://localhost:8082/mex (the mex address) 

,WCF測試客戶端現在應該找到你的服務,並能夠發現驗證這一點的能力。