WCF json错误处理程序导致异常
我在端点中配置了两种行为:WCF json错误处理程序导致异常,json,wcf,ierrorhandler,wcf-behaviour,Json,Wcf,Ierrorhandler,Wcf Behaviour,我在端点中配置了两种行为: 一种是json序列化,它基本上与exmaple非常相似。 重要的是以下几点: 另一个是错误处理。因此,当抛出异常时,将向客户端发送一条json格式的消息。代码取自(答案开头是:“根据上面的一些信息,这里有一个完整的解决方案:”) 当我只使用行为1时,一切正常。当我添加第二个行为时,我得到以下异常: {“ExceptionType”:“System.InvalidOperationException”,“Message”:“ 传入消息具有意外的消息格式“Raw”。应为
<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>
...
...
有什么想法吗?为什么要从NewtonsoftJsonContentTypeMapper返回WebContentFormat.Raw?您不应该返回WebContentFormat.Json以便格式正确匹配吗
你能澄清一下你想完成什么吗?以下是解决我问题的方法: 在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>