C#4.0 WCF REST JSON - HTTP GET CODE 400错误请求

use*_*261 10 rest wcf json

尝试创建一个简单的服务,通过以下几个教程返回一个简单的JSON字符串.我被两个不同的机器卡在了一个HTTP Statuscode 400错误请求上.示例教程使用JSON pt.1和pt.2的RESTful WCF服务 - http://www.youtube.com/watch?v=5BbDxB_5CZ8

我也有谷歌并在这里搜索(StackOverflow)类似的问题没有成功.

问题是我在尝试进行健全性检查以浏览到WCF服务并执行该方法时收到400错误请求.通过编译服务并浏览此地址:http:// localhost:49510/Service1.svc/GetPerson 就像教程一样.我试过找3天的解决方案.任何帮助表示赞赏.

这就是我的工作.

首先,我创建一个简单的WCF服务应用程序的新项目.我删除了默认的Service1.svc并添加了一个新的WCF服务,它生成了一个新的Service1.svc和一个IService1.cs

这是接口的代码(IService1.cs)

namespace WcfService1
{
    // 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", BodyStyle=WebMessageBodyStyle.Bare, ResponseFormat=WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json, UriTemplate="GetPerson")]
        Person GetPerson();
    }

    [DataContract(Name="Person")]
    public class Person
    {
        [DataMember(Name="name")]
        public string Name { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是Service1.svc的代码

namespace WcfService1
{
    // 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 Person GetPerson()
        {
            return new Person() { Name = "Tobbe" };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并且Web.config不受影响,看起来像这个web.config

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
Run Code Online (Sandbox Code Playgroud)

Aru*_*ana 14

对于REST WCF您必须在web.config中进行绑定和端点设置

通过以下方式替换整个web.config,它将起作用

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
 </configuration>
Run Code Online (Sandbox Code Playgroud)

你还剩下两件事

使用webHttpBinding(将默认http端口映射更改为webHttpBinding)

<system.serviceModel>
    <protocolMapping>
        <add scheme="http" binding="webHttpBinding"/>
    </protocolMapping>
    <behaviors>

<system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

指定webHttp End Point Behaviors

<system.serviceModel>
    -----
    </protocolMapping>
    <behaviors>
        <endpointBehaviors>
            <behavior>
                <webHttp />
            </behavior >
        </endpointBehaviors>
    <behaviors>
    ------
<system.serviceModel> 
Run Code Online (Sandbox Code Playgroud)


Rom*_*sse 5

您没有指定任何端点...默认情况下,在WCF 4上,将使用使用basicHttpBinding的端点.它在这里不起作用,因为它是一个基于SOAP的绑定.你想要使用的是webHttpBinding,这是基于REST的......

以下是如何使用WCF 4覆盖默认绑定:

<system.serviceModel>
  <protocolMapping>
    <add scheme="http" binding="webHttpBinding"/>
  </protocolMapping>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

您还必须通过在配置中添加此端点行为来启用webHttp:

<behaviors>
    <endpointBehaviors>
        <behavior>
            <webHttp />
        </behavior >
    </endpointBehaviors>
<behaviors>
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/bb924425.aspx