cannot access a disposed object UserManager in asp.net mvc core
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
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.
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
}
Interesting your instatiating the service potentially inside the controller. Why do that?
@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)'
Update: i have replaced in my Startup.ConfigureServices()
services.AddTransient<ISubscriptionService, SubscriptionService>();
with
services.AddSingleton<ISubscriptionService, SubscriptionService>();
and app worked as it should be.
Yes that would work but not ideal. How are you constructing the controller?
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;
}
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.
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.