Thursday, 11 July 2013

Installing assemblies to GAC on Azure Instances

After a four months of interval I again restarting to blog in this blog. Sorry for those who asked some queries which I could not answer on time, was busy with work.

Normally, when we refer some assemblies in Web (or Worker) role project, we will be changing the Copy Local property of the assembly reference to True, which make sure the assembly to get copied with the package and deployed to Azure instances. This helps deploying assemblies that private and will be referred from any of a directory from the application path.

But in some cases, we required to deploy some assemblies to the GAC on the Azure instances and refer from the project. For example – Custom HTTP handler assemblies, HTTP Modules, etc.

To install assemblies into GAC on development servers, we will be using various methods including gacutil.exe command utility. But GACUtil.exe command will not exist in the Azure instances. When I tried coping gacutil.exe and related config file with the package and deploy to Azure and used startup script to deploy my package. But failed. I thought it should be possible by installing .NET runtime as part of Azure startup just to get the gacutil.exe command to work, which is a wrong sign to me to go with.

Finally, I decided to install the assembly using PowerShell script. Below are the steps I used to install my custom assembly into GAC on Azure VM. (Just to say, I did not invent anything here, get the pieces from net and used together).

Getting Ready:

Step 1: Created a sample Azure Web Role project (InstallGACdllEx) and verified the application works perfectly.

Step 2: Added a Class Library in to the project and named it as MyGACdll. This is going to be my custom assembly, which I need to install into GAC on Azure instance.

Step 3: Added a custom method on the project, to verify.
public class Verify
{
    public string SayHello(string myName)
    {
        return "Hello " + myName;
    }
}
Step 4: To install the assembly install into GAC, the assembly require signing with Strong Name Key. So, open the Properties of the project, and select the Signing tab.

Select the Sign the assembly checkbox and choose the <New…> from the dropdown box.

There will be a Create Strong Name Key popup.

Step 5: Provide Key file name and password. Press OK.


Finally my MyGACdll project and the solution look like this.


Step 6: Added the MyGACdll project as reference to the WebRole1 project. Make the Copy Local property to Local.


Step 7: Added below code in the page load event to show list of GAC assemblies in the default page.

Step 8: Deployed to Azure and browsed the deployed url. The page shows, the list of GAC assemblies into the default page.


Installing assembly to GAC:

To install assembly into the GAC on Azure instances, we can use Startup Script. Verify the below links for more information on startup script and to run PowerShell script on startup.

http://www.dotnettwitter.com/2011/11/windows-azure-startup-tasks.html
http://www.dotnettwitter.com/2011/11/executing-powershell-script-with.html

Step 1: Added Add-AssemblyToGlobalAssemblyCache.ps1 PowerShell script file and changed Copy to Output Directory property to Copy Always.

The Add-AssemblyToGlobalAssemblyCache.ps1 file can find from the link here.

Step 2: Added Startup script (StartupTask.cmd) and changed Copy to Output Directory property to Copy Always.

Step 3: Added below script in StartupTask.cmd file.
if "%EMULATED%"=="true" goto EOF

powershell -command "Set-ExecutionPolicy Unrestricted" 2>> error.out
powershell .\Add-AssemblyToGlobalAssemblyCache.ps1 -AssemblyName E:\sitesroot\0\bin\MyGACdll.dll 2>> error.out

exit /b 0
As mentioned in the script, first set the execution policy and call the Add-AssemblyToGlobalAssemblyCache.ps1 with the assembly name to install the assembly to GAC.

Step 4: Save the StartupTask.cmd file to Unicode by selecting File -> Advanced Save Options…


Step 5: Added StartupTask.cmd file as startup script into the service definition file.


Step 6: Now deployed the cloud app to Azure, and verified the url again. Now the MyGACdll.dll will be listed in the page.


So, now the MyGACdll.dll assembly is deployed to GAC.


0 Responses to “Installing assemblies to GAC on Azure Instances”

Post a Comment