简体   繁体   English

错误:无法获取元数据; 使用WCF测试客户端,C#,并尝试实现webhttpbinding和json

[英]Error: Cannot obtain Metadata; using WCF Test client, C#, and trying to implement webhttpbinding and json

I'm getting the fabled Error: Cannot obtain Metadata from... message, particularly http://localhost:1640/Service1.svc I've scoured stackoverflow and Google and, so far, none of it has helped. 我得到了传说中的Error: Cannot obtain Metadata from...消息中Error: Cannot obtain Metadata from... ,尤其是http://localhost:1640/Service1.svc我已经搜索了stackoverflow和谷歌,到目前为止,它都没有帮助。 I created a new product to dumb it down and it's still not working. 我创造了一个新产品,使其愚蠢,它仍然无法正常工作。 So I am here asking for help. 所以我在这里寻求帮助。

I'm trying to setup a WCF Service that uses JSON, which means I need to use webhttpbinding, in C#. 我正在尝试设置一个使用JSON的WCF服务,这意味着我需要在C#中使用webhttpbinding。 When I use the WCF Test Client, I get the aforementioned metadata error. 当我使用WCF测试客户端时,我得到上述元数据错误。 I'm using Visual Studio Express 2010 and the target framework. 我正在使用Visual Studio Express 2010和目标框架。 I've been at this for two days and cannot understand why or what is the problem. 我已经在这两天了,无法理解为什么或者问题是什么。

I would appreciate any help. 我将不胜感激任何帮助。 Thank you. 谢谢。

Here is my web.config file: 这是我的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>

Here is the IService1.cs file: 这是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;
        }
    }
}

Here is the Service1.svc.cs file: 这是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;
        }
    }
}

Take a look at this link 看看这个链接

It states the following: 它陈述如下:

WebHttpBinding is a REST-based binding - REST does not expose metadata like WSDL/XSD contrary to SOAP. WebHttpBinding是一个基于REST的绑定 - 与SOAP相反,REST不会像WSDL / XSD那样公开元数据。

In addition to bruno bologna's answer and link, the link at WCF REST Service not visible in WCFTestClient also provides some very useful information. 除了bruno bologna的答案和链接之外, WCFTestClient中不可见的WCF REST服务链接也提供了一些非常有用的信息。 Basically, the reason why it's not working is the WCFTestClient is not designed for web (think JSON). 基本上,它不起作用的原因是WCFTestClient不是为web设计的(想想JSON)。 It hooks in through SOAP. 它通过SOAP挂钩。 If I have a service that is dependent on JSON, I cannot test it through the WCFTestClient. 如果我有一个依赖于JSON的服务,我无法通过WCFTestClient测试它。

I see that it is possible to modify the client configuration file in WCFTestClient to enable web binding, but that may be because of future-proofing for WADL or if someone writes a WADL service extension. 我看到可以在WCFTestClient中修改客户端配置文件以启用Web绑定,但这可能是因为WADL的未来验证或者有人写了WADL服务扩展。 That's just specultation on my part, though. 不过,这只是我个人的鉴定。 Otherwise, it doesn't appear one can test WCF Service with JSON using the WCFTestClient tool. 否则,似乎没有人可以使用WCFTestClient工具使用JSON测试WCF服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM