I've been following several tutorial on integrating the OWin identity in my ASP.net app, for instance this one
So my StartupLogin now has the method
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
{
[...]
to create (and I quote the link) "a per-request, single instance of the UserManager and DbContext classes from the OWIN context to be used throughout the application."
But I saw very weird calling behavior to the Create
functions, so I added the 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() { }
}
and the line
app.CreatePerOwinContext(DebugManager.Create);
Looking at the debug window, I saw that the Create()
methods are called 74 times on initial application startup, and 120-124(!!!!) times on other actions like login, page refresh, logout, etc, etc...
That cannot be the way it should work, right? A create
function should only be called once, where after you can get the instance of the object via the OWinContext.Get<>
, right? What's going on here?