Losing value of static variable declared in Globa.asax and initialized in onstart event of webrole

Answered Losing value of static variable declared in Globa.asax and initialized in onstart event of webrole

  • Mittwoch, 4. April 2012 07:43
     
     

    Hi all,

    I have declared a static string "drivePath" in Gloabal.asax.cs. In Onstart event of webrole I am assigning a value to it. I am accessing this value on my web page but it shows as if the variable is just declared. The value I get for the My code is as follows

    Global .asax.cs

    public static string drivePath = string.Empty;

    WebRole.cs

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

    I am assigning value to WebRole.drivePath in "MountAzureDrive()" function.

    On Web Page

    On the web page in some function I am accessing the drivePath as "Global.drivePath"and I am getting value as string.Empty.

    My question is Why is not preserving the value of drive path? What do I have to here if I want to preserve the value of drivepath and get it?

    Please help me

    Thanks in advance.

Alle Antworten

  • Mittwoch, 4. April 2012 10:53
     
     Vorgeschlagene Antwort

    Hi BhimrajG,

    The code running in the WebRole is not the same process as your web application (Global.asax.cs). What you could do is the following:

    • WebRole: Mount the drive
    • WebRole: Store the drive letter as an environment variable
    • Global.asax.cs: Read the drive letter from the environment variable.
    • ...

    Follow this article for more information: http://www.codeproject.com/Articles/81413/Windows-Azure-Drives-Part-1-Configure-and-Mounting 

    Sandrino


    Sandrino Di Mattia | Twitter: http://twitter.com/sandrinodm | Azure Blog: http://fabriccontroller.net/blog | Blog: http://sandrinodimattia.net/blog

  • Donnerstag, 5. April 2012 09:01
     
     Beantwortet

    OnStart (Role) is in another process than your web page (pages, Global.asax ..). At least when using Full IIS Mode

    So I implemented/moved the code of mounting azure drive from onstart event of WebRole to Application_start event of Global.asax.cs file and it worked.

    After this implementation for testing you need to restart your emulator.


    • Als Antwort markiert BhimrajG Donnerstag, 5. April 2012 09:01
    • Bearbeitet BhimrajG Donnerstag, 5. April 2012 09:02 Added tip
    •