由于完整性错误,Blazor WebAssembly 应用程序无法加载

Blazor WebAssembly Application fails to load due to integrity errors

我们开发了一个 Blazor WebAssembly 应用程序,该应用程序已经为特定客户群投入生产使用。

该应用程序在具有标准安全设置的所有浏览器中运行良好。然而,今天早上我接到一位客户的电话,他们的 Chrome 浏览器中根本没有加载应用程序。

我在控制台中看到以下错误:

现在我的问题是,这可能是什么原因造成的?这是浏览器安全设置,还是其他安全设置,例如在服务器上、在代码中等?我该如何解决这个问题?

这是上面提到的错误的图片

发生这种情况的最可能原因是某些防病毒软件会阻止执行下载的 .dll 文件。这就是为什么它在某些网络中有效,但在其他一些网络中无效的原因。

您可以做的,也是 suggested as a Workaround by microsoft,是将所有 .dll 重命名为 .bin - 并更改配置 json。它对我有用。

我为此使用了以下 PowerShell 函数:

Function Hide-BlazorDLL {

    Param(
        [string]$Path = (Get-Location).Path
    )

    <#
        According to the following Links:
        https://github.com/dotnet/aspnetcore/issues/19552
        https://github.com/dotnet/aspnetcore/issues/5477#issuecomment-599148931
        https://github.com/dotnet/aspnetcore/issues/21489
        https://gist.github.com/Swimburger/774ca2b63bad4a16eb2fa23b47297e71 
    #>

    # Test if path is correct and accessible
    $WorkingDir = Join-Path $Path "_framework"
    if (!(Test-Path $WorkingDir)) { Throw "Wrong path $Path. current location must be wwwroot folder of published application." }

    # Get All Items
    $AllItems = Get-ChildItem $WorkingDir -Recurse
    $DLLs = $AllItems | Where-Object { $_.Name -like '*.dll*' }
    $BINs = $AllItems | Where-Object { $_.Name -like '*.bin*' }

    # End script if no .dll are found
    if ($DLLs) { 

        # Delete all current .bin files
        if ($BINs) {
            Remove-item $BINs.FullName -Force
        }
    
        # Change .dll to .bin on files and config
        $DLLs | Rename-item -NewName { $_.Name -replace ".dll\b",".bin" }
        ((Get-Content "$WorkingDir\blazor.boot.json" -Raw) -replace '.dll"','.bin"') | Set-Content "$WorkingDir\blazor.boot.json"
    
        # Delete Compressed Blazor files
        if (Test-Path "$WorkingDir\blazor.boot.json.gz") {
            Remove-Item "$WorkingDir\blazor.boot.json.gz"
        }
        if (Test-Path "$WorkingDir\blazor.boot.json.br") {
            Remove-Item "$WorkingDir\blazor.boot.json.br"
        }
    
        # Do the same for ServiceWorker, if it exists
        $ServiceWorker = Get-Item "$Path\service-worker-assets.js" -ErrorAction SilentlyContinue
        if ($ServiceWorker) {
            ((Get-Content $ServiceWorker.FullName -Raw) -replace '.dll"','.bin"') | Set-Content $ServiceWorker.FullName
            Remove-Item ($ServiceWorker.FullName + ".gz")
            Remove-Item ($ServiceWorker.FullName + ".br")
        }
    }
    else {
        Write-Host "There are no .dll Files to rename to .bin" 
    }
}

基本上,您需要导航到已发布应用程序的 wwwroot 文件夹和 运行 那里的功能。例如:

PS D:\inetpub\wwwroot\<appname>\wwwroot> Hide-BlazorDLL

当我在匿名浏览器中测试我的应用程序时,出于某种原因这个错误一直在我身上发生 window (Google Chrome)。

如果出现完整性错误,请尝试使用普通浏览器window。

此外,如果您使用的是 Cloudflare CDN,请不要忘记在缓存中“清除所有内容”。

我的解决方案是删除客户端和服务器项目文件夹中的 objbin 文件夹