net 5 filter 异步版本


直接上代码

[TypeFilter(typeof(customAttribute))] public async Task<IActionResult>Index() { System.Console.WriteLine($"2222222222线程{Thread.CurrentThread.ManagedThreadId.ToString()}"); await Task.Run(() => { System.Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId.ToString()}"); Thread.Sleep(1000); _test.hello(); }); System.Console.WriteLine($"2222222222线程{Thread.CurrentThread.ManagedThreadId.ToString()}"); return Ok(); }

 

 






////
customAttribute 类


public class customAttribute:ActionFilterAttribute { public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Console.WriteLine($"111111{ next.Method.Name.ToString()}"); var areaName = context.RouteData.Values["area"] + "/"; var controllerName = context.RouteData.Values["controller"] + "/"; string action = context.RouteData.Values["Action"].ToString(); string currentUrl = areaName + controllerName + action; Console.WriteLine($"111111{ currentUrl}线程{Thread.CurrentThread.ManagedThreadId.ToString()}"); var resultContext = await next(); // resultContext.Result is set. Console.WriteLine($"33333{ currentUrl}线程{Thread.CurrentThread.ManagedThreadId.ToString()}"); } }

结果是 线程 可能是主线程也可能子线程

posted @ 2021-09-23 18:13  非著名架构师  阅读(47)  评论(0编辑  收藏  举报