2014-10-07 28 views
0

我有我的端點配置了兩個行爲:WCF JSON錯誤處理程序引起了異常

  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; 
    } 
} 
  • 另一種是對錯誤處理。所以,當拋出異常時,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> 
    

    任何想法?

    回答

    0

    爲什麼從NewtonsoftJsonContentTypeMapper返回WebContentFormat.Raw?你不應該返回WebContentFormat.Json,以便格式正確匹配嗎?

    你能澄清一下你試圖完成什麼嗎?

    +0

    我試圖將其更改爲json,但它沒有奏效。 – 2014-10-08 05:11:26

    0

    這裏是解決我的問題:

    在web.config我簡單地切換<newtonsoftJsonBehavior/><jsonErrorBehavior/>之間的順序。 我承認我不完全理解所有這些行爲,不知道它爲什麼會有所幫助,但它確實如此。