2011-03-07 54 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