簡體   English   中英

IActionContextAccessor 為 Null

[英]IActionContextAccessor Is Null

我正在為我的 .NET 核心應用程序設置一個自定義中間件來記錄異常錯誤,並在 Startup.cs 中有以下內容來注冊我的上下文:

   services.AddHttpContextAccessor();
   services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

在我的配置中,我添加了以下內容以使用自定義中間件:

   app.UseMiddleware<CustomMiddleware>();

我的自定義中間件 class 如下:

 public class CustomMiddleware
 {
    private readonly RequestDelegate next;

    public CustomMiddleware(RequestDelegate next)
    {
        this.next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        try
        {
            this.BeginInvoke(context);
            await this.next.Invoke(context);
            this.EndInvoke(context);
        }
        catch(Exception ex)
        {
            //Capture the exception.
            string hostName = Environment.MachineName;
            string url = StringFunctions.getCurrentUrl(context);
            string userName = string.Empty;
            string controllerName = string.Empty;
            string actionName = string.Empty;

            //THIS IS NULL BELOW.
            IActionContextAccessor contextAccessor = context.RequestServices.GetService(typeof(IActionContextAccessor)) as IActionContextAccessor;
            RouteData routeData = contextAccessor.ActionContext.RouteData;

            if (context.User != null)
            {
                userName = context.User.Identity.Name;
            }

            if (routeData != null && routeData.Values != null)
            {
                controllerName = routeData.Values["controller"].ToString();
                actionName = routeData.Values["action"].ToString();
            }

            EventLogging.LogApplicationError(hostName, controllerName, actionName, url, userName, ex);
        }
    }

    private void BeginInvoke(HttpContext context)
    {
        //custom work here
    }

    private void EndInvoke(HttpContext context)
    {
        // Do custom work after controller execution
    }
}

我試圖從中獲取 ActionContext.RouteData 值的“contextAccessor”似乎是 Null。

我究竟做錯了什么? 謝謝你。

IActionContextAccessor.ActionContext是mvc中間件的scope之外的null。

如果您在異常處理程序中只關心MVC操作,那么您可以創建一個操作過濾器而不是中間件來捕獲和記錄異常。

public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
    public override void OnException(ExceptionContext context)
    {
        //Capture the exception.
        string hostName = Environment.MachineName;
        string url = StringFunctions.getCurrentUrl(context.HttpContext);
        string userName = string.Empty;
        string controllerName = string.Empty;
        string actionName = string.Empty;

        if (context.HttpContext.User != null)
        {
            userName = context.HttpContext.User.Identity.Name;
        }

        controllerName = context.RouteData.Values["controller"].ToString();
        actionName = context.RouteData.Values["action"].ToString();

        EventLogging.LogApplicationError(hostName, controllerName, actionName, url, userName, ex);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM