user459866
user459866

Reputation: 121

Add-Type -ReferencedAssemblies fails with could not load or find assembly. Solution

The solution to this problem is add the DLLs to the GAC as was suggested in one of the responses to my posting. As I noted in one of my responses, the gacutility will not be available in the environment where this process needs to run. Because of this the simple solution of the gacutility is not an option. To resolve this I derived a Posh function that will add DLLs to the GAC:

param([string]$dllPath)

[string]$publicToken = $null [string]$val = $null [string]$version = $null

if (test-path) $dllPath) { $baseFileName = [System.IO.Path]::GetFileNameWithoutExtension($dllPath) $targetName = "c:\windows\assembly\GAV_MSIL\" + $baseFileName

# Get the key and public token
$val = sn -Tp $dllPath

# Get the version w/o loading
$version = [System.reflection.AssemblyName]::GetAssemblyName($dllPath).Version

# Proceed if the token is valid
if ($val -ne -null)
{
    $vals = $val.split(" ")
    $publicToken = $vals[$vals.length-1]
    $targetNameSub=$targetName + "\" + $version + "__" + $publicToken

    if (!(test-path $targetName))
    {
        Md $targetName | Out-Null
    }

    Md $targetNameSub | Out-Null

    # Copy the DLL to the GAC
    copy-item $dllPath $targetNameSub | Out-Null
}

}

I have tested this and it works very well. In my research I found something that indicated that the gacutility makes entries to the registry which I am not doing. But this function does work quite well.

I have tried to reverse the process to come up with a Posh function to remove the GAC entries but I have not been successful yet each time getting an access denied on the DLL file removal.

Upvotes: 1

Views: 2429

Answers (2)

cristobalito
cristobalito

Reputation: 4282

Add the Assembly to your GAC:

gacutil /i Assembly.dll

Upvotes: 1

JaredPar
JaredPar

Reputation: 754665

It sounds like the problem is that tools.Utilities.dll itself is fine but one if it's dependencies is unavailable inside of c:\Program Files\subDir. This is suggested by both the error message and the fact that moving the DLL to a different folder fixes the issue. It's likely the missing dependency is available in the new folder.

The easiest way to verify this is to use fuslogvw.exe to see exactly what error is preventing tools.Utilities.dll from loading.

Upvotes: 3

Related Questions