我试图熟悉OWIN并且有很多让我感到困惑的事情。例如,在部分startup.cs类中,我通过
注册上下文回调app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
区别是什么?为什么我们需要这种通用方法?
我可以像这样得到这样的背景:
context.Get<ApplicationDbContext>())
context.GetUserManager<ApplicationUserManager>()
Get和GetUserManager方法之间的区别是什么?为什么我不能只调用context.Get for ApplicationUserManager?
答案 0 :(得分:6)
Get<UserManager>
和GetUserManager<UserManager>
这是两个源代码......
/// <summary>
/// Retrieves an object from the OwinContext using a key based on the AssemblyQualified type name
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static T Get<T>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<T>(GetKey(typeof (T)));
}
/// <summary>
/// Get the user manager from the context
/// </summary>
/// <typeparam name="TManager"></typeparam>
/// <param name="context"></param>
/// <returns></returns>
public static TManager GetUserManager<TManager>(this IOwinContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
return context.Get<TManager>();
}