Skip to content

AssemblyLoadContext load multiple assemblies with same name #78649

Open
@linkem

Description

@linkem

Description

Application loaded by AssemblyLoadContext (implementation from documentation) loads multiple .dll files with same AssemblyName which leads to exceptions like System.MissingMethodException Method not found: ...

Reproduction Steps

Small repo with reproduction code here

Repro with exception

  • Build PluginApp1 application
  • Start PluginHost
  • Exception is thrown:
    System.MissingMethodException
        HResult=0x80131513
        Message=Method not found: 'System.Threading.Tasks.Task`1<!!0> System.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync(System.Net.Http.HttpContent, System.Text.Json.JsonSerializerOptions, System.Threading.CancellationToken)'.
        Source=PluginApp1
        StackTrace:
            at PluginApp1.PluginApp1.<Execute>d__0.MoveNext() in C:\projects\GitHub\Github.AssemblyLoadContextError\PluginApp1\PluginApp1.cs:line 32
            at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine) in /_/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncMethodBuilderCore.cs:line 38
            at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) in /_/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncTaskMethodBuilder.cs:line 33
            at PluginApp1.PluginApp1.Execute(CancellationToken cancellationToken) in C:\projects\GitHub\Github.AssemblyLoadContextError\PluginApp1\PluginApp1.cs:line 9
            at Program.<<Main>$>d__0.MoveNext() in C:\projects\GitHub\Github.AssemblyLoadContextError\HostApp\Program.cs:line 12
            at Program.<Main>(String[] args)
    

Repro with listed duplicated assemblies

  • Comment out PluginApp1.PluginApp1.cs line 30
  • Build PluginApp1 application
  • Start PluginHost
  • Application should print output similar to this one:
    For Assembly 'System.Text.Json' found multiple instances:
    
    System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51; Location: <PROJECT_PATH>\PluginApp1\bin\Debug\net6.0\System.Text.Json.dll
    
    System.Text.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51; Location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\6.0.11\System.Text.Json.dll
    
  • Output indicates that there were loaded multiple assemblies with same name 'System.Text.Json'

Expected behavior

Plugin application should be started without duplicated assemblies, it should use assembly that is in local folder or in shared\Microsoft.NETCore.App but not both.

Actual behavior

In some cases duplicated assemblies are loaded.

Regression?

No response

Known Workarounds

For case presented in repro, in application PluginApp1 package reference of System.Text.Json can be changed from 6.0.7 to 6.0.0, but this is not applicable in more complex scenarios

Configuration

.NET SDK:
Version:   7.0.100
Commit:    e12b7af219

Runtime Environment:
OS Name:     Windows
OS Version:  10.0.19042
OS Platform: Windows
RID:         win10-x64
Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
Version:      7.0.0
Architecture: x64
Commit:       d099f075e4

But it also happens on dotnet 6 runtime

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions