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

Error: Cannot obtain Metadata; using WCF Test client, C#, and trying to implement webhttpbinding and json
2021-07-31
  •  译文(汉语)
  •  原文(英语)

我得到了寓言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;
        }
    }
}
速聊1:
您是否正在使用客户端跟踪实用程序?
速聊2:
我不相信 我是WCF的新手.我在哪里可以获得客户端跟踪实用程序?
解决过程1

看看这个链接

它指出以下内容:

WebHttpBinding是基于REST的绑定-与SOAP相反,REST不会公开像WSDL / XSD这样的元数据.

速聊1:
非常感谢您提供的信息和链接.我不是要公开元数据;直言不讳,我对元数据一点也不在乎,因为它与我的目的无关.我要做的只是设置一个使用JSON而不是SOAP和/或XML的WCF服务.此错误正好在路上.再次感谢.
速聊2:
好吧,我找到了一个不错的帖子链接,该链接解释了如何使用wcf创建rest服务并将其发布在iis上.但是,如果要配置响应和响应的格式,则应在OperationContract的WebInvoke或WebGet属性中指定它.例如 [WebGet(UriTemplate = "MyTemplate", ResponseFormat = WebMessageFormat.Json ), RequestFormat = WebMessageFormat.Json]
解决过程2

除了bruno bologna的答案和链接之外,WCFTestClient中不可见的WCF REST Service上的链接还提供了一些非常有用的信息.基本上,它不起作用的原因是WCFTestClient不是为Web设计的(请考虑JSON).它通过SOAP连接.如果我有依赖于JSON的服务,则无法通过WCFTestClient对其进行测试.

我看到可以在WCFTestClient中修改客户端配置文件以启用Web绑定,但这可能是因为WADL的面向未来能力或有人编写了WADL服务扩展.不过,那只是我的投机行为.否则,似乎无法使用WCFTestClient工具使用JSON测试WCF服务.

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. 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#. When I use the WCF Test Client, I get the aforementioned metadata error. I'm using Visual Studio Express 2010 and the target framework. 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:

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

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:

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;
        }
    }
}
Talk1:
Are you using the client trace utility?
Talk2:
I don't believe so. I'm a little new to WCF. Where would I obtain the client trace utility?
Solutions1

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.

Talk1:
Thank you very much for the info and link. I'm not trying to expose meta data; to be blunt, I couldn't care less about meta data, as it's irrelevant for my purpose. All I want to do is setup a WCF service that uses JSON, not SOAP and/or XML. This error is just in the way. Thanks again.
Talk2:
Well, I found a nice post link that explains how to create a rest service with wcf and publish it on iis. But, if you want to configure the format in which responses and responses are being made, you should specify it on the WebInvoke or WebGet attribute of your OperationContract. For instance [WebGet(UriTemplate = "MyTemplate", ResponseFormat = WebMessageFormat.Json ), RequestFormat = WebMessageFormat.Json]
Solutions2

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. Basically, the reason why it's not working is the WCFTestClient is not designed for web (think JSON). It hooks in through SOAP. If I have a service that is dependent on JSON, I cannot test it through the 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. That's just specultation on my part, though. Otherwise, it doesn't appear one can test WCF Service with JSON using the WCFTestClient tool.

转载于:https://stackoverflow.com/questions/17070677/error-cannot-obtain-metadata-using-wcf-test-client-c-and-trying-to-implemen

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入