Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

IsInRoleAsync gives a runtime error

$
0
0

Hi,

Following view model and controller:

{

    public class User
    {
        public string UserName { get; set; }
        public bool IsUser { get; set; }
        public bool IsAdmin { get; set; }
    }

    public class UserListModel : PageModel
    {
        private readonly ApplicationDbContext _context;
        private readonly UserManager<ApplicationUser> _userManager;

        public UserListModel(ApplicationDbContext context, UserManager<ApplicationUser> userManager)
        {
            _context = context;
            _userManager = userManager;
        }

        public IList<User> Users = new List<User>();

        public async void OnGetAsync()
        {
            List<ApplicationUser> AppUsers = _context.Users.ToList();
            foreach (ApplicationUser U in AppUsers)
            {
                User X = new User  //Runtime error here!!
                {
                    UserName = U.UserName,
                    IsUser = await _userManager.IsInRoleAsync(U, "Member"),
                    IsAdmin = await _userManager.IsInRoleAsync(U, "Admin")
                };
                Users.Add(X);
            }
        }
    }
}

it's building OK, but when I run it I get A runtime error:

System.ObjectDisposedException
HResult=0x80131622
Message=Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
Object name: 'ApplicationDbContext'.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.get_StateManager()
at Microsoft.EntityFrameworkCore.Query.QueryContextDependencies.get_StateManager()
at Microsoft.EntityFrameworkCore.Query.QueryContext.get_StateManager()
at Microsoft.EntityFrameworkCore.Query.QueryContext.BeginTrackingQuery()
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider._TrackEntities[TOut,TIn](IAsyncEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<ExecuteSingletonAsyncQuery>d__21`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.<IsInRoleAsync>d__37.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.AspNetCore.Identity.UserManager`1.<IsInRoleAsync>d__113.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at RoosterApp.Areas.Identity.Pages.Account.UserListModel.<OnGetAsync>d__4.MoveNext() in F:\rooster\Rooster\RoosterApp\RoosterApp\Areas\Identity\Pages\Account\UserList.cshtml.cs:line 39

Please can someone tell me what is wrong in my code.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>