Updated 10 may 2011, added some clarification

Startup tasks are the new way to run a script or file in Windows Azure, and we can run them under Elevated (Administrator) context. For this to work, add the assembly you want to add to the GAC to your project and set copy local to true. GacUtil is not installed in the Azure WebRole, so you’ll have to supply it as well.

Edit the .CSDEF file and add a Startup task with executionContext elevated:

<ServiceDefinition name=MyProject xmlns=http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition>
   <WebRole name=MvcWebRole1>
<
Startup>
         <Task commandLine=RegisterGAC.cmd executionContext=elevated taskType=simple />
      </Startup>

Now add the RegisterGAC.cmd AND gacutil.exe from the platform SDK to your project, set the build action to none, and copy to output directory to Copy Always for both files!

In the RegisterGAC.cmd enter the following 2 lines:

gacutil /nologo /i .\Microsoft.IdentityModel.dll 
exit /b 0

Remember that the startup task runs from the /BIN folder. This file must be saved as Unicode without “byte code order”, choose advanced save options: UTF-8 without signature

There is also the possibility to set the taskType:

  • Simple – needs to complete before the role continues
  • Background – runs parallel with the role (startup)
  • Foreground – runs parallel with the role, but needs to finish before the role can shutdown

Also remember that the right .Net 4.0 gacutil.exe is in this directory: C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools

Using Windows Update packages (MSU)

Another way to install the Windows Identity Framework (WIF) is to include the windows update package for WIF.

Download the correct WIF msu file for the Azure Role:
v1 = server 2008  =  Windows6.0-KB974405-x64.msu
v2 = server 2008 R2 = Windows6.1-KB974405-x64.msu

And add it to your project and set “Copy to Output Directory Always” to “Copy Always”.
To run a MSU update package use the following startup task:

@echo off sc config wuauserv start=demand wusa.exe "%~dp0Windows6.1-KB974405-x64.msu" /quiet /norestart sc config wuauserv start=disabled exit /b 0
The ~dp0 resolves to the directory where the scripts are being extracted (the correct bin folder)
Use the exit /b 0 to signal the end of the batch file back to Azure.

12 thoughts on “Adding assemblies to the GAC in Windows Azure with Startup Tasks

  1. I still seem to be getting problem even having independantly tried both methods. I’ve confirmed the files are being copied to the bin (before being packaged) and am able run the cmd scripts on the local machine. But once I try to use it in the cloud the same error pops up. Anyway ideas?

  2. Make sure the Microsoft.IdentityModel.dll is set to copy local, and add more debugging info to the command script to figure out if there is a directory wrong.

    Log in with Remote Desktop and run the Command script from the bin folder to see if it is executed.

    Most problems are folder related.

  3. The first one did not work for me, because my machine is a 32 bit and Windows Azure is 64 bit.
    In the second one, I was not sure whether Azure is Windows Server 2008 or Windows Server 2008 R2. I tried Windows6.0-KB974405-x64.msu and the problem got solved for me. Thanks for this blog.

  4. Pingback: Azure Service Configuration Error - Windows Azure Blog

  5. First one worked for me. Allthough I had to try several times.

    I think you need to include gacutil.exe.config and gacutilrc.dll as well.

  6. Pingback: Windows Azure and Cloud Computing Posts for 4/10/2012+ - Windows Azure Blog

  7. Pingback: Correct gacutil command syntax for azure running windows server 2012 - Windows Azure Blog

  8. Very Important to add gacutil.exe.config file to the Project. I wasted a lot of time debugging to figure the issue.

  9. Pingback: Web Role with MVC4 Just Recycles on Windows Azure - Windows Azure Cloud Integration Engineering - Site Home - MSDN Blogs

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>