Web Development Best Practice

2 posts, 0 answers
  1. Rui
    Rui avatar
    30 posts
    Member since:
    Feb 2012

    Posted 03 Apr 2012 Link to this post

    Hello I was looking at your samples in the SDK.
    In bestpracticewebdevelopmenttwo you have:
    public class ContextFactory
            public static NorthwindContext GetContextPerRequest()
                HttpContext httpContext = HttpContext.Current;
                if (httpContext == null)
                    return new NorthwindContext();
                    int contextId = Thread.CurrentContext.ContextID;
                    int hashCode = httpContext.GetHashCode();
                    string key = string.Concat(hashCode, contextId);
                    NorthwindContext context = httpContext.Items[key] as NorthwindContext;
                    if (context == null)
                        context = new NorthwindContext();
                        httpContext.Items[key] = context;
                    return context;

    Using this method how will the NorthwindContext be disposed?

  2. Ivailo
    Ivailo avatar
    318 posts

    Posted 09 Apr 2012 Link to this post

    Hello Rui,

    You have probably seen the old version of this example, available with Q3 2011 of OpenAccess SDK.

    In the latest release of the SDK (available on our web site for free) you can check the refactored and renamed sample now called "Managing OpenAccess Context" in the ASP.NET category. There, you will find the following way of disposing the OpenAccessContext in case you have chosen to store it in the HttpContext:

    public class Global : HttpApplication
        protected void Application_EndRequest(object sender, EventArgs e)
            NorthwindContext context = ContextFactory.GetContextPerRequest();
            if (context != null)

    I hope that helps. Let me know if you have any further questions. 

    All the best,
    the Telerik team
    Telerik OpenAccess ORM Q1 2012 release is here! Check out what's new or download a free trial >>
  3. DevCraft banner
Back to Top