简体   繁体   English

WCF JSON错误处理程序导致异常

[英]WCF json error handler causes exception

I have two behaviors configured in my endpoint: 我在端点中配置了两种行为:

  1. One is for json serialization which is basically very similar to the exmaple here . 一种是用于json序列化,基本上与此处的示例非常相似。 What's important there is the following: 重要的是以下内容:
 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. The other is for error handling. 另一个用于错误处理。 So that when exception is thrown a json formatted message will be sent to the client. 这样,当引发异常时,就会将json格式的消息发送到客户端。 The code is taken from here (The answer staring with: "Here's a complete solution based on some info from above:"). 代码是从这里获取的 (答案盯着:“这是一个基于以上信息的完整解决方案:”)。

when I use only behavior 1 everything works fine. 当我仅使用行为1时,一切正常。 When I add the second behavior I get the following exception: 当我添加第二个行为时,出现以下异常:

{"ExceptionType":"System.InvalidOperationException","Message":"The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details."} {“ ExceptionType”:“ System.InvalidOperationException”,“ Message”:“传入的消息具有意外的消息格式“ Raw”。该操作的预期消息格式为“ Xml”,“ Json”。这可能是因为WebContentTypeMapper尚未在绑定上配置。请参阅WebContentTypeMapper的文档以获取更多详细信息。“}

Here is how my web.config looks like: 这是我的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>

Any ideas? 有任何想法吗?

Why are you returning WebContentFormat.Raw from NewtonsoftJsonContentTypeMapper? 为什么要从NewtonsoftJsonContentTypeMapper返回WebContentFormat.Raw? Shouldn't you be returning WebContentFormat.Json so that the format matches correctly? 您不应该返回WebContentFormat.Json以便正确匹配格式吗?

Can you clarify a bit what you're trying to accomplish? 您能否澄清一下您要完成的工作?

Here is what solved my problem: 这是解决我的问题的方法:

In the web.config I simply switched the order between <newtonsoftJsonBehavior/> and <jsonErrorBehavior/> . 在web.config中,我只是在<newtonsoftJsonBehavior/><jsonErrorBehavior/>之间切换了顺序。 I admit that I don't understand completely all this behaviors and don't know why it helped but it did. 我承认我不完全理解所有这些行为,也不知道为什么这样做有帮助,但确实有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM