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?
That isn’t right. This checks
HttpContext.Currentonce, 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 requireHttpContext.Currentto not benull. 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 whetherHttpContext.Currentis 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
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 ifFirstPartscannot get set.However, it still requires that you do not read
FirstPartsfrom a thread. You can avoid that problem by reading it once before starting the thread:Once the thread has started, after
Initialize()has been called, you can accessFirstPartsandSecondPartsreliably.