Question

In the application there are 2 pages, CompletePoll.aspx, Default.aspx.

CompletePoll.aspx --> Page_Load()

          Ultoo u = new Ultoo();
          u.UserName = Request["username"].ToString();
          u.Password = Request["password"].ToString();
          new Thread(u.CompletePoll).Start();

CompletePoll()

          .......
          .......
          String str = "Question:" + QuestionGenerator.GetNextQuestion(); /*Here i am getting Type initializer exception*/
          .......
          .......

QuestionGenerator

          public static class QuestionGenerator
          {
               private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
               private static string[] SecondParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart2.txt")).ReadToEnd(). Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
               private static Random r = new Random();

               public static string GetNextQuestion()
               {
                    return FirstParts[r.Next(0, FirstParts.Length - 1)] + " " + SecondParts[r.Next(0, SecondParts.Length - 1)] + "?";
               }
          }

But if i am calling Default.aspx first and then CompletePoll.aspx the code is working fine.

Default.aspx --> Page_Load()

         Label1.Text = QuestionGenerator.GetNextQuestion();

So here my problem is if i am accessing CompletePoll.aspx first i am getting TypeInitializer Exception. If i am accessing Default.aspx first and then CompletePoll.aspx, I am not getting any problem. Whats wrong in my code, am i missing something? How can i access CompletePoll.aspx first?

Was it helpful?

Solution

private static string[] FirstParts = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")).ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

That isn't right. This checks HttpContext.Current once, when the type is initialised, saves the result, and never attempts to read it again. The never checking again can be correct when the first time succeeds, but the first time will require HttpContext.Current to not be null. If the first attempt causes an exception, it won't be reinitialised later. You cannot be sure when exactly the class gets initialised, so you cannot be sure whether HttpContext.Current is set at that point (and it won't be if you call it from a thread).

Also, this does not call StreamReader.Dispose, so it will leave the reader and file itself open until the garbage collector happens to run.

A safer way would be something like

private static string[] firstParts; // field

private static string[] FirstParts // property
{
    get
    {
        if (firstParts == null)
        {
            using (var reader = new StreamReader(HttpContext.Current.Server.MapPath("App_Data/QuestionPart1.txt")))
                firstParts = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        }
        return firstParts;
    }
}

This will make sure reader.Dispose() gets called, will make sure the file is read when the property is first accessed instead of when the type is initialised, will make sure any exceptions actually tell you what's going on in a more straightforward way, and will make sure the rest of the type is usable even if FirstParts cannot get set.

However, it still requires that you do not read FirstParts from a thread. You can avoid that problem by reading it once before starting the thread:

Ultoo u = new Ultoo();
u.UserName = Request["username"].ToString();
u.Password = Request["password"].ToString();
QuestionGenerator.Initialize(); // from the main thread
new Thread(u.CompletePoll).Start();


public static class QuestionGenerator
{
    public static void Initialize()
    {
        var firstParts = FirstParts;
        var secondParts = SecondParts;
        // merely reading the properties is enough to initialise them, so ignore the results
    }
}

Once the thread has started, after Initialize() has been called, you can access FirstParts and SecondParts reliably.

OTHER TIPS

You need to look at the inner exception. A TypeInitializerException simply means an exception has been thrown from inside a constructor. The exception would be generated from the code contained within the call to QuestionGenerator.GetNextQuestion();

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top