asp.net Core 异步程序改造遇到的问题

在公众号中看到关于提高netCore程序性能的文章,说到要用到异步编程的思想,总结下来就是 async Task 以及 await 关键字加到程序中,之前写过一个文件上传的控制器,就用到了异步,这次索性把程序从查库开始都改成了异步方法,而且微软相关的方法也都有同样的异步方法,下面说一说遇到的几个小问题

1、ToList()这个异步的方法是ToListAsync,需要引用using Microsoft.EntityFrameworkCore;

2、过滤器异步方法重写,方法里面的next实在不知道怎么用,有知道的大侠可以留言赐教

        /// <summary>
        /// 判断当前用户的角色权限是否可以访问
        /// </summary>
        /// <param name="filterContext"></param>
        public override async Task OnActionExecutionAsync(ActionExecutingContext filterContext, ActionExecutionDelegate next)
        {
            if (_auth.CheckLogin() == false)
            {
                filterContext.HttpContext.Response.Cookies.Append("wangbg_login_error", "overdue");
                filterContext.Result = new RedirectResult("/Manage/Login");
                return;
            }
            if (Ignore == true)
            {
                await base.OnActionExecutionAsync(filterContext, next);
                return;
            }
            var tempUser = _auth.GetCurrentUser();
            if (tempUser.IsSystem == true)
            {
                await base.OnActionExecutionAsync(filterContext, next);
                return;
            }
            var request = filterContext.HttpContext.Request;
            var moduleId = request.Cookies["currentmoduleid"];
            if (string.IsNullOrEmpty(moduleId) == true)
            {
                throw new Exception("很抱歉!参数传递错误,访问被拒绝!");
            }
            var tempRet = await _sysRoleAuthorizeApp.ActionValidate(tempUser.SysRoleId, moduleId, request.Path);
            if (tempRet == false)
            {
                throw new Exception("很抱歉!您的权限不足,访问被拒绝!");
            }
            await base.OnActionExecutionAsync(filterContext, next);
        }

下一步就是把程序中关于redis缓存操作的方法也都改造过来。

猜你喜欢

转载自www.cnblogs.com/wangbg/p/10536008.html