我有我的端點配置了兩個行爲:WCF JSON錯誤處理程序引起了異常
- 一個是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; } }
- 另一種是對錯誤處理。所以,當拋出異常時,json格式的消息將被髮送到客戶端。代碼取自here(答案盯着:「這是一個基於上面一些信息的完整解決方案:」)。
當我只使用行爲1一切工作正常。當我添加第二個問題,我得到以下異常:
{「ExceptionType」:「System.InvalidOperationException」,「消息」:「在 傳入的消息有意想不到的消息格式的‘原始’的預期 對於操作消息格式「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,但它沒有奏效。 – 2014-10-08 05:11:26