GAC installation in many ways!

Posted: November 29, 2011 in .Net Framework, .Net Tools, PowerShell
Tags: , , , ,

Every BizTalk Developer knows that one of the tedious tasks during BizTalk Development is Deployment, as a Developer you will find yourself spending most of your time deploying / redeploying your applications.
One strategy I used to save some time from this tasks is just to Rebuild non BizTalk artifacts such as Dataaccess, Helper and Pipeline Component, then just use gacutil.exe to install the latest build to GAC.

Over the years I have collected several ways on how to automate the boring task of installing to GAC. I listed some of them below for reference.

The great gacutil.exe that everyone knows.

Using Windows Folder Context Menu.

My favorite of them all, Powershell!

And Below is my modified version of the same powershell script that allows pipeline input and selection of framework version.

PARAM
(
        [parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$True)]
        [alias("FullName")]
        [alias("Path")]
        [string[]]$AssemblyFullNames,
		[Parameter(Position=1)] [String] $CLRVersion = "4.0.0.0"
)
BEGIN
{
	Write-Host "Using ToolVersion: $CLRVersion"
}
PROCESS
{

FUNCTION Invoke-CLR4PowerShellCommand {
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory=$true)]
        [ScriptBlock]
        $ScriptBlock,

        [Parameter(ValueFromRemainingArguments=$true)]
        [Alias('Args')]
        [object[]]
        $ArgumentList
    )

    if ($PSVersionTable.CLRVersion.Major -eq 4) {
        Invoke-Command -ScriptBlock $ScriptBlock -ArgumentList $ArgumentList
        return
    }

    $RunActivationConfigPath = $Env:TEMP | Join-Path -ChildPath ([Guid]::NewGuid())
    New-Item -Path $RunActivationConfigPath -ItemType Container | Out-Null
@"
<!--?xml version="1.0" encoding="utf-8" ?-->

"@ | Set-Content -Path $RunActivationConfigPath\powershell.exe.activation_config -Encoding UTF8

    $EnvVarName = 'COMPLUS_ApplicationMigrationRuntimeActivationConfigPath'
    $EnvVarOld = [Environment]::GetEnvironmentVariable($EnvVarName)
    [Environment]::SetEnvironmentVariable($EnvVarName, $RunActivationConfigPath)

    try {
        & powershell.exe -inputformat text -command $ScriptBlock -args $ArgumentList
    }
	Catch { write-host "An Error was encountered" }
	finally {
        [Environment]::SetEnvironmentVariable($EnvVarName, $EnvVarOld)
        $RunActivationConfigPath | Remove-Item -Recurse
    }

}

	FUNCTION CopyToGAC {
	PARAM
	(
		[Parameter(Position=0)]
		[string[]]$files
		,[Parameter(Position=1)]
		[String] $NetFxVersion
	)

	BEGIN
	{

	    $ErrorActionPreference = "Stop"

	    if ( $null -eq ([AppDomain]::CurrentDomain.GetAssemblies() |? { $_.FullName -eq "System.EnterpriseServices, Version=$NetFxVersion, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" }) ) {
	        [System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=$NetFxVersion, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a") | Out-Null
	    }

	    $publish = New-Object System.EnterpriseServices.Internal.Publish

	}

	PROCESS
	{
	    foreach( $file in $files ) {
	        $assembly = $file
	        if ((!$assembly) -or (-not (Test-Path $assembly -type Leaf)) ) {
	            throw "The assembly '$assembly' does not exist."
	        }

	        if ( [System.Reflection.Assembly]::LoadFile( $assembly ).GetName().GetPublicKey().Length -eq 0 ) {
	          throw "The assembly '$assembly' must be strongly signed."
	        }

	        Write-Output "Copying to GAC: $assembly"

	        $publish.GacInstall( $assembly )
		}
	}
	}

If ($CLRVersion -ne "2.0.0.0")
{
	$sBlock = $Function:CopyToGAC
	Invoke-CLR4PowerShellCommand -ScriptBlock $sBlock -ArgumentList $AssemblyFullNames, $CLRVersion, $AssemblyPattern
}
else
{
	CopyToGAC $AssemblyFullNames $CLRVersion $AssemblyPattern
}
}

Did I missed some other ways? please let me know and I will be very much willing to update here.

Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s