[英]WCF json error handler causes exception
I have two behaviors configured in my endpoint: 我在端点中配置了两种行为:
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; } }
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.