错误:无法获取元数据;使用WCF测试客户端,C#,并试图实现的WebHttpBinding和JSON [英] Error: Cannot obtain Metadata; using WCF Test client, C#, and trying to implement webhttpbinding and json

查看:165
本文介绍了错误:无法获取元数据;使用WCF测试客户端,C#,并试图实现的WebHttpBinding和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到传说中的错误://本地主机:1640 /服务1无法从... 消息,尤其是 HTTP获取元数据。 SVC 我已经冲刷计算器和谷歌,到目前为止,还没有它的帮助。我创建了一个新的产品,以哑下来,它仍然没有工作。所以我在这里寻求帮助。



我试图安装使用JSON WCF服务,这意味着我需要使用的WebHttpBinding,在C#。当我使用WCF测试客户端,我得到上述错误的元数据。我使用Visual Studio Express的2010年目标框架。我已经在这两天不明白为什么还是有什么问题。



我将不胜感激任何帮助。谢谢。



下面是我的web.config文件:

 < ;? XML版本=1.0>?; 
<结构>

<&的System.Web GT;
<编译调试=真targetFramework =4.0/>
< /system.web>
< system.serviceModel>

<服务和GT;
<服务名称=WcfService4.Service1
behaviorConfiguration =jsonRestDefault>

<终点
NAME =jsonRestEndpoint
behaviorConfiguration =RESTFriendly
结合=的WebHttpBinding
合同=IService1
类地址=HTTP://本地主机:1640 / Service1.svc/>
< /服务>
< /服务>

<&行为GT;
< serviceBehaviors>
<行为NAME =jsonRestDefault>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =真/>
< /行为>
< / serviceBehaviors>
< endpointBehaviors>
<行为NAME =RESTFriendly>
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>

< serviceHostingEnvironment multipleSiteBindingsEnabled =真/>
< /system.serviceModel>
< system.webServer>
<模块runAllManagedModulesForAllRequests =真/>
< /system.webServer>

< /结构>

下面是IService1.cs文件:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.Text;

命名空间WcfService4
{
//注意:您可以使用重构菜单上的重命名命令代码和配置更改界面的名称IService1文件一起。
[的ServiceContract]
公共接口IService1
{
[OperationContract的]
[WebInvoke(
方法=GET,
UriTemplate =玩家,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
名单,LT;人> GetPlayers();
}

//使用数据合同下面的示例中所示复合类型添加到服务操作。
[DataContract]
公共类Person
{
[数据成员]
公共字符串名字{获得;组; }

[数据成员]
公共字符串名字{获得;组; }

[数据成员]
公众诠释年龄{搞定;组; }

公众人物(字符串名字,字符串姓氏,年龄INT)
{
this.FirstName =名字;
this.LastName = lastName的;
this.Age =年龄;
}
}
}

下面是Service1.svc cs文件:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.Text;使用System.Net
;

命名空间WcfService4
{
//注意:您可以使用重构菜单上的重命名命令来更改代码中的类名服务1,SVC和配置文件一起。
公共类服务:IService1
{
公开名单<&人GT; GetPlayers()
{
名单,LT;人>玩家=新的List<&人GT;();
players.Add(新的Person(佩顿,曼宁,35));
players.Add(新的Person(德鲁,易建联,31));
players.Add(新的Person(布雷特,法弗,38));

返回播放器;
}
}
}


解决方案

看看这个链接



它规定了以下内容:




的WebHttpBinding是一个基于REST的结合 - REST不公开
类似的元数据W​​SDL / XSD违背了SOAP。



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

解决方案

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.

这篇关于错误:无法获取元数据;使用WCF测试客户端,C#,并试图实现的WebHttpBinding和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆