Why would this line throw exception for type initializer failed?

Posted by Jaggu on Stack Overflow See other posts from Stack Overflow or by Jaggu
Published on 2012-06-19T14:49:46Z Indexed on 2012/06/19 15:16 UTC
Read the original article Hit count: 250

Filed under:
|
|
|
|

I had a class:

public class Constant
{

    public static string ConnString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
}

which would throw exception on LIVE: Type initialize failed for Constant ctor

If I change the class to:

public class Constant
{
    public static string ConnString
    {
        get
        {
            return ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
        }
    }
}

it works. I wasted 2 hours behind this but I still don't know why would this happen. Any ideas?

Note: The 1st class used to work on DEV environment but not on LIVE. The 2nd class works on DEV and also on Production.

I am using VS2010 on production and Asp.Net 4.0 Website project.

I am totally amazed by this inconsistency to say the least!

Edit: This class was in App_Code folder.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET