2011-03-07 52 views
16

我只是在學習wcf,現在已經有這麼多了。WCF服務主機找不到任何服務元數據

CS文件:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 

namespace wcfLib 
{    
    [ServiceContract] 
    public interface IfaceService 
    { 
     [OperationContract] 
     int wordLen(string word); 
    } 

    public class StockService : IfaceService 
    { 
     public int wordLen(string word) 
     { 
      return word.Length; 
     } 
    } 
} 

然而,當我試圖運行它,它會彈出一個錯誤:

WCF service host cannot find any service metadata...

任何想法,這可能是什麼?

配置文件:

<system.serviceModel> 
    <services> 
     <service behaviorConfiguration="wcfLib.Service1Behavior" name="wcfLib.Service1"> 
     <endpoint address="" binding="wsHttpBinding" contract="wcfLib.ser"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8732/Design_Time_Addresses/wcfLib/Service1/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="wcfLib.Service1Behavior"> 
      <serviceMetadata httpGetEnabled="True"/> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
+5

您需要向我們展示您的** config **文件!像元數據交換的東西在配置 –

+0

中定義並且您嘗試連接到哪個URL以獲取元數據?你如何主辦這項服務 - IIS或自託管? –

+2

Yoru代碼和配置不匹配 - 代碼中的服務是'wcfLib.StockService',但您的標籤包含'name = wcfLib.Service1' - 這些名稱需要匹配!與端點上的'contract =「wcfLib.ser」'屬性相同 - 需要匹配命名空間+接口名稱! ('wcfLib.IfaceService') –

回答

23

你必須在你的配置文件中的以下內容:

1)服務行爲元數據:

<behaviors> 
    <serviceBehaviors> 
    <behavior name="Metadata"> 
     <serviceMetadata httpGetEnabled="true" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

2)參考,在服務的配置服務行爲

<service name="wcfLib.StockService" 
     behaviorConfiguration="Metadata"> 
    .... 
</service> 

*配置文件中服務標籤中的名稱值必須與實施合同的物理類名稱相同。請記住,如果班級名稱發生更改,請確保將此值更改爲匹配。

3)代替MEX(元數據交換)

<service name="wcfLib.StockService" 
     behaviorConfiguration="Metadata"> 
    .... 

    <endpoint name="mex" 
       address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
</service> 

有了這一切的終點,事情應該是蠻好的! :-)

+0

謝謝! :)作品! – Rob

+3

我有你在上面的帖子中提到的所有它仍然我得到相同的錯誤(對話框)。 –

0

默認情況下,Visual Studio是要嘗試建立一個客戶端/測試你用你的新服務來玩玩。爲此,需要了解您的服務提供的結構和方法。這是通過消費標準WSDL格式的定義來實現的。但是,默認情況下,WCF不會發布此數據。

您必須設置一個元數據交換端點行爲來使用acehive。

要麼在這裏發表您的配置文件,我們可以協助,或谷歌的元數據交換和WSDL /堆棧搜索在WCF

0

我得到這個錯誤,因爲我的服務名稱是錯誤的。然後使用netTcpBinding和mexTcpBinding以httpGetEnabled = False工作。

0
// get the <system.serviceModel>/<services> config section 
ServicesSection services = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection; 

ServiceHost host = new ServiceHost(typeof(SelfHostedService.Service)); 

// enumerate over each <service> node 
foreach (ServiceElement aService in services.Services) 
{ 
    Console.WriteLine(); 
    Console.WriteLine("Name: {0}/Behavior: {1}", aService.Name, aService.BehaviorConfiguration); 

    // enumerate over all endpoints for that service 
    foreach (ServiceEndpointElement see in aService.Endpoints) 
    { 
     Console.WriteLine("\tEndpoint: Address = {0}/Binding = {1}/Contract = {2}", see.Address, see.Binding, see.Contract); 
     //host.AddServiceEndpoint(
    } 
} 

try 
{ 
    Console.WriteLine("Service EndPoints are: "); 
    foreach (ServiceEndpoint endpoint in host.Description.Endpoints) 
    { 
     Console.WriteLine("{0} ({1})", endpoint.Address.ToString(), endpoint.Binding.Name); 
    } 
    host.Open(); 

    Console.WriteLine(string.Concat("Service is host at ", DateTime.Now.ToString())); 
    Console.WriteLine("\n Self Host is running... Press <Enter> key to stop"); 
} 
catch(Exception ex) 
{ 
    Console.WriteLine(ex.Message.ToString()); 
} 

如果還是不行,以便刪除當前配置文件,其默認名稱的App.config重建,這是作品。

15

我得到這個確切的同樣的問題,並大力經歷我的配置和一切被內嵌在元數據終端等問題?這條線:

<service behaviorConfiguration="wcfLib.Service1Behavior" name="wcfLib.Service1"> 

name必須必須有正在執行合同的物理類的名稱。我忘了......再次任意命名它認爲它可能是任何字符串。所以在上面的實例中,必須命名爲Service1。如果類名稱更改,請確保更改此值。

這就像WCF 101的東西,我仍然被它燒了,即使我自從在CTP Framework 3.0以來一直在做WCF。 Blah ...

+0

當我按照改變服務接口和類名的教程時,我忘記了這一點。使用代碼重構來重命名可以避免這種情況。 – jingtao

+0

謝謝你這是非常非常有幫助:) – TeaLeave

0

如果您以編程方式創建服務主機,並且忘記將[ServiceContract]和[OperationContract]屬性添加到接口合同中,您也可能會得到同樣的錯誤。

1

創建此問題的最簡單方法是簡單重新考慮您的接口名稱。它肯定會更改項目中的實例名稱,但它無法更新web.config文件。重新創建一個新的服務,重命名你的界面,然後重擊F5,繁榮,元數據對話框出現:-)答案與上面一樣,只要記住要手動改變你的web.config文件。

問候

0

我知道這樣一個老問題,但我想我給關於這個問題我的2美分,因爲它只是發生在我身上。

我改變了我的構建平臺從「任何CPU」到「x86」的Web服務本身。第二我把它改回「任何CPU」,它解決了這個問題。

-1

我有HTTP激活打開。確保你有它。

Ensure HTTP Activation is on