当尝试通过遵循几个教程创建一个简单的服务以返回简单的 JSON 字符串时。我被困在两台不同的机器上,并收到 HTTP Statuscode 400 错误请求。 示例教程 使用 JSON 的 RESTful WCF 服务 pt.1 和 pt.2 - http://www.youtube.com/watch?v=5BbDxB_5CZ8
我也谷歌并在这里(StackOverflow)搜索类似的问题,但没有成功。
问题是我在尝试进行健全性检查以浏览 WCF 服务并执行该方法时收到 400 错误请求。通过编译服务并浏览这个地址:http://localhost:49510/Service1.svc/GetPerson 就像教程一样。我已经尝试寻找解决方案大约三天了。如有任何帮助,我们将不胜感激。
这就是我所做的。
首先我创建一个新项目,一个简单的 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; }
}
}
这是 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 中进行绑定和端点设置
按照以下方式替换整个 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 4 上,将使用使用 basicHttpBinding 的端点。它在这里不起作用,因为它是基于 SOAP 的绑定。您想要使用的是基于 REST 的 webHttpBinding...
以下是如何使用 WCF 4 覆盖默认绑定:
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
</system.serviceModel>
您还必须通过在配置中添加此端点行为来启用 webHttp :
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior >
</endpointBehaviors>
<behaviors>
我不完全确定为什么,但是当我将“Factory”属性添加到我的 .SVC 文件中(您需要将其显式拖动到 Visual Studio)时,一切都正常工作 - 无需对默认设置进行任何更改网络配置! 我添加了
Factory="System.ServiceModel.Activation.WebServiceHostFactory"所以我的 .SVC 文件是这样的:
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" %>
对此:
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceNameSpace.ServiceName" CodeBehind="ServiceName.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
唯一的副作用似乎是,当您在浏览器中单击 .SVC 文件时,您会收到“未找到端点”错误,但无论如何,当您正确调用它时,该服务都可以正常工作。如前所述,我使用的是 .NET 4.6 的默认 Web.config(简化的 WCF 配置