AbeyMarquez
AbeyMarquez

Reputation: 655

Does ASP.NET Identity's UserManager need to be closed or disposed?

I've been using ASP.NET Identity for a while now and I've followed tutorials and sites that always say to create the UserManager in my methods with this code:

var userManager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>()

All was working fine until, one day, I noticed that UserManager implements IDisposable. And on my quest to make sure everything I open gets closed properly, I added using to all these instances and suddenly everything stopped working. I started getting "Cannot access a disposed object." errors all over the place. From what I could research, apparently some instances were being closed while others were still being used.

Oddly enough, when I searched the web, there was very little to no info on whether one should close or dispose of UserManager or any of the other Managers (i.e. SignInManager, RoleManager).

So, what's the consensus on this? Should those Managers be closed/disposed or not? And if not, then where should we declare them if not inside a method? I've read they shouldn't be static.

Upvotes: 1

Views: 1211

Answers (1)

AbeyMarquez
AbeyMarquez

Reputation: 655

I may have found my answer. After I kept digging around obscure links, I found this MSDN blog post which explains that these Managers should be single-instance and explains exactly how to declare them in a page or controller: https://blogs.msdn.microsoft.com/webdev/2014/02/12/per-request-lifetime-management-for-usermanager-class-in-asp-net-identity/

Upvotes: 1

Related Questions