【问题标题】:WCF json error handler causes exceptionWCF json错误处理程序导致异常
【发布时间】:2014-12-01 22:01:12
【问题描述】:

我在端点中配置了两种行为:

  1. 一个是用于json序列化的,它与示例here基本非常相似。 重要的是以下几点:
public class NewtonsoftJsonBehaviorExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(NewtonsoftJsonBehavior); }
    }

    protected override object CreateBehavior()
    {
        return new NewtonsoftJsonBehavior();
    }
}

public class NewtonsoftJsonContentTypeMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
}
  1. 另一个用于错误处理。这样当抛出异常时,将向客户端发送一条 json 格式的消息。代码取自here(答案以:“这是基于上面的一些信息的完整解决方案:”)。

当我只使用行为 1 时,一切正常。当我添加第二个行为时,我得到以下异常:

{"ExceptionType":"System.InvalidOperationException","Message":"The 传入消息具有意外的消息格式“原始”。预期的 该操作的消息格式为“Xml”、“Json”。这可以是 因为尚未在绑定上配置 WebContentTypeMapper。 有关详细信息,请参阅 WebContentTypeMapper 的文档。"}

这是我的 web.config 的样子:

<services>
      <service name="Algotec.Services.Archive.Data.ArchiveDataService" behaviorConfiguration="defaultBehavior">
        <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint name="restXml" address="" binding="webHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" behaviorConfiguration="restBehavior" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint name="restJson" address="json" binding="webHttpBinding" contract="Algotec.Interfaces.Archive.Data.IArchiveData" behaviorConfiguration="jsonBehavior" bindingConfiguration="jsonBinding" bindingNamespace="http://algotec.co.il/ArchiveData"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
      </service>
    </services>

...

    <endpointBehaviors>
            <behavior name="restBehavior">
              <enhancedWebHttp defaultOutgoingRequestFormat="Xml" defaultOutgoingResponseFormat="Xml"/>
            </behavior>
            <behavior name="jsonBehavior">
              <enhancedWebHttp defaultOutgoingRequestFormat="Json" defaultOutgoingResponseFormat="Json" helpEnabled="true"/>
              <newtonsoftJsonBehavior/>
              <jsonErrorBehavior/>
            </behavior>
          </endpointBehaviors>

    ...

        <extensions>
              <behaviorExtensions>
                <add name="newtonsoftJsonBehavior" type="Algotec.Services.Infra.BehaviorExtensions.NewtonsoftJsonBehaviorExtension, Algotec.Services.Infra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
                <add name="jsonErrorBehavior" type="Algotec.Services.Infra.Behaviors.JsonErrorWebHttpBehaviorElement, Algotec.Services.Infra, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
              </behaviorExtensions>
            </extensions>

有什么想法吗?

【问题讨论】:

    标签: json wcf ierrorhandler wcf-behaviour


    【解决方案1】:

    这是解决我的问题的方法:

    在 web.config 中,我只是在 &lt;newtonsoftJsonBehavior/&gt;&lt;jsonErrorBehavior/&gt; 之间切换了顺序。 我承认我并不完全理解所有这些行为,也不知道为什么它会有所帮助,但确实如此。

    【讨论】:

      【解决方案2】:

      为什么要从 NewtonsoftJsonContentTypeMapper 返回 WebContentFormat.Raw?您不应该返回 WebContentFormat.Json 以便格式正确匹配吗?

      你能澄清一下你想要完成什么吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-07
        • 2013-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多