2011-04-11 117 views
2

我在使用WCF 4.0 RESTful服務一個令人興奮的問題。我試圖做一個REST服務,將返回,在出現錯誤的情況下,一個XML文檔描述問題 例如:WCF休息錯誤處理

<ErrorHandler> 
     <cause>Resource not available</cause> 
     <errorCode>111103</errorCode> 
    </ErrorHandler> 

爲了使這個我已經創建使用模板默認REST服務由Visual Studio提供 這裏是我的服務類:

public class Service1 
    { 
     // TODO: Implement the collection resource that will contain the SampleItem instances 

     [WebGet(UriTemplate = "")]   
     public List<SampleItem> GetCollection() 
     { 
      // TODO: Replace the current implementation to return a collection of SampleItem instances\ 
      // throw new WebException("lala"); 
      throw new WebFaultException<ErrorHandler>(new ErrorHandler { cause = "Resource not available", errorCode = 100 }, System.Net.HttpStatusCode.NotFound); 
      //return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } }; 
     } 

     [WebInvoke(UriTemplate = "", Method = "POST")] 
     public SampleItem Create(SampleItem instance) 
     { 
      // TODO: Add the new instance of SampleItem to the collection   
      return new SampleItem() { Id = 3, StringValue = "59" }; 

     } 

     [WebGet(UriTemplate = "{id}")] 
     public SampleItem Get(string id) 
     { 
      // TODO: Return the instance of SampleItem with the given id 
      throw new NotImplementedException(); 
     } 

     [WebInvoke(UriTemplate = "{id}", Method = "PUT")] 
     public SampleItem Update(string id, SampleItem instance) 
     { 
      // TODO: Update the given instance of SampleItem in the collection 
      throw new NotImplementedException(); 
     } 

     [WebInvoke(UriTemplate = "{id}", Method = "DELETE")] 
     public void Delete(string id) 
     { 
      // TODO: Remove the instance of SampleItem with the given id from the collection 
      throw new NotImplementedException(); 
     } 

    } 
} 

正如你可以從代碼中看到上面我在GetCollection方法扔WebFaultException。應該在響應的主體中放入一個「ErrorHandler」對象。 這裏是我的ErrorHandler類看起來像:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Runtime.Serialization; 

namespace WcfRestService1 
{ 
    [DataContract] 
    public class ErrorHandler 
    { 
     [DataMember] 
     public int errorCode { get; set; } 
     [DataMember] 
     public string cause { get; set; } 

    } 
} 

瘋狂的事情是,這個事情的作品,但它down't :))。我想說的是,視覺工作室給我一個錯誤,說WebFaultException不被用戶代碼捕獲:並暫停我的應用程序。如果我按繼續,一切正常,因爲它應該。 下面是一些圖片說明我的問題:

在提琴手

第一步: First Step

下一個Visual Studio的錯誤:Visual Studio Error

最後按下繼續後,一切都工作:

這是沒有意義的我和我不知道爲什麼這件事正在發生,以及如何解決它:P。我在網上搜索了幾天,試圖找到一個沒有運氣的解決方案。我使用Visual Studio 2010的最終

最好的問候:)

回答

3

沒有什麼是錯在這裏。你正在調試,拋出異常,中斷,你繼續,它應該像它應該的那樣工作。

我懷疑你已經設置了異常處理選項(按Ctrl + Alt + E)時拋出異常打破。 (選項中的「Thrown」)無論何時處理異常,都會導致中斷。了在WCF操作拋出

例外將由WCF運行時進行處理,並且如果它們的故障,從而使信道沒有故障,他們將被送回作爲這樣。

現在對於發回的XML,你可以發送郵件使用WebFaultException<string>的XML的字符串表示。

+0

非常感謝您的回答。目前還不清楚爲什麼VS 2010將這個例外報告爲未處理。 WCF運行時是否捕獲到異常?據我瞭解,除應由運行時捕獲,並且和「原因」對象將被序列化,並與指定的HTTP代碼返回給客戶端。奇怪的是,2008年的比賽並不像這樣。我曾經通過拋出Web ProtocolException來報告錯誤,並且一切都很好(除了客戶端不會收到xml的事實)。在升級到vs 2010之後,發生了這種情況。 – Andrei 2011-04-12 15:43:11

+0

我從來沒有與2008年合作,所以我不知道,但我不會太多掛在這一點。只要你的網站有效就好。但正如我所說,按Ctrl + Alt + E查看你的VS設置。 – Aliostad 2011-04-12 15:48:20

+0

我會這樣做的。但我想要一個更便攜的解決方案。我不是唯一一個在這個項目上工作的人,並且要求我的團隊中的每個人都使用這種解決方法並不可行:) – Andrei 2011-04-12 16:41:37