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

  • Thread starter Thread starter Max2006
  • Start date Start date
M

Max2006

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);
}
}
}
 
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:
(e-mail address removed).

==================================================
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.
 
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:
(e-mail address removed).
=================================================
 
Back
Top