【问题标题】:WCF service host cannot find any service metadataWCF 服务主机找不到任何服务元数据
【发布时间】:2011-03-07 15:45:42
【问题描述】:

我刚刚学习 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 服务主机找不到任何服务元数据...

知道它可能是什么吗?

配置文件:

<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>

【问题讨论】:

  • 您需要向我们展示您的 config 文件!像元数据交换这样的东西在配置中定义
  • 您尝试连接到哪个 URL 以获取元数据?您如何托管此服务 - IIS 或自托管??
  • Yoru 代码和配置不匹配 - 您的代码中的服务是 wcfLib.StockService 但您的 标记包含 name=wcfLib.Service1 - 这些名称需要匹配!与端点上的 contract="wcfLib.ser" 属性相同 - 需要匹配命名空间+接口名称! (wcfLib.IfaceService)
  • 我遇到了同样的问题,这是一个与名称不匹配的问题

标签: c# wcf


【解决方案1】:

您需要在配置文件中包含以下内容:

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>

有了这一切,一切都会好起来的! :-)

【讨论】:

  • 我有你在上面的帖子中提到的所有内容,但我仍然收到同样的错误(对话框)。
【解决方案2】:

我遇到了同样的问题,并且正在大力检查我的配置,一切都与元数据端点等内联。问题是什么?这一行:

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

name必须,必须具有正在实施合同的物理类的名称。我忘记了......再次任意命名它,以为它可以是任何字符串。所以在上面的例子中,实现类必须命名为Service1。如果类名发生变化,请务必更改此值。

这就像 WCF 101 的东西,即使我从 Framework 3.0 中的 CTP 开始就一直在做 WCF,我仍然被它烧毁。废话……

【讨论】:

  • 我在学习更改服务接口和类名的教程时忘记了这一点。使用代码重构重命名可以避免这种情况。
  • 谢谢你这非常有帮助:)
【解决方案3】:

我打开了 HTTP 激活。确保你有它。

【讨论】:

  • 这是我的问题。谢谢。
【解决方案4】:

创建此问题的最简单方法是简单地重构您的接口名称。它肯定会更改整个项目的实例名称,但无法更新 web.config 文件。要重新创建一个新服务,重命名你的界面并敲击 F5,繁荣,元数据对话框出现 :-) 答案如上,只记得手动修改你的 web.config 文件。

问候

【讨论】:

    【解决方案5】:

    听起来您需要添加元数据交换端点:

    http://en.csharp-online.net/WCF_Essentials%E2%80%94Metadata_Exchange

    【讨论】:

      【解决方案6】:

      默认情况下,Visual Studio 会尝试为您设置一个客户端/测试 ui 来玩弄您的新服务。为此,需要了解您的服务提供的结构和方法。这是通过使用标准 WSDL 格式的定义来实现的。但是,默认情况下,WCF 不会发布此数据。

      您必须设置 metadataexchange 端点行为才能实现。

      在此处发布您的配置文件,我们可以提供帮助,或者在 WCF 中使用 google/stack 搜索 metadataexchange 和 wsdl

      【讨论】:

        【解决方案7】:

        我收到此错误是因为我的服务名称错误。将 netTcpBinding 和 mexTcpBinding 与 httpGetEnabled = False 一起使用。

        【讨论】:

          【解决方案8】:
          // 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 重新创建它,这是可行的。

          【讨论】:

            【解决方案9】:

            如果您以编程方式创建服务主机并忘记将 [ServiceContract] 和 [OperationContract] 属性添加到接口协定,也会出现同样的错误。

            【讨论】:

              【解决方案10】:

              我知道这是一个老问题,但我想我会在这个问题上给我 2 美分,因为它刚刚发生在我身上。

              我以某种方式将 Web 服务本身的构建平台从“任何 CPU”更改为“x86”。第二次我把它改回“Any CPU”,它就解决了这个问题。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-10-27
                • 2011-12-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-09-12
                相关资源
                最近更新 更多