WCF REST服务不接受.Net 4中的JSON

tha*_*awg 9 .net wcf json

我一直在尝试引入作为.NET 4.0的一部分,该StandardEndpoints和我得到的最奇特的错误.

我的代码

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
    AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

这个例外让我发疯!

415 Cannot process the message because the content type 'application/json' was not 
the expected type 'text/xml; charset=utf-8'.
Run Code Online (Sandbox Code Playgroud)

我可以通过回到声明每项服务的.Net 3.5标准来解决这个问题,但是,除非我弄错了,WCF与.Net 4的主要升级之一是能够处理这样的事情.难道我做错了什么?

mar*_*c_s 7

如果我正确阅读了此操作合同,您已将JSON定义为您的响应格式 - 但不是您的请求格式:

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", 
     BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     ResponseFormat = WebMessageFormat.Json)]
    AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
Run Code Online (Sandbox Code Playgroud)

这可能是问题吗?如果你加入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);
}
Run Code Online (Sandbox Code Playgroud)

更新:如果您使用WCF 4"开箱即用",其协议映射将与该http://方案关联basicHttpBinding.

要解决此问题,您需要覆盖这样的默认协议映射(在您的web.config中):

默认协议映射

这个问题的答案很简单.WCF定义传输协议方案(例如,http,net.tcp,net.pipe等)与内置WCF绑定之间的默认协议映射.默认协议映射可在.NET 4 machine.config.comments文件中找到,它看起来像这样:

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

现在,默认情况下http://.....将映射到webHttpBinding.

(摘自:开发人员对Windows Communication Foundation 4的介绍)

  • 当我切换到使用协议映射时,我得到了这个例外:由于EndpointDispatcher上的AddressFilter不匹配,无法在接收器处处理To'http://localhost/TokenAuthProvider/Authenticator.svc/AuthenticateUser'的消息.检查发送方和接收方的EndpointAddresses是否一致. (2认同)