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

2024-02-10

我得到了传说中的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;
        }
    }
}

看看这个link https://stackoverflow.com/questions/3040165/get-metadata-out-of-a-webhttpbinding-endpoint

它规定如下:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐