【问题标题】:Error: Cannot obtain Metadata; using WCF Test client, C#, and trying to implement webhttpbinding and json错误:无法获取元数据;使用 WCF 测试客户端、C#,并尝试实现 webhttpbinding 和 json
【发布时间】:2013-06-08 20:54:21
【问题描述】:

我收到了传说中的 Error: Cannot obtain Metadata from... 消息,尤其是 http://localhost:1640/Service1.svc 我已经搜索了 stackoverflow 和 Google,但到目前为止,没有任何帮助。我创建了一个新产品来使它变得笨拙,但它仍然无法正常工作。所以我在这里寻求帮助。

我正在尝试设置一个使用 JSON 的 WCF 服务,这意味着我需要在 C# 中使用 webhttpbinding。当我使用 WCF 测试客户端时,出现上述元数据错误。我正在使用 Visual Studio Express 2010 和目标框架。我已经在这两天了,不明白为什么或什么问题。

我将不胜感激。谢谢你。

这是我的 web.config 文件:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="WcfService4.Service1"
        behaviorConfiguration="jsonRestDefault">

        <endpoint 
              name="jsonRestEndpoint"
              behaviorConfiguration="RESTFriendly"
              binding="webHttpBinding"
              contract="IService1"
              address="http://localhost:1640/Service1.svc"/>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="jsonRestDefault">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="RESTFriendly">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

这是 IService1.cs 文件:

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

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(
            Method = "GET", 
            UriTemplate = "players",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Bare)]
        List<Person> GetPlayers();
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class Person
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public int Age { get; set; }

        public Person(string firstName, string lastName, int age)
        {
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;
        }
    }
}

这是 Service1.svc.cs 文件:

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

namespace WcfService4
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public List<Person> GetPlayers()
        {
            List<Person> players = new List<Person>();
            players.Add(new Person ( "Peyton", "Manning", 35 ) );
            players.Add(new Person ( "Drew", "Brees", 31 ) );
            players.Add(new Person ( "Brett", "Favre", 38 ) );

            return players;
        }
    }
}

【问题讨论】:

  • 您在使用客户端跟踪实用程序吗?
  • 我不这么认为。我对 WCF 有点陌生。我在哪里可以获得客户端跟踪实用程序?

标签: c# json wcf metadata webhttpbinding


【解决方案1】:

除了 bruno bologna 的回答和链接,WCF REST Service not visible in WCFTestClient 的链接也提供了一些非常有用的信息。基本上,它不起作用的原因是 WCFTestClient 不是为 Web 设计的(想想 JSON)。它通过 SOAP 连接。如果我有一个依赖于 JSON 的服务,我无法通过 WCFTestClient 对其进行测试。

我看到可以修改 WCFTestClient 中的客户端配置文件以启用 Web 绑定,但这可能是因为 WADL 的未来证明或者如果有人编写了 WADL 服务扩展。不过,这只是我的猜测。否则,似乎无法使用 WCFTestClient 工具使用 JSON 测试 WCF 服务。

【讨论】:

    【解决方案2】:

    看看这个link

    它声明如下:

    WebHttpBinding 是基于 REST 的绑定 - REST 不公开 WSDL/XSD 等元数据与 SOAP 相悖。

    【讨论】:

    • 非常感谢您提供的信息和链接。我不是要公开元数据;坦率地说,我不太关心元数据,因为它与我的目的无关。我要做的就是设置一个使用 JSON,而不是 SOAP 和/或 XML 的 WCF 服务。这个错误只是碍事。再次感谢。
    • 好吧,我找到了一篇不错的帖子link,它解释了如何使用 wcf 创建一个 rest 服务并将其发布到 iis 上。但是,如果您想配置响应和响应的格式,您应该在 OperationContract 的 WebInvoke 或 WebGet 属性上指定它。例如[WebGet(UriTemplate = "MyTemplate", ResponseFormat = WebMessageFormat.Json ), RequestFormat = WebMessageFormat.Json]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 2015-12-18
    • 1970-01-01
    相关资源
    最近更新 更多