簡體   English   中英

PowerShell 7.0.x 無法加載文件或程序集 Microsoft.CodeAnalysis

[英]PowerShell 7.0.x Could not load file or assembly Microsoft.CodeAnalysis

I have a .NET Core 3.1 class Library which uses a nuget package called RazorEngineCore which references Microsoft.CodeAnalysis.CSharp v3.7.0 which then references Microsoft.CodeAnalysis.Common v3.7.0 when I use this in a .NET Core Exe or PowerShell 7.1 it works沒有問題,但是當我使用 PowerShell 7.0.x 時,我收到以下錯誤消息:

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)   at System.Reflection.Assembly.LoadFrom(String assemblyFile)   at System.Reflection.Assembly.LoadFromResolveHandler(Object sender, ResolveEventArgs args)   at System.Runtime.Loader.AssemblyLoadContext.InvokeResolveEvent(ResolveEventHandler eventHandler, RuntimeAssembly assembly, String name)   at System.Runtime.Loader.AssemblyLoadContext.OnAssemblyResolve(RuntimeAssembly assembly, String assemblyFullName)

我啟用了融合日志記錄並且沒有 output 並且內部異常沒有提供對任何丟失 dll 的引用。 我正在調用的 powershell 代碼是這樣的:

    Add-Type -Path "$($PSScriptRoot)\ComponentHealth.Rendering.dll"

    $ViewModel = [PSCustomObject]@{
        Name     = 'Kevin'
    }

    $template = "Hello @Model.Name"

    try {
        $output = [ComponentHealth.Rendering.Rendering]::Render("Bob", $template, $ViewModel) 
    } catch {
        $ex = $_.Exception
        $base = $_.Exception.GetBaseException()
        Write-Host $ex
    }
    Write-Host $output 

我無法弄清楚為什么這在 exe 應用程序或 PowerShell 7.1.x 中有效,但在 PowerShell 7.0.x 中無效。 Unfortunately, I cannot use a different version of PowerShell as this needs to be added to an Azure App Function which is pinned at PowerShell runtime 7.0.3.

我們發現了問題,如果您嘗試導入 dll,其版本高於 PowerShell 核心運行時隨附的版本,您將在嘗試加載 dll 時遇到異常。 我們通過使用低於 PowerShell 7.0.3 附帶的 Microsoft.CodeAnalysis.CSharp 版本重新編譯RazorEngineCore package 解決了這個問題,現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM