asp.net mvc核心在第一个操作完成之前,已在上下文中启动了第二个操作

时间:2018-08-15 10:47:24

标签: asp.net-mvc asynchronous async-await task

我正在使用此代码在控制器上进行授权。

[Authorize(Policy = "CustomRole")]

发生了这样的事情:3或4个请求后,它失败并显示

在先前的操作完成之前,第二个操作就此开始了

 public class CustomRoleRequirement : AuthorizationHandler<CustomRoleRequirement>, IAuthorizationRequirement
{
    public CMSContext _context = new CMSContext();
    protected override  Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomRoleRequirement requirement)
    {

        var routeobj = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext;
        var c = routeobj.RouteData.Values.Values.ToList();

        var keys = routeobj.RouteData.Values.Keys.ToList();
        string area = "";
        string id = "";
        string controller = "";
        string action = "";
        string module = "";
        foreach (var item in keys)
        {                       
            if (item=="area")
            {
                int indexs = keys.FindIndex(cc => cc == "area");
                area = c[indexs].ToString();
            }
            else if (item == "id")
            {
                int indexs = keys.FindIndex(cc => cc == "id");
                id = c[indexs].ToString();
            }
            else if (item == "controller")
            {
                int indexs = keys.FindIndex(cc => cc == "controller");
                controller = c[indexs].ToString();
            }
            else if (item == "module")
            {
                int indexs = keys.FindIndex(cc => cc == "module");
                module = c[indexs].ToString();
            }
            else  if (item == "action")
            {
                int indexs = keys.FindIndex(cc => cc == "action");
                action = c[indexs].ToString();
            }
        }

        string modulelink = controller;

        if (!string.IsNullOrEmpty(module))
        {
            modulelink = modulelink + "/" + module; 
        }

        List<string> Roles = new List<string>();
        int UserId = Auth.UserID;
        string UserName = Auth.UserName;
        if (UserName == "superadmin")
        {
            context.Succeed(requirement);
            return Task.CompletedTask;
        }
        else
        {
// apparently the error occurred here

            var moduleobj = _context.AppModules.FirstOrDefault(q => q.Link == modulelink);
            if (moduleobj != null)
            {  // 69 role is assessing news module
               //60 role is accessing page module
                var RolesModulesobj = _context.AppRolesModules.FirstOrDefault(q => q.ModuleId == moduleobj.ModuleId && q.RolesId == Auth.RoleId);
                if (RolesModulesobj != null)
                {
                    string permissionsobj = RolesModulesobj.Permissions;
                    List<string> PermissionsListobj = permissionsobj.Split(',').Select(x => x.Trim()).ToList();

                    var FindFullAccess = PermissionsListobj.FirstOrDefault(q => q.Contains("FullAccess:true"));
                    if (FindFullAccess != null)
                    {

                        context.Succeed(requirement);
                        return Task.CompletedTask;
                    }
                    else
                    {
                        var abc = PermissionsListobj.FirstOrDefault(q => q.Contains(action + ":true"));
                        if (abc != null)
                        {
                            context.Succeed(requirement);
                            return Task.CompletedTask;
                        }
                        else
                        {
                            context.Fail();
                            return Task.CompletedTask;
                        }
                    }
                }
            }
        }

此行上方的错误

 var moduleobj = _context.AppModules.FirstOrDefault(q => q.Link == modulelink);

如何在上述方法中开始第二项操作之前等待任务?

1 个答案:

答案 0 :(得分:1)

您不能使用单例数据库上下文。您可以在每次需要时创建一个,也可以pool them来创建。