Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Use Local Storage to Store Files During Startup

Use Local Storage to Store Files During Startup

Updated: October 24, 2014

You can use a local storage resource to store files created by your startup task which will be accessed later by your application.

To use a local storage resource in your startup task, you need to create a LocalStorage element in your ServiceDefinition.csdef file, and then create an environment variable to reference the local storage resource location. Then the Startup task and the application will be able to read and write files to the local storage resource.

To create the local storage resource, you must edit the ServiceDefinition.csdef file. Add a LocalResources section to the ServiceDefinition.csdef file, if a LocalResources section does not exist. Then add the LocalStorage element within the LocalResources section. Give the local storage resource a unique name and an appropriate size for your startup task. In this example, the unique name is StartupLocalStorage.

To create the environment variable that will reference the local storage location, add an Environment section and a Variable section to the Startup section of the ServiceDefinition.csdef file. The Variable element must have a unique name for the environment variable that will be used in the startup task. In this example, the environment variable is named PathToStartupStorage.

Within the Variable section of the ServiceDefinition.csdef file, add the RoleInstanceValue element. The xPath attribute must use the same unique name for the local storage resource as was referenced in the LocalStorage element. The unique name for the local storage resource must be after the @name=, and must be in single quotes. In this example, the xPath attribute is "/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='StartupLocalStorage']/@path" and contains the local storage resource name StartupLocalStorage.

The relevant sections of the ServiceDefinition.csdef file are shown here:

    <!-- Create the Local Storage used by the startup task. -->
      <LocalStorage name="StartupLocalStorage" sizeInMB="5"/>
      <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple">
          <!-- Create the environment variable that informs the startup task where to find its Local 
               Storage. %PathToStartupStorage% resolves to the fully qualified path to the location 
               of the Local Storage.-->
          <Variable name="PathToStartupStorage">
            <RoleInstanceValue xpath="/RoleEnvironment/CurrentInstance/LocalResources/LocalResource[@name='StartupLocalStorage']/@path" />

The Startup.cmd batch file uses the PathToStartupStorage environment variable to create the file MyTest.txt on the local storage location, as shown here:

REM   Create a simple text file.

ECHO This text will go into the MyTest.txt file which will be in the    >  "%PathToStartupStorage%\MyTest.txt"
ECHO path pointed to by the PathToStartupStorage environment variable.  >> "%PathToStartupStorage%\MyTest.txt"
ECHO The contents of the PathToStartupStorage environment variable is   >> "%PathToStartupStorage%\MyTest.txt"
ECHO "%PathToStartupStorage%".                                          >> "%PathToStartupStorage%\MyTest.txt"

REM   Exit the batch file with ERRORLEVEL 0.

EXIT /b 0

To access the local storage resource, the application must retrieve the path from the GetLocalResource method. Standard file read and write operations will then work to read and write the contents of the local storage resource. The following sample shows how to read the contents of MyTest.txt from the local storage resource, and display it on the home page of an MVC 3 application:

using Microsoft.WindowsAzure.ServiceRuntime;
using System;
using System.Text;
using System.Web.Mvc;

namespace StartupExercise.Controllers
    public class HomeController : Controller
        public ActionResult Index()
            string SlsPath = RoleEnvironment.GetLocalResource("StartupLocalStorage").RootPath;

            string s = System.IO.File.ReadAllText(SlsPath + "\\MyTest.txt");

            ViewBag.Message = "Contents of MyTest.txt = " + s;

            return View();

See Also

© 2015 Microsoft