当尝试通过遵循几个教程创建一个简单的服务以返回简单的 JSON 字符串时。我被困在两台不同的机器上,并收到 HTTP Statuscode 400 错误请求。 示例教程 使用 JSON 的 RESTful WCF 服务 pt.1 和 pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8
我也在 Google 上搜索过类似的问题,但没有成功。
问题是,当我尝试进行健全性检查以浏览 WCF 服务并执行该方法时,收到 400 错误请求。通过编译服务并浏览这个地址:http://localhost:49510/Service1.svc/GetPerson 就像教程一样。我已经尝试寻找解决方案大约三天了。如有任何帮助,我们将不胜感激。
这就是我所做的。
首先,我创建一个新项目,一个简单的 WCF 服务应用程序。我删除默认的 Service1.svc 并添加一个新的 WCF 服务,生成新的 Service1.svc 和 IService1.cs
这是接口(interface)的代码(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; }
}
}
这是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" };
}
}
}
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>
最佳答案
对于 REST WCF,您必须在 web.config 中进行绑定(bind)和端点设置
通过以下方式替换整个 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>
您还剩下以下两件事
使用 webHttpBinding(将默认 http 端口映射更改为 webHttpBinding)
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
<system.serviceModel>
指定 webHttp 端点行为
<system.serviceModel>
-----
</protocolMapping>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior >
</endpointBehaviors>
<behaviors>
------
<system.serviceModel>
关于wcf - C# 4.0 WCF REST JSON - HTTP GET 代码 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8428010/