2015-07-20 56 views
0

我正在使用帶有自定義異常的Agatha框架。我有一個例外,MyException,這我指定爲BusinessExceptionType當我配置我的要求:使用Agatha框架進行異常處理(BusinessExceptionType)

new ServiceLayerConfiguration(Assembly.GetExecutingAssembly(), typeof(SomeRequestResponse.MakeARequest).Assembly, typeof(Container)) { BusinessExceptionType = typeof(MyException) }.Initialize(); 

這是怎麼了拋出該異常:但是

throw new MyException(message); 

當異常被拋出, ,MyException未被識別爲BusinessExceptionType。我如何才能將其識別爲BusinessExceptionType?以下情況並非如此。

if (response.ExceptionType == ExceptionType.Business) 

這裏是異常類:

public class MyException : Exception 
{ 
    public MyException() 
    { 

    } 

    public MyException(string message) : base(message) 
    { 

    } 

    public MyException(string message, Exception inner) : base(message, inner) 
    { 

    } 

    protected MyException(SerializationInfo info, StreamingContext context) : base(info, context) 
    { 

    } 
} 

UPDATE: 它出現在BusinessExceptionTypeserviceLayerConfiguration設置正確,直到我把我的第一個請求時,BusinessExceptionType變得null。在配置請求和提出我的第一個請求之間的某個地方,它變成了null。要麼,要麼在RequestProcessor.cs中抓取不同的serviceLayerConfiguration。任何幫助,這是表示讚賞。

回答

0

我發現你必須在拋出它之前在響應中設置異常類型。

return new SomeRequestResponse.MakeAResponse() { Exception = new Exception(ex), ExceptionType = ExceptionType.Business }; 

然後在你的消費這種說法將是正確的:

return new MakeARequestResponse.MakeAResponse() 
{ 
    Exception = new ExceptionInfo(new Exception("Whatever you want to say.")), 
    ExceptionType = ExceptionType.Business 
}; 

if (response.ExceptionType == ExceptionType.Business) 

您還可以,如果你想使自己的異常做到這一點