Skip to content

Asp.Net 5.0 Blazor Assembly.Load does not create types that link to types in loaded assemblies #42207

@allderidge

Description

@allderidge

Describe the bug

When using Blazor running in the browser with dotnet 5.0.0-preview.8.20407.11 an assembly loaded using Assembly.Load(byte[] rawAssembly, byte[]? rawSymbolStore) will create types that reference assemblies that appear to be duplicates of assemblies that have already been loaded preventing any use of shared types. This behavior is different to other non WASM runtimes and to the dotnet 3.2.0 WASM runtime.

The bug is not present in the release of WASM Blazor in dotnet core 3.2.0 and was found during an upgrade of existing code.

E.g.

  • A Type A implements interface IA
  • Interface IA is defined in an assembly that the a Blazor component is build against
  • Type A is defined in an project that references the project interface IA is present in
  • Interface IA will be resolved when the Blazor component loads.
  • Type A is defined in an assembly loaded by Assembly.Load
  • In the dotnet 5.0 WASM runtime the interface IA implemented by Type A is defined in an assembly that is different to the interface IA referenced by the Blazor component meaning that the component cannot cast Type A to Interface IA.
  • The same code run in a stardard dotnet 5.0 runtime is correctly able to perform the cast.

To Reproduce

A reproduction based on the Blazor starter project is available from: https://github.com/allderidge/BlazorBugReport_AssemblyLoad

There are two solutions:

  • AssemblyLoadTest_5_0/AssemblyLoadTest.sln Reproduces the bug and illustrates that a test in a standard runtime passes.
  • AssemblyLoadTest_3_2_0/AssemblyLoadTest.sln Illustrates the bug is not present in the dotnet core 3.2

The web page shown running in dotnet 5.0:

image

The web page shown running in dotnet core 3.2

image

Illustrative code

var assemblyData = await Client.GetFromJsonAsync<DynamicAssembly>("Assembly");
var assembly = Assembly.Load(assemblyData.AssemblyDllBytes, assemblyData.AssemblyPdbBytes);

var dynamicType = assembly.GetType("AssemblyLoadTest.Dynamic.SharedInterfaceImplementation", true) ?? throw new ApplicationException("Unable to load dynamic type");
var sharedInterface = dynamicType.GetInterfaces().First(e => e.Name == nameof(ISharedInterface));
SharedAssemblyMatches = sharedInterface.Assembly == typeof(ISharedInterface).Assembly;  // should be true, false in bug repo
SharedInterfaceMatches = sharedInterface == typeof(ISharedInterface); // should be true, false in bug repo

var instance = Activator.CreateInstance(dynamicType);
CanCast = instance is ISharedInterface;  // should be true, false in bug repo

Name = ((ISharedInterface)instance).HelloWorldName; // causes a invalid cast exception

Further technical details

  • ASP.NET Core version: 5.0.0-preview.8.20414.8
  • Include the output of dotnet --info
 Version:   5.0.100-preview.8.20417.9
 Commit:    fc62663a35

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\

Host (useful for support):
  Version: 5.0.0-preview.8.20407.11
  Commit:  bf456654f9

.NET SDKs installed:
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.700-preview-009601 [C:\Program Files\dotnet\sdk]
  3.0.100-preview4-011223 [C:\Program Files\dotnet\sdk]
  3.1.103 [C:\Program Files\dotnet\sdk]
  3.1.200 [C:\Program Files\dotnet\sdk]
  3.1.300-preview-015048 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]
  3.1.402 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.0.0-preview4-19216-03 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.8.20414.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.0.0-preview4-27615-11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.0.0-preview4-27613-28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version
    Microsoft Visual Studio Enterprise 2019 Preview Version 16.8.0 Preview 2.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions