locked
Error when attempting to use _userManager.GetUserByEmailAsync(... RRS feed

  • Question

  • User1457412228 posted

    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: 'AsyncDisposer'.

    This code fails when excuted in the IndexModel class, but I simply copied it from where I am using it in my own directory in the Identity Area where it does work and does not throw an exception.

    I have put off learning DI, and it appears this has bitten me.  

    The main difference between the two classes is that I am using a different menu for the class that works, than the class that does not.

    I would assume that since the only difference I am aware of between IndexModel and the class that works is the menus, that commenting out everything in the larger menu except the bare minimum to launch the page would fix this issue.  But it does  not.

      What could I be doing in one place to break this code, that I am not doing in the other?

    thanks!

     public class IndexModel : PageModel
        {
            private readonly SignInManager<IdentityUser> _signInManager;
            private readonly UserManager<IdentityUser> _userManager;
            private readonly ILogger<RegisterModel> _logger;
            private readonly RoleManager<IdentityRole> _roleManager;
    
            public IndexModel(
                UserManager<IdentityUser> userManager,
                SignInManager<IdentityUser> signInManager,
                ILogger<RegisterModel> logger,
                RoleManager<IdentityRole> roleManager
                )
            {
                _userManager = userManager;
                _signInManager = signInManager;
                _logger = logger;
                _roleManager = roleManager;
            }
    
    
           
            public async void OnGet()
            {
                var userName = User.Identities.First<ClaimsIdentity>().Name;
    
    
                try
                {
                    var userByEmail = await _userManager.FindByEmailAsync(userName);
    

    And to be complete, here is the code that does work that I copied this over from.  

    public class ManageUserAccountsModel : PageModel
        {
    
            private readonly SignInManager<IdentityUser> _signInManager;
            private readonly UserManager<IdentityUser> _userManager;
            private readonly ILogger<RegisterModel> _logger;
            private readonly RoleManager<IdentityRole> _roleManager;
    
            public ManageUserAccountsModel(
                UserManager<IdentityUser> userManager,
                SignInManager<IdentityUser> signInManager,
                ILogger<RegisterModel> logger,
                RoleManager<IdentityRole> roleManager
                )
            {
                _userManager = userManager;
                _signInManager = signInManager;
                _logger = logger;
                _roleManager = roleManager;
            }
    
            [BindProperty ]
            public List<EmailDto> Customers { get; set; }
            public async Task<IActionResult> OnGet()
            {
                //currently can only manage account from local computer in VS.  Remove If Debug to publish to web
                Customers = new List<EmailDto>();
                using (var context = new resumesEntities())
                {
                    var users = (from i in context.Users
                                 select new EmailDto
                                 {
                                     Id = i.ID,
                                     Email = i.IdentityName
                                 }).OrderBy(a=>a.Email); 
    
                    if(users!=null)
                    { 
                        IList<string> emptyList = new List<string> { "none"};
                         
                        foreach (EmailDto j in users)
                        {
                            var next = await _userManager.FindByEmailAsync(j.Email);

    Wednesday, June 19, 2019 2:16 PM

Answers

  • User-1764593085 posted

    Hi talldaniel,

    If somewhere in your call stack you return a void instead of Task you get the described behavior. At that point the call is ended and the context disposed. 

    You need to return Task instead of void in IndexModel if you are using DI and async calls.

    public async Task OnGetAsync()

     Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 20, 2019 6:04 AM