Motoko
Motoko

Reputation: 1132

[System.Reflection.Assembly]::Load(byte[]) failed: The system cannot find the file specified

I got a Windows service written in C# that executes Powershell command. It tries to load Azure Storage dll and fails.

$bytes = [System.IO.File]::ReadAllBytes($azureStorageDll) // local path of the dll
[System.Reflection.Assembly]::Load($bytes) | Out-Null

The dll is in package version 9.0.0. And the error is Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=9.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Interestingly, if I run the Windows service under my credentials, it works. But it fails when running under the bot account. And 9.3.2 is not even the latest version

I tried printing out the GAC under the bot account but I don't see anything related to Azure Storage

$n1 = New-PSDrive -Name HKCR -PSProvider 'Microsoft.PowerShell.Core\Registry' -Root HKEY_CLASSES_ROOT
$n2 = Get-ItemProperty -Path 'HKCR:\Installer\Assemblies\Global' | Get-Member -MemberType NoteProperty

I understand there are various contexts when loading dlls. But I don't know how to debug this? Could someone please help with some suggestions?

Upvotes: 0

Views: 912

Answers (1)

Rich Moss
Rich Moss

Reputation: 2384

Try enabling Fusion logging, then signing in to windows as the bot user and attempting to load the DLL. Fusion logs will probably show that there is a dependent DLL that is not readable by your service account, probably due to filesystem permissions.

Here's a Powershell script that makes it easier to toggle the state of Fusion logging:

Param( $LogPath = "c:\temp\Fusion" )

#restart as admin if not started as admin 
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { 
    Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -PassThru -Verb RunAs | Out-null; exit 
}
$RegKey = 'HKLM:\SOFTWARE\Microsoft\Fusion'
If(!(Test-Path $RegKey)) {New-Item -Path $RegKey -ItemType Container | Out-Null}
$NewValue = If((Get-ItemProperty $RegKey).EnableLog -eq 1){0} Else {1}
Set-ItemProperty -Path $RegKey -Name EnableLog        -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name ForceLog         -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogFailures      -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogResourceBinds -Value $NewValue -Type DWord
Set-ItemProperty -Path $RegKey -Name LogPath          -Value $LogPath  -Type String
If(!(Test-Path $LogPath -PathType Container)){New-Item $LogPath -ItemType Directory | Out-Null}
Write-Host "$(If($NewValue -eq 1){'Enabled'}Else{'Disabled'}) Fusion Logging at $LogPath"
If(!$Host.Name.Contains("ISE")){Pause}

Upvotes: 1

Related Questions