Skip to content

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

@GavinTang-Dev

Description

@GavinTang-Dev

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<Microsoft.Extensions.Configuration.IConfigurationBuilder>)'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions