ASP.NET托管Blazor WASM应用(.NET 6)引用共享类库时出现「Cannot load file or assembly」异常求助
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 yourxxxxxx.Contractsclass library is targeting net6.0 (not a platform-specific variant likenet6.0-windowsor an older standard likenetstandard2.0). Open the library’s.csprojfile 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:- Run
dotnet cleanin both your Blazor project and thexxxxxx.Contractslibrary directory - Delete the
binandobjfolders from all projects manually (sometimesdotnet cleanmisses stuff) - Run
dotnet restoreto refresh dependencies - 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.
- Run
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:- Temporarily disable linking in your Blazor project’s
.csproj:<BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking> - 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.xmlfile in your Blazor project with this content:<linker> <assembly fullname="xxxxxx.Contracts"> <type fullname="*" /> </assembly> </linker> - Reference the linker file in your
.csproj:<LinkerDescriptor>Linker.xml</LinkerDescriptor>
- Temporarily disable linking in your Blazor project’s
Unsupported Platform Code in the Shared Library
Thexxxxxx.Contractslibrary 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 !WASMdirectives) to exclude them from the client, or abstract the functionality into a platform-specific implementation.
Missing Assembly in Deployment
Check if thexxxxxx.Contracts.dllis actually being deployed with your Blazor app. In your browser’s DevTools:- Go to the Network tab
- Refresh the app and look for
xxxxxx.Contracts.dllin the list of downloaded files - 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/bindirectory.
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

