如何创建异常包装 fubumvc 行为?

发布于 2024-08-26 07:51:34 字数 1627 浏览 6 评论 0原文

如何创建一个 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; }
}

How can I create a fubumvc behaviour that wraps actions with a particular return type, and if an exception occurs while executing the action, then the behaviour logs the exception and populates some fields on the return object? I have tried the following:

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();
    }
}

But, although I get the AjaxResponse object from the request, any changes I make don't get sent back to the client. Also, any exceptions thrown by the action don't make it as far as this, the request is terminated before execution gets to the catch block. What am I doing wrong?

For completeness, the behaviour is wired up with the following in my WebRegistry:

Policies
    .EnrichCallsWith<JsonExceptionHandlingBehaviour>(action =>
        typeof(AjaxResponse).IsAssignableFrom(action.Method.ReturnType));

And AjaxResponse looks like:

public class AjaxResponse
{
    public bool Success { get; set; }
    public object Data { get; set; }
    public string Exception { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-09-02 07:51:34

不幸的是,这不适用于当前版本的 FubuMVC。原因是对 _innerBehavior.Invoke() 的调用将继续执行行为链的其余部分,包括将 JSON 输出渲染回客户端的调用。

使用当前的 FubuMVC,您无法在执行渲染输出行为之前包装行为并修改其输出。

我刚刚与 Jeremy (Miller) 讨论了这一点,我们将向 FubuMVC 添加此功能,以便您可以包装特定行为(在本例中为调用操作的行为),而不是包装整个行为链(其中就是你现在正在发生的事情)。

当我们添加此功能后,我将在这里评论这个 StackOverflow 问题。

This won't work with the current version of FubuMVC, unfortunately. The reason is that the call to _innerBehavior.Invoke() will proceed down the rest of the behavior chain including the call to render the JSON output back to the client.

With current FubuMVC, you can't wrap a behavior and modify its output before the render output behavior is executed.

I just talked with Jeremy (Miller) about this and we're going to add this capability to FubuMVC so that you can wrap a specific behavior (in this case, the behavior that calls the action) instead of wrapping the entire behavior chain (which is what's happening to you right now).

I'll comment back here on this StackOverflow question when we have added this feature.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文