特定の戻り値の型でアクションをラップする fubumvc 動作を作成するにはどうすればよいですか? アクションの実行中に例外が発生した場合、動作は例外をログに記録し、戻りオブジェクトのいくつかのフィールドにデータを入力しますか? 私は次のことを試しました:
public class JsonExceptionHandlingBehaviour : IActionBehavior
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
private readonly IActionBehavior _innerBehavior;
private readonly IFubuRequest _request;
public JsonExceptionHandlingBehaviour(IActionBehavior innerBehavior, IFubuRequest request)
{
_innerBehavior = innerBehavior;
_request = request;
}
public void Invoke()
{
try
{
_innerBehavior.Invoke();
var response = _request.Get<AjaxResponse>();
response.Success = true;
}
catch(Exception ex)
{
logger.ErrorException("Error processing JSON request", ex);
var response = _request.Get<AjaxResponse>();
response.Success = false;
response.Exception = ex.ToString();
}
}
public void InvokePartial()
{
_innerBehavior.InvokePartial();
}
}
しかし、AjaxResponseリクエストからオブジェクトを取得しても、行った変更はクライアントに送り返されません。また、アクションによってスローされた例外は、これまでのところ成功しません。実行が catch ブロックに到達する前に、リクエストが終了します。私は何を間違っていますか?
完全を期すために、動作は WebRegistry で次のように接続されています。
Policies
.EnrichCallsWith<JsonExceptionHandlingBehaviour>(action =>
typeof(AjaxResponse).IsAssignableFrom(action.Method.ReturnType));
AjaxResponse は次のようになります。
public class AjaxResponse
{
public bool Success { get; set; }
public object Data { get; set; }
public string Exception { get; set; }
}