IIS7, VS2008 debug mode and Cache.Insert thread results NullReferenceException

Discussion in 'ASP .Net' started by Max2006, Feb 22, 2009.

  1. Max2006

    Max2006 Guest

    Hi,

    I am debugging a very simple, web project in VS2008, IIS7. The application
    uses AbsoluteExpiration timeout in Cache.Insert.

    The application works fine except when a debugger is attached to it. Even a
    single break point on a line of code crashes the application. The problem is
    when a debugger is attached and the "onRemoveCallback" method is called,
    HttpContext.Current is null.

    Here is the code that reproduces the problem. If you have breakpoint
    anywhere in the application, RefreshHelpContentsUrlCache method gives
    NullReferenceException because HttpContext.Current is null.

    Is there any way to make it work when VS2008 debugger is engaged?

    Any help would be appreciated,
    Max


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Caching;

    namespace WebLabIIS
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Label1.Text = CachedData;
    }
    public static string CachedData
    {
    get
    {
    if (HttpContext.Current.Cache["CachedData"] == null)
    RefreshCache(null, null,
    CacheItemRemovedReason.Expired);

    return HttpContext.Current.Cache["CachedData"] as string;
    }
    }
    private static void RefreshCache(String key, object value,
    CacheItemRemovedReason removedReason)
    {
    string data = "Time: " + DateTime.Now.ToString();

    HttpContext.Current.Cache.Insert(
    "CachedData",
    data,
    null,
    System.DateTime.Now.AddSeconds(1),
    Cache.NoSlidingExpiration,
    CacheItemPriority.Default,
    RefreshCache);
    }
    }
    }
     
    Max2006, Feb 22, 2009
    #1
    1. Advertising

  2. Hi Max,

    My name is Allen Chen. It's my pleasure to work with you on this issue.

    I've reproduced this issue on my side. As you said, HttpContext.Current is
    null in the callback method.

    However, this is an expected behavior. In general, it's recommended to use
    HttpRuntime.Cache instead of HttpContext.Current in the callback method.
    Please check out the following documentations for more details:

    http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx
    http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx

    Please try it and let me know if it can solve this issue. If you have
    additional questions please feel free to ask. I'll do my best to follow up.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Feb 23, 2009
    #2
    1. Advertising

  3. Max2006

    Max2006 Guest

    Thank you very much Allen. HttpRuntime.Cache solved the problem.


    "Allen Chen [MSFT]" <> wrote in message
    news:D...
    > Hi Max,
    >
    > My name is Allen Chen. It's my pleasure to work with you on this issue.
    >
    > I've reproduced this issue on my side. As you said, HttpContext.Current is
    > null in the callback method.
    >
    > However, this is an expected behavior. In general, it's recommended to use
    > HttpRuntime.Cache instead of HttpContext.Current in the callback method.
    > Please check out the following documentations for more details:
    >
    > http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx
    > http://weblogs.asp.net/pjohnson/archive/2006/02/06/437559.aspx
    >
    > Please try it and let me know if it can solve this issue. If you have
    > additional questions please feel free to ask. I'll do my best to follow
    > up.
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 2 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions. Issues of this
    > nature are best handled working with a dedicated Microsoft Support
    > Engineer
    > by contacting Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
     
    Max2006, Feb 23, 2009
    #3
  4. You're welcome Max.

    Thank you for using our Newsgroup Support Service and have a nice day!

    Regards,
    Allen Chen
    Microsoft Online Community Support

    =================================================
    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .
    =================================================
     
    Allen Chen [MSFT], Feb 23, 2009
    #4
    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. Asaf
    Replies:
    11
    Views:
    638
    OneTime Use
    Mar 22, 2008
  2. Max2006
    Replies:
    1
    Views:
    2,144
    Steven Cheng [MSFT]
    Jun 25, 2008
  3. Luis Serrano
    Replies:
    0
    Views:
    2,403
    Luis Serrano
    May 4, 2009
  4. ADNT
    Replies:
    3
    Views:
    2,025
  5. Peter Larsen [CPH]

    Internet Explorer and Kernel mode authentication on IIS7

    Peter Larsen [CPH], Jul 30, 2010, in forum: ASP .Net Security
    Replies:
    2
    Views:
    1,795
    Peter Larsen [CPH]
    Aug 11, 2010
Loading...

Share This Page