WCF REST服务不接受JSON在.NET 4中 [英] WCF REST Service not accepting JSON in .Net 4
问题描述
我一直在尝试了分别介绍.NET 4.0的一部分StandardEndpoints和我得到的最奇特的错误。
I've been trying out the StandardEndpoints that were introduced as part of .Net 4 and I'm getting the most peculiar of errors.
我的code
[ServiceContract]
public interface IAuthenticator
{
[OperationContract]
[WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
我的web.config
My web.config
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
这是推动我疯了例外!
415 Cannot process the message because the content type 'application/json' was not
the expected type 'text/xml; charset=utf-8'.
我可以让这个问题要追溯到宣布每个服务的。NET 3.5的标准去走,但是,如果我没记错的话,主要的升级,WCF与.NET 4中之一是它的能力,处理这样的东西。难道我做错了什么?
I can make the problem going away by going back to the .Net 3.5 standard of declaring each service, but, unless I am mistaken, one of the major upgrades in WCF with .Net 4 was it's ability to handle stuff like this. Am I doing something wrong?
推荐答案
如果我正确地阅读本经营合同,你定义了JSON是您的响应格式 - 但不是你的请求格式为:
If I read this operation contract correctly, you have defined JSON to be your response format - but not your request format:
[ServiceContract]
public interface IAuthenticator
{
[OperationContract]
[WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
ResponseFormat = WebMessageFormat.Json)]
AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
莫非是这个问题?如果添加,会发生什么 RequestFormat = WebMessageFormat.Json
您的运营合同?
[ServiceContract]
public interface IAuthenticator
{
[OperationContract]
[WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, <== ADD THIS HERE TO YOUR CODE !
ResponseFormat = WebMessageFormat.Json)]
AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
更新:如果您正在使用WCF 4开箱即用,它的协议映射将相关联的http://
方案与 basicHttpBinding的
。
Update: if you're using WCF 4 "out of the box", its protocol mapping will associate the http://
scheme with basicHttpBinding
.
要解决这个问题,你需要重写这样的默认协议映射(在你的web.config):
To fix this, you need to override the default protocol mapping like this (in your web.config):
默认协议映射
在这个问题的答案很简单。 WCF定义传输协议方案之间的默认协议映射(例如http,的net.tcp,net.pipe等)和内置的WCF绑定。默认的协议映射发现在.NET 4 machine.config.comments文件,它看起来是这样的:
The answer to this question is simple. WCF defines a default protocol mapping between transport protocol schemes (e.g., http, net.tcp, net.pipe, etc) and the built-in WCF bindings. The default protocol mapping is found in the .NET 4 machine.config.comments file and it looks like this:
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding" bindingConfiguration="" />
</protocolMapping>
现在,在默认情况下的http:// .....
将映射到的WebHttpBinding
Now, by default http://.....
would be mapped to webHttpBinding
.
(摘自:开发人员的介绍到Windows Communication Foundation的4 )
这篇关于WCF REST服务不接受JSON在.NET 4中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!