C# 丢失在Global.asax中声明并在webrole的onstart事件中初始化的静态变量的值

C# 丢失在Global.asax中声明并在webrole的onstart事件中初始化的静态变量的值,c#,asp.net,azure,C#,Asp.net,Azure,我在Gloabal.asax.cs中声明了一个静态字符串“drivePath”。在webrole的Onstart事件中,我给它赋值。我在我的网页上访问这个值,但它显示的好像是刚刚声明的变量。我得到的My代码的值如下 Global.asax.cs public static string drivePath = string.Empty; public override bool OnStart() { MountAzureDrive(); Global.drivePath =

我在Gloabal.asax.cs中声明了一个静态字符串“drivePath”。在webrole的Onstart事件中,我给它赋值。我在我的网页上访问这个值,但它显示的好像是刚刚声明的变量。我得到的My代码的值如下

Global.asax.cs

public static string drivePath = string.Empty;
public override bool OnStart()
{
    MountAzureDrive();
    Global.drivePath = WebRole.drivePath;
    return base.OnStart();
}
WebRole.cs

public static string drivePath = string.Empty;
public override bool OnStart()
{
    MountAzureDrive();
    Global.drivePath = WebRole.drivePath;
    return base.OnStart();
}
我正在为“MountAzureDrive()”函数中的WebRole.drivePath赋值

在网页上

在某个函数的网页上,我以“Global.drivePath”的形式访问驱动路径,得到的值为string.Empty

我的问题是,为什么不保留驱动路径的价值?如果我想保留drivepath的价值并获得它,我必须做什么

请帮帮我


提前感谢。

OnStart(角色)正在您的网页(pages,Global.asax..)之外的另一个进程中。至少在使用时。

在WebRole中运行的代码与web应用程序(Global.asax.cs)的进程不同。您可以执行以下操作:

  • WebRole:装载驱动器
  • WebRole:将驱动器号存储为环境变量
  • Global.asax.cs:读取环境变量中的驱动器号
  • 有关详细信息,请参阅本文:


    Sandrino

    那么,在OnStart事件中安装驱动器后,如何获取驱动器路径?因为我在Onstart事件中将值分配给drivePath,您可以看到,在web应用程序的整个生命周期中访问它。我所有的操作都是在同一条车道上执行的。你看过Global.asax的呼叫吗?或者您是否有多个驱动器,需要知道是哪一个?您的意思是我必须在应用程序\u Start of Global.asax中调用MountAzureDrive()函数,而不是在WebRole的Onstart中调用?如果在Onstart中装载它是web角色正常工作的必要先决条件,则可能是一个好主意。您可以在Global.Application\u Start中调用CloudDrive.GetMountedDrives,并将驱动器号放入IIS进程的静态字段中。如果您决定从Global.asax挂载它,您需要检查它是否已经存在,因为(可能)有多个IIS应用程序池/工作进程。我在Global.asax的应用程序启动事件中实现了MountAzureDrive()方法,而不是WebRole onstart,但我仍然在网页的drivePath中获得string.Empty值