mercredi 23 septembre 2015

c# Mvc HttpContext.Current.Items and Clearing when request is complete diff

Lets say that you are storing a Entity Framework DBConect in the HttpContext.Current.Items

When the request is finished... does it make a difference if you do?

protected virtual void Application_BeginRequest() {
    var entityContext = HttpContext.Current.Items["myContext"] as DbEnitities
    if (entityContext != null)
    entityContext.Dispose();
}

Or not... since the HttpContext.Current.Items only lives for the request and will no longer exist after the request is complete... it should clear all its contents anyway.

or will the connection technically still be open and only closed when the GC(garbage collection) is called? so basically all it would do is close the connection sooner? what is the negative of this.... I assume an open connection really doesn't consume that much memory.

Is it more correct to have the Application_BeginRequest and close the connection or not? Since this should all happen automatically anyway... are there any benefits or negatives to doing this?

Would prefer a long technically explanation with supporting info, rather than "Yes" as an answer.




Aucun commentaire:

Enregistrer un commentaire