【问题标题】:WCF REST Service not accepting JSON in .Net 4WCF REST服务不接受.Net 4中的JSON
【发布时间】:2011-02-15 16:12:00
【问题描述】:

我一直在尝试作为 .Net 4 的一部分引入的 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);
}

我的 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 的主要升级之一是它能够处理这样的事情.我做错了吗?

【问题讨论】:

  • 您是否尝试删除BodyStyle = WebMessageBodyStyle.WrappedRequest 属性?您如何托管 WFC 服务?你使用 .svc 吗?你在 .svc 文件中使用了哪个factory
  • 这被包装在一个没有工厂定义的 .svc 文件中。
  • 您应该尝试将 .svc 与工厂 "System.ServiceModel.Activation.WebServiceHostFactory" 一起使用,或者在 web.config 中包含其他部分,在没有 .svc 文件的情况下执行相同的操作。有关详细信息,请参阅stackoverflow.com/questions/4680162/host-wcf-in-mvc2-site/…。此外,如果您在描述的错误消息之后包含客户端代码(jQuery.ajax 或您使用的代码),它可能会有所帮助。
  • 你有解决问题的进展吗?

标签: .net wcf json


【解决方案1】:

如果我正确阅读了这份操作合同,您已将 JSON 定义为您的 response 格式 - 但不是您的 request 格式:

[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 相关联。

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

默认协议映射

这个问题的答案很简单。 WCF 定义了传输协议方案(例如,http、net.tcp、net.pipe 等)和内置 WCF 绑定之间的默认协议映射。默认协议映射位于 .NET 4 machine.config.cmets 文件中,如下所示:

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

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

(取自:A Developer's Introduction to Windows Communication Foundation 4

【讨论】:

  • 我实际上在早期版本的代码中就是这样,但它仍然在解决同样的问题。让我感到沮丧的是,当我在 web.config 中明确声明服务时,一切正常。
  • @thaBadDawg:嗯,.NET 4 有那些标准端点——不过,http:// 方案的默认映射是使用basicHttpBinding。因此,除非您更改了该映射,否则当您使用 http://somethingoranother....... 地址时,WCF 4 将默认为 basicHttpBinding - not webHttpBinding
  • 当我切换到使用协议映射时,我得到了这个异常:由于 EndpointDispatcher 的 AddressFilter 不匹配,接收方无法处理带有 To 'localhost/TokenAuthProvider/Authenticator.svc/AuthenticateUser' 的消息。检查发送方和接收方的 EndpointAddresses 是否一致。
  • 这似乎对我不起作用。现在获得 500 而不是 415。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-19
  • 2020-11-16
  • 1970-01-01
  • 2011-10-18
  • 1970-01-01
相关资源
最近更新 更多