[System.Reflection.sembly] :: load(byte [])失败:系统无法找到指定的文件



我得到了用C#编写的Windows服务,该服务执行PowerShell命令。它试图加载Azure存储DLL并失败。

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

DLL在包装版本9.0.0中。错误是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.

有趣的是,如果我在凭据下运行Windows Service,则可以正常运行。但是在机器人帐户下运行时,它会失败。9.3.2甚至不是最新版本

我尝试在Bot帐户下打印GAC,但我看不到与Azure Storage

$n1 = New-PSDrive -Name HKCR -PSProvider 'Microsoft.PowerShell.CoreRegistry' -Root HKEY_CLASSES_ROOT
$n2 = Get-ItemProperty -Path 'HKCR:InstallerAssembliesGlobal' | Get-Member -MemberType NoteProperty

我了解加载DLL时有各种环境。但是我不知道该如何调试?有人可以帮忙一些建议吗?

尝试启用融合记录,然后作为bot用户登录Windows并尝试加载DLL。Fusion Logs可能会表明,您的服务帐户不可读取的DLL可能是由于文件系统权限所致。

这是一个PowerShell脚本,它使更容易切换融合记录的状态:

Param( $LogPath = "c:tempFusion" )
#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:SOFTWAREMicrosoftFusion'
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}

最新更新