Startup tasks are often used in Windows Azure to install things or make other configuration changes to the virtual machine hosting your role code. Sometimes those setup steps are things you don’t want to execute when you’re running and testing locally via the compute emulator. (For example, you may want to skip a lengthy download or an installation of something you already have on your computer.)
With SDK 1.5, there are a few supported ways for code to determine whether or not it’s running emulated. From .NET code, there’s the new
RoleEnvironment.IsEmulated static property. From other code (like a batch file startup task), SDK 1.5 adds a nice way to put the value of
IsEmulated into an environment variable. Here’s the definition of a startup task that will get an
EMULATED environment variable telling it whether or not the role is running under the compute emulator.
<Startup> <Task executionContext="elevated" commandLine="startup\startup.cmd"> <Environment> <Variable name="EMULATED"> <RoleInstanceValue xpath="/RoleEnvironment/Deployment/@emulated" /> </Variable> </Environment> </Task> </Startup>
xpath attribute. There are many useful paths you can provide that will help you get at things like the port for an endpoint, the location of a local storage resource, or configuration setting values. See the MSDN documentation for the full details: “xPath Values in Windows Azure” and “WebRole Schema”.
Now all we need to do is make use of this environment variable in our startup task. The first line of
startup.cmd simply checks the environment variable and immediately exit if it’s set to
if "%EMULATED%"=="true" goto :EOF
I used to write all sorts of tests in my startup tasks to try to avoid rerunning installers on my laptop, and this nice feature is going to save me the effort.