access web application object from background thread?

Discussion in 'ASP .Net' started by =?Utf-8?B?TWlrZQ==?=, Feb 8, 2007.

  1. I have an asp.net 2.0 web application where some of the processing has been
    delegated to a background worker thread. The code for that thread is in a VB
    module that is part of the application, but is not a code-behind. I would
    like to read/write values in the application object's cache, but can't find a
    way to get to it. The background thread is part of the web app, but it does
    not run in the context of an http request. So when I try:
    Dim app As HttpApplication = HttpContext.Current.ApplicationInstance
    Dim appcache As HttpApplicationState = app.Application
    appcache.Add("v1", 1)
    I get an exception on the first line because "Current' is set to nothing.

    Any suggestions appreciated-

    Mike
     
    =?Utf-8?B?TWlrZQ==?=, Feb 8, 2007
    #1
    1. Advertising

  2. Your VB module that needs to access the current HttpContext would need to
    have a reference to System.Web assembly.
    There may be other issues but since we haven't seen your code, deal with
    those afterward.
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Mike" wrote:

    > I have an asp.net 2.0 web application where some of the processing has been
    > delegated to a background worker thread. The code for that thread is in a VB
    > module that is part of the application, but is not a code-behind. I would
    > like to read/write values in the application object's cache, but can't find a
    > way to get to it. The background thread is part of the web app, but it does
    > not run in the context of an http request. So when I try:
    > Dim app As HttpApplication = HttpContext.Current.ApplicationInstance
    > Dim appcache As HttpApplicationState = app.Application
    > appcache.Add("v1", 1)
    > I get an exception on the first line because "Current' is set to nothing.
    >
    > Any suggestions appreciated-
    >
    > Mike
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 8, 2007
    #2
    1. Advertising

  3. My apologies Peter, but I can't figure out where to go with your advice.
    Maybe I misused the term 'module'? It's just another VB file in my web app
    project, that isn't a code-behind for an aspx page. I'm only guessing that
    that's why HttpContext.Current is coming up null. Or is it because the code
    actually runs in a background thread, and not associated directly or
    indirectly with http request code?

    In any event, my project has a reference to System.Web, and I do not see a
    way to add a reference to System.Web just for this one VB file.

    Thanks for your input, apologies if I am making this harder than it should be.

    Mike

    "Peter Bromberg [C# MVP]" wrote:
    > Your VB module that needs to access the current HttpContext would need to
    > have a reference to System.Web assembly.
    > There may be other issues but since we haven't seen your code, deal with
    > those afterward.
    > Peter
    >
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
     
    =?Utf-8?B?TWlrZQ==?=, Feb 8, 2007
    #3
  4. OK, this should have been obvious: There is no HttpContext.Current when my
    background thread is running an no http requests are active. Duh.

    Switched to HttpRuntime.Cache, and all good. Here's an article if anyone
    follows this thread looking for help:

    http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx

    Thank you Peter Bromberg. You are a Rock Star, I have benefitted a lot from
    your various articles/postings.

    Mike

    "Mike" wrote:

    > My apologies Peter, but I can't figure out where to go with your advice.
    > Maybe I misused the term 'module'? It's just another VB file in my web app
    > project, that isn't a code-behind for an aspx page. I'm only guessing that
    > that's why HttpContext.Current is coming up null. Or is it because the code
    > actually runs in a background thread, and not associated directly or
    > indirectly with http request code?
    >
    > In any event, my project has a reference to System.Web, and I do not see a
    > way to add a reference to System.Web just for this one VB file.
    >
    > Thanks for your input, apologies if I am making this harder than it should be.
    >
    > Mike
    >
    > "Peter Bromberg [C# MVP]" wrote:
    > > Your VB module that needs to access the current HttpContext would need to
    > > have a reference to System.Web assembly.
    > > There may be other issues but since we haven't seen your code, deal with
    > > those afterward.
    > > Peter
    > >
    > > --
    > > Site: http://www.eggheadcafe.com
    > > UnBlog: http://petesbloggerama.blogspot.com
    > > Short urls & more: http://ittyurl.net

    >
     
    =?Utf-8?B?TWlrZQ==?=, Feb 8, 2007
    #4
  5. Glad you fixed it. Rock star, doubt it. Just another slob trying to help out
    by giving back.
    Cheers,
    Peter

    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "Mike" wrote:

    > OK, this should have been obvious: There is no HttpContext.Current when my
    > background thread is running an no http requests are active. Duh.
    >
    > Switched to HttpRuntime.Cache, and all good. Here's an article if anyone
    > follows this thread looking for help:
    >
    > http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx
    >
    > Thank you Peter Bromberg. You are a Rock Star, I have benefitted a lot from
    > your various articles/postings.
    >
    > Mike
    >
    > "Mike" wrote:
    >
    > > My apologies Peter, but I can't figure out where to go with your advice.
    > > Maybe I misused the term 'module'? It's just another VB file in my web app
    > > project, that isn't a code-behind for an aspx page. I'm only guessing that
    > > that's why HttpContext.Current is coming up null. Or is it because the code
    > > actually runs in a background thread, and not associated directly or
    > > indirectly with http request code?
    > >
    > > In any event, my project has a reference to System.Web, and I do not see a
    > > way to add a reference to System.Web just for this one VB file.
    > >
    > > Thanks for your input, apologies if I am making this harder than it should be.
    > >
    > > Mike
    > >
    > > "Peter Bromberg [C# MVP]" wrote:
    > > > Your VB module that needs to access the current HttpContext would need to
    > > > have a reference to System.Web assembly.
    > > > There may be other issues but since we haven't seen your code, deal with
    > > > those afterward.
    > > > Peter
    > > >
    > > > --
    > > > Site: http://www.eggheadcafe.com
    > > > UnBlog: http://petesbloggerama.blogspot.com
    > > > Short urls & more: http://ittyurl.net

    > >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Feb 9, 2007
    #5
  6. =?Utf-8?B?TWlrZQ==?=

    Mike Otten Guest

    I'll bet you slouch too. Man, if I can't get in a good slouch: *nap*.

    Peter Bromberg [C# MVP] wrote:
    > Glad you fixed it. Rock star, doubt it. Just another slob trying to help out
    > by giving back.
    > Cheers,
    > Peter
    >
     
    Mike Otten, Feb 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Al Cohen
    Replies:
    4
    Views:
    2,470
    Al Cohen
    Jan 10, 2004
  2. Johanna
    Replies:
    0
    Views:
    623
    Johanna
    Oct 13, 2004
  3. =?Utf-8?B?Sm9oYW5uYQ==?=
    Replies:
    3
    Views:
    1,998
    =?Utf-8?B?Sm9oYW5uYQ==?=
    Oct 15, 2004
  4. Ashish
    Replies:
    1
    Views:
    636
    Spam Catcher
    Dec 23, 2005
  5. mason66
    Replies:
    0
    Views:
    437
    mason66
    Jul 27, 2006
Loading...

Share This Page