OWin Appbuilder 每次事件重建应用程序 70 -125 次

OWin Appbuilder rebuilding the app 70 -125 times per event

我一直在关注一些关于将 OWin 身份集成到我的 ASP.net 应用程序中的教程,例如 this one

所以我的 StartupLogin 现在有了方法

public static void ConfigureAuth(IAppBuilder app)
{
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
    app.CreatePerOwinContext<ApplicationRoleManager>(Application‌​RoleManager.Create);

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
    [...]

创建(我引用了 link)“来自 OWIN 上下文的 UserManager 和 DbContext class 的单个实例,以在整个应用程序中使用。”。 =18=]

但是我看到 Create 函数的调用行为非常奇怪,所以我添加了 class

class DebugManager : IDisposable
{
    private static int count = 0;
    public static DebugManager Create() { Debug.WriteLine($"Owin Build called {count++};"); return new DebugManager(); }
    public void Dispose() { }
}

和行

app.CreatePerOwinContext(DebugManager.Create);

查看调试 window,我看到 Create() 方法在初始应用程序启动时被调用 74 次, 120-124(!!!!) 次其他操作,如登录、页面刷新、注销等...

这不是它应该的工作方式,对吧? create 函数应该只调用一次,之后你可以通过 OWinContext.Get<> 获取对象的实例,对吧?这是怎么回事?

我找到了答案here

Dependency Injection in Owin

... each time the OwinContext is pulled from the HttpContext, it returns a new context ... if we are applying DI terms here, then your object is registered in per-request scope, as you would expect to be DbContext.

我最近在 中了解了“瞬态”、“作用域”和“单例”之间的区别。

事实证明,我们(非常大的)项目(在开发模式下,静态文件缓存关闭)的页面请求导致对服务器的 125 次请求,这导致了问题。