Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Uri bug in .NET that Ayende refers to in the Raccoon Blog source code?

Tags:

.net

uri

asp.net

While looking at source of Ayende's Racoon Blog, I saw this in global.asax.cs:

// Work around nasty .NET framework bug
try
{
    new Uri("http://fail/first/time?only=%2bplus");
}
catch (Exception)
{
}

This appears to be a workaround for a bug that happens on the first request. Does anyone know what the bug is or how to reproduce it?

like image 593
Goran Obradovic Avatar asked Nov 04 '22 15:11

Goran Obradovic


1 Answers

A bit of googling gets to this Ayende blog post from March 2010 from which I quote an excerpt:

I can reproduce this now, here it how it got there:

public class Strange : MarshalByRefObject
{
    public void WTF()
    {
        Console.WriteLine(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        new Uri("http://localhost:58080/indexes/categoriesByName?query=CategoryName%3ABeverages&start=0&pageSize=25");
    }
}

public class Program
{
    private static void Main()
    {
        var instanceAndUnwrap = (Strange) AppDomain.CreateDomain("test", null, new AppDomainSetup
        {
            ConfigurationFile = ""
        }).CreateInstanceAndUnwrap("ConsoleApplication5", "ConsoleApplication5.Strange");
        instanceAndUnwrap.WTF();
    }
}

That took some time to figure out.

From the comment thread below, which I have skimmed but not read in detail, the root cause appears to be an error in the machine root config file, which is only parsed once per ?AppDomain, hence the lack of an error the second and subsequent times.

Habitually using this construct having been burned by it once is the kind of habit that programmers accumulate through hard experience. The less experienced may snigger "cargo cult" or "programming by coincidence", to which the more experienced will just smile and nod.

like image 97
AakashM Avatar answered Nov 15 '22 05:11

AakashM