[英]Disposing UserManager and UserStore outside of AccountController?
Is this good enough? 这够好吗? Or do i have to dispose
UserStore
as well? 或者我是否也必须处置
UserStore
? If i do have to any suggestions would be welcome. 如果我有任何建议,欢迎。 I'm new to ASP.NET Identity.
我是ASP.NET身份的新手。
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext)))
{
}
}
This would be better i guess: 我想这会更好:
using (var applicationDbContext = new ApplicationDbContext())
{
using (var userStore = new UserStore<ApplicationUser>(applicationDbContext))
{
using (var userManager = new UserManager<ApplicationUser>(userStore))
{
}
}
}
EDIT: I'm glad that i asked this question, although i may have already answered my initial question. 编辑:我很高兴我问这个问题,虽然我可能已经回答了我的初步问题。 Thanks Glenn Ferrie, will check out the ASP.NET dependency injection.
感谢Glenn Ferrie,将检查ASP.NET依赖注入。
This is a few code snippets from a new ASP.NET MVC (.NET 4.6) created with VS 2015 RC. 这是使用VS 2015 RC创建的新ASP.NET MVC(.NET 4.6)的一些代码片段。 First the
Startup
class: 首先是
Startup
类:
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// rest of implementation ommitted for brevity.
then here is how you access it in a Controller class: 那么这是你在Controller类中访问它的方式:
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
// NOTE: ASP.NET will use this contructor and inject the instances
// of SignInManager and UserManager from the OWIN container
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager )
{
UserManager = userManager;
SignInManager = signInManager;
}
// there are implementations for the public properties
// 'UserManager' and 'SignInManager' in the boiler plate code
// not shown here
Happy Coding! 快乐的编码!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.