DEV Community

Mayuki Sawatari
Mayuki Sawatari

Posted on

AssemblyResolutionException is thrown when publishing a Blazor web assembly.

Original: https://medium.com/@mayuki/assemblyresolutionexception-is-thrown-when-publishing-a-blazor-web-assembly-5eb35daa9b0a

Symptoms

Publishing from Visual Studio and dotnet publish -c Release throw the following AssemblyResolutionException at the build step of IL Linker.

Fatal error in Mono IL Linker
C:\Users\Tomoyo\.nuget\packages\microsoft.aspnetcore.components.webassembly.build\3.2.1\targets\Blazor.MonoRuntime.targets(326,5): error : Unhandled exception. Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'SQLitePCLRaw.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1488e028ca7ab535' [C:\Users\Tomoyo\Source\Repos\BlazorApp6\BlazorApp6\BlazorApp6.csproj]
   ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'SQLitePCLRaw.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1488e028ca7ab535'
     at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
     at Mono.Linker.AssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
     at Mono.Linker.LinkContext.Resolve(IMetadataScope scope)
     at Mono.Linker.LinkContext.Resolve(IMetadataScope scope)
     at Mono.Linker.LinkContext.ResolveReferences(AssemblyDefinition assembly)
     at Mono.Linker.Steps.LoadReferencesStep.ProcessReferences(AssemblyDefinition assembly)
     at Mono.Linker.Steps.LoadReferencesStep.ProcessAssembly(AssemblyDefinition assembly)
     at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
     at Mono.Linker.Driver.Execute(String[] args, ILogger customLogger)
     at Mono.Linker.Driver.Main(String[] args)

Cause

IL Linker is a build step that searches for all dependent assembly references and trims unnecessary ILs.

The exception is raised when it can't find an assembly while looking for a dependent assembly, and it usually doesn't occur for NuGet package-referenced ones.

However, depending on your package dependencies, an exception may be raised. For example, it occurs when referencing a package like Microsoft.CodeAnalysis.Workspaces.

The reason for this is that Microsoft.CodeAnalysis.Workspaces.Common refers to the SQLitePCLRaw.bundle_green package as PrivateAssets="all".
As a result, the package does not contain a package reference to SQLitePCLRaw.bundle_green. Therefore, if you add the package to your application, SQLitePCLRaw.bundle_green package is not considered a dependency, and the assembly is not included.

This problem happens in situations where you don't treat it as a package dependency, but it does reference an assembly.

Workaround

There are two workarounds to this problem:

1. Reference the required package in the project.

Add the package that contains the missing assembly to the project.

2. Disable IL Linker.

Some packages, such as SQLitePCLRaw.bundle_green, get an error when adding a reference. In that case, disable the IL Linker itself.

https://docs.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/configure-linker?view=aspnetcore-3.1

<PropertyGroup>
  <BlazorWebAssemblyEnableLinking>false</BlazorWebAssemblyEnableLinking>
</PropertyGroup>

Top comments (0)