dotnet / dotnet/samples

System.MissingMethodException: Method not found: 'Microsoft.Extensions.Hosting.IHostBuilder Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.ConfigureAppConfiguration

Open
#7,071 161 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
3.7k
Forks
5.1k
Avg merge
9h 11m
Merged PRs (30d)
1

Description

I have two webapi programs (Main and Plugin) both developed using net9. To facilitate startup, I use the Main program and start the Plugin program through reflection. However, this error keeps occurring during startup. For the dependent programs, the versions of my two programs are the same.

```
// This is a collectible (unloadable) AssemblyLoadContext that loads the dependencies
// of the plugin from the plugin's binary directory.
public class PluginAssemblyLoadContext: AssemblyLoadContext
{
// Resolver of the locations of the assemblies that are dependencies of the
// main plugin assembly.
public AssemblyDependencyResolver _resolver;
private readonly string _mainAssemblyPath;

public PluginAssemblyLoadContext(string pluginPath) : base(isCollectible: false)
{
_resolver = new AssemblyDependencyResolver(pluginPath);
}

// The Load method override causes all the dependencies present in the plugin's binary directory to get loaded
// into the HostAssemblyLoadContext together with the plugin assembly itself.
// NOTE: The Interface assembly must not be present in the plugin's binary directory, otherwise we would
// end up with the assembly being loaded twice. Once in the default context and once in the HostAssemblyLoadContext.
// The types present on the host and plugin side would then not match even though they would have the same names.
protected override Assembly Load(AssemblyName assemblyName)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
if (assemblyPath != null)
{
var assembly = LoadFromAssemblyPath(assemblyPath);
return assembly;
}
return null;
}

protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
{
string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
if (libraryPath != null)
{
return LoadUnmanagedDllFromPath(libraryPath);
}
return IntPtr.Zero;
}
}
```

```
var webApiAlc = new PluginAssemblyLoadContext(file);
Assembly assembly = webApiAlc.LoadFromAssemblyPath(file);
MethodInfo entryPoint = assembly.EntryPoint;

string[] methodParams = new string[] { path };
var result = entryPoint.Invoke(null, new object[] { methodParams });
```
**Error message**
```
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.MissingMethodException: Method not found: 'Microsoft.Extensions.Hosting.IHostBuilder
Microsoft.Extensions.Hosting.HostingHostBuilderExtensions.ConfigureAppConfiguration(Microsoft.Extensions.Hosting.IHostBuilder, System.Action`1)'.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.