HttpContext server compatibility?

I have a web application that currently uses the current HttpContext to store the LINQ Data Context. The context is saved for the current request based on each user for Rick Stragg's Blog :

string ocKey = "ocm_" + HttpContext.Current.GetHashCode().ToString("x")  
Thread.CurrentContext.ContextID.ToString();

if (!HttpContext.Current.Items.Contains(ocKey))
{
    // Get new Data Context and store it in the HTTP Context
}

However, I have some scripts that run from the global.asax file that do not have an HttpContext. HttpContext.Current is NULL because the server is the one who makes the request.

Is there an equivalent object that I can use to store a Data Context? So I don’t have to worry about re-creating it and connecting / disconnecting objects? I only want to maintain context for the life of my processes.

UPDATED:

I'm currently trying to use a static variable in my DAL helper class. upon the first call of one of the methods in the class, an instance of the DataContext is created and stored in a static variable. At the end of my process, I call another method that calls Dispose on the DataContext and sets the static variable to NULL.

+2
source share
5 answers

Can you not just use a static variable specifically for these scripts? This will have the same service life as AppDomain. You should probably think carefully about any concurrency issues, but that sounds like the easiest way to keep the value around.

( , HttpApplication , , . 't , , .)

EDIT: , , . , , , , , , , . , HttpApplication -derived - , :)

+4

HttpContext? global.asax , , , , .

- . HttpContext , , . - , HttpContext .

, DataContext, ? IDisposable - , , .


UPDATE

, , . , - , . HttpContext .

+1

HttpContext.Current , .

, , Application.Cache, , DataContext. linq , , , , , , .

global.asax Windows. , , -.

JS, . , ThreadLocal. . , .

0

, , DataContexts? , , . , Application_Start ( ), - .

0

Set the DataContext as the state parameter when creating the timer. Based on the information posted in the comments, it seems to me that your DataContext is more related to timers than to anything else.

Also avoid using the same DataContext for different timers, because you will get mixed changes from different timers. Also, make sure that the same timer logic does not start twice, as this will lead to the same too short period without control.

0
source

All Articles