Identity icon indicating copy to clipboard operation
Identity copied to clipboard

cannot access a disposed object UserManager in asp.net mvc core

Open onedevteam opened this issue 10 years ago • 9 comments

I have following code in my service that should create UserManager instance

public class SubscriptionService : ISubscriptionService
{

    private UserManager<ApplicationUser> userManager;
    public UserManager<ApplicationUser> UserManager
    {
        get
        {
            return userManager;  

        }
        private set { userManager = value; }
    }

    public SubscriptionService(
        UserManager<ApplicationUser> userManager,   
        StripeCustomerService customerService, 
        StripeSubscriptionService subscriptionService)
    {
        this.userManager = userManager;
        this.customerService = customerService;
        this.subscriptionService = subscriptionService;
    }

    public SubscriptionService(UserManager<ApplicationUser> userManager)
    {
        this.userManager = userManager;
    }

When userManager is called first time inside class, it works but second call returns an error "cannot access a disposed object UserManager"

I'm consuming SubscriptionService class in my controller with:

public ISubscriptionService SubscriptionService
{
    get
    {
        return subscriptionService ?? new SubscriptionService(_userManager);
    }
    private set
    {
        subscriptionService = value;
    }
}

and service is called with

SubscriptionService.Create(HttpContext.User.Identity.Name, model.Plan, model.StripeToken); //this is function that calls userManager and throws an exception

Can someone point me where i'm wrong with this and how to fix it?

I'm using ASP.NET Core 1.0

onedevteam avatar Jun 11 '16 09:06 onedevteam

How are you registering your SubscriptionService, is its lifetime scoped? The usermanager will be disposed after each request, so that is most likely what's causing the error.

HaoK avatar Jun 11 '16 09:06 HaoK

SubscriptionService is registered in Startup.ConfigureServices with

        services.AddTransient<ISubscriptionService, SubscriptionService>();

and i'm using userManager inside function:

 public async void Create(...) {
var user = userManager.GetUserByNameAsync(name);
.
.//do something with user
.

userManager.UpdateAsync(user); // this is where exception is thrown
}

onedevteam avatar Jun 11 '16 09:06 onedevteam

Interesting your instatiating the service potentially inside the controller. Why do that?

dcarr42 avatar Jun 11 '16 10:06 dcarr42

@dallancarr If i don't reference service, when i try to call it i get compile error An object reference is required for the non-static field, method, or property 'SubscriptionService.Create(string, Plan, string)'

onedevteam avatar Jun 11 '16 11:06 onedevteam

Update: i have replaced in my Startup.ConfigureServices()

services.AddTransient<ISubscriptionService, SubscriptionService>();

with

services.AddSingleton<ISubscriptionService, SubscriptionService>();

and app worked as it should be.

onedevteam avatar Jun 11 '16 11:06 onedevteam

Yes that would work but not ideal. How are you constructing the controller?

dcarr42 avatar Jun 11 '16 11:06 dcarr42

Controller constructor:

        private IPlanService planService;
        private ISubscriptionService subscriptionService;
        private UserManager<ApplicationUser> _userManager;

        public SubscriptionController(IPlanService planService, 
                                        ISubscriptionService subscriptionService, 
                                        UserManager<ApplicationUser> userManager)
        {
            this._userManager = userManager;
            this.planService = planService;
            this.subscriptionService = subscriptionService;
        }

onedevteam avatar Jun 11 '16 11:06 onedevteam

The newing code should be removed and let DI deal with the lifetime. There some scoped dependencies inside the manager which force your hand a little.

dcarr42 avatar Jun 11 '16 12:06 dcarr42

Also no need to inject the manager in the controller if you are constructing it as part of the service. But that is down to your implementation.

dcarr42 avatar Jun 11 '16 12:06 dcarr42