You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET托管Blazor WASM应用(.NET 6)引用共享类库时出现「Cannot load file or assembly」异常求助

Troubleshooting "Cannot load file or assembly" in Blazor WASM with Shared .NET 6 Library

Hey there, I’ve dealt with this exact error a few times in Blazor WASM projects, so let’s walk through the most likely causes and fixes for your issue with the xxxxxx.Contracts library:

  • Target Framework Mismatch
    Blazor WASM has strict requirements for referenced libraries. Double-check that your xxxxxx.Contracts class library is targeting net6.0 (not a platform-specific variant like net6.0-windows or an older standard like netstandard2.0). Open the library’s .csproj file and confirm the line looks like this:

    <TargetFramework>net6.0</TargetFramework>
    

    Platform-specific frameworks won’t load correctly in the WASM runtime.

  • Corrupted Build Output or References
    Often this error comes from wonky build artifacts or broken references. Try these steps:

    1. Run dotnet clean in both your Blazor project and the xxxxxx.Contracts library directory
    2. Delete the bin and obj folders from all projects manually (sometimes dotnet clean misses stuff)
    3. Run dotnet restore to refresh dependencies
    4. Verify your Blazor project uses a project reference to xxxxxx.Contracts, not a file reference to a DLL. File references can break if the DLL moves or gets updated.
  • Linker Trimming Accidentally Removing Types
    Blazor WASM enables linker trimming by default in Release mode to reduce app size. Sometimes it incorrectly marks types from your shared library as unused and strips them out. To test this:

    1. Temporarily disable linking in your Blazor project’s .csproj:
      <BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking>
      
    2. Rebuild and run the app. If the error goes away, you need to configure the linker to preserve your library’s types. Create a Linker.xml file in your Blazor project with this content:
      <linker>
        <assembly fullname="xxxxxx.Contracts">
          <type fullname="*" />
        </assembly>
      </linker>
      
    3. Reference the linker file in your .csproj:
      <LinkerDescriptor>Linker.xml</LinkerDescriptor>
      
  • Unsupported Platform Code in the Shared Library
    The xxxxxx.Contracts library might contain code that’s not compatible with WASM (e.g., Windows-specific APIs, System.Drawing, or native interop calls). Even if it’s a "shared" library, WASM can’t execute platform-specific code.

    • Audit the library for any non-cross-platform types or methods
    • If you find any, use conditional compilation (#if !WASM directives) to exclude them from the client, or abstract the functionality into a platform-specific implementation.
  • Missing Assembly in Deployment
    Check if the xxxxxx.Contracts.dll is actually being deployed with your Blazor app. In your browser’s DevTools:

    1. Go to the Network tab
    2. Refresh the app and look for xxxxxx.Contracts.dll in the list of downloaded files
    3. If it’s missing, verify your build configuration: make sure the library is set to build when you build the Blazor project, and that its output is being copied to the Blazor app’s wwwroot/_framework/bin directory.

If none of these fixes work, try creating a brand new Blazor WASM app targeting .NET 6, add a reference to xxxxxx.Contracts, and see if you can reproduce the error. If the new app works, the issue is likely a corrupted configuration in your original project.

内容的提问来源于stack exchange,提问作者Peter Morris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 19:42:45