我有下一个静态类:
public static class GlobalVar
{
public static string DatabaseName = "ProjectDatabase.mdf";
public static AdminClass Admin;
public static string TruePath = AppDomain.CurrentDomain.BaseDirectory;
public static string TimeStampPattern = "dd/MM/yyyy HH:mm";
static GlobalVar()
{
TruePath = TruePath.Remove(TruePath.Length - 1);
Admin = new AdminClass("Admin", "Admin");
GlobalStatus = new Dictionary<string, string>();
string Query = "SELECT * FROM global_status";
DataTable Types = MyAdoHelper.ExecuteDataTable(GlobalVar.DatabaseName, Query);
foreach (DataRow Status in Types.Rows)
{
GlobalStatus.Add(Status["title"].ToString(), Status["info"].ToString());
}
}
public static Dictionary<string, string> GlobalStatus;
public static string BasePath = HttpContext.Current.Request.ApplicationPath;
}
GlobalStatus 是一个字典,接收站点可能返回的自定义错误。当我启动项目(Microsoft Visual Web developer 2008)时,它给出了一个错误:
Object reference not set to an instance of an object.
尝试从 GlobalStatus 获取值时(例如 GlobalVar.GlobalStatus["Page_NoAccess"])。
仅一分钟后,当我再次启动该项目时,它运行良好。
我该如何解决这个问题,比如强制浏览器等待它填充 GlobalStatus。我需要这个类只初始化一次,因为它包含全局变量,我不想在每个浏览器请求中都调用它们。
谢谢