Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 5,953 | Comments: 44,409

filter by tags archive

Hunt the bug


The following code will throw under certain circumstances, what are they?

public class Global : HttpApplication
{
       public void Application_Start(object sender, EventArgs e)
       {
            HttpUtility.UrlEncode("Error inside!");
       }
}

Hint, the exception will not be raised because of transient conditions such as low memory.

What are the conditions in which it would throw, and why?

Hint #2, I had to write my own (well, take the one from Mono and modify it) HttpUtility to avoid this bug.

ARGH!


Comments

tobi

I guess it has to do with the new HttpEncoder support. I can be disabled by setting Response.DisableCustomHttpEncoder but that is not available in application start. You could have used HttpEncoder.Default however.

Artur

I'm guessing that HttpUtility underneath requires an HttpContext and that might not be present at Application_Start.

Victor Kornov

Yep, starting .NET 4 HttpUtility requires a HttpContext /HttpResponse wich is not yet available at the time of Application_Start with IIS7 in integrated mode. I guess the stack trace looks like:

[HttpException (0x80004005): Response is not available in this context.]

System.Web.HttpContext.get_Response() +8753496

System.Web.Util.HttpEncoder.get_Current() +39

System.Web.HttpUtility.UrlEncodeToBytes(String str, Encoding e) +31

System.Web.HttpUtility.UrlEncode(String str) +41

Global.Application_Start(Object sender, EventArgs e)

tobi

This means that the context is available but it is a fake one - getting the response throws. My workaround that I posted is still valid ;-)

Sebastien Ros

There has been some changes in .NET 4 which introduced this bug. I hit it when we switched our solution to VS2010. So as it's actually forgetting to check HttpContext.Current null values before using it, we hacked this by assigning a mock HttpContext around it.

Sebastien

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. The RavenDB Comic Strip (3):
    28 May 2015 - Part III – High availability & sleeping soundly
  2. Special Offer (2):
    27 May 2015 - 29% discount for all our products
  3. RavenDB Sharding (3):
    22 May 2015 - Adding a new shard to an existing cluster, splitting the shard
  4. Challenge (45):
    28 Apr 2015 - What is the meaning of this change?
  5. Interview question (2):
    30 Mar 2015 - fix the index
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats