GetAssemblies 从未在 IAassembliesResolver 中调用

GetAssemblies is never called in IAassembliesResolver

目前我尝试覆盖 ASP.Net WebApi 2 中解析的程序集。覆盖:

var resolver = new ExtendedAssemblyResolver(new[] { dynAsm });

// Add a list of dynamically loaded assemblies
GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), resolver);

解析器如下所示:

public class ExtendedAssemblyResolver : DefaultAssembliesResolver
{
    private string[] dynamicAssemblies;

    public ExtendedAssemblyResolver(string[] dynamicAssemblies)
    {
        System.Diagnostics.Debugger.Launch();
        this.dynamicAssemblies = dynamicAssemblies;
    }

    public ICollection<Assembly> GetAssemblies()
    {
        var baseAssemblies = base.GetAssemblies();
        var assemblies = new List<Assembly>(baseAssemblies);

        if (dynamicAssemblies != null)
        {
            foreach (var asm in dynamicAssemblies)
            {
                var controllersAssembly = Assembly.LoadFrom(asm);
                baseAssemblies.Add(controllersAssembly);
            }
        }

        return baseAssemblies;
    }
}

问题是,GetAssemblies 从未被调用过。

我也曾尝试更换 Startup class 中的解析器,但这也无济于事:

public void Configuration(IAppBuilder appBuilder)
{
    // Configure Web API for self-host. 
    HttpConfiguration config = new HttpConfiguration();
    // -- here
}

非常感谢!

您缺少覆盖

public class ExtendedAssemblyResolver : DefaultAssembliesResolver
{
    private string[] dynamicAssemblies;

    public ExtendedAssemblyResolver(string[] dynamicAssemblies)
    {
        System.Diagnostics.Debugger.Launch();
        this.dynamicAssemblies = dynamicAssemblies;
    }

    public override ICollection<Assembly> GetAssemblies()
    {
        var baseAssemblies = base.GetAssemblies();
        var assemblies = new List<Assembly>(baseAssemblies);

        if (dynamicAssemblies != null)
        {
            foreach (var asm in dynamicAssemblies)
            {
                var controllersAssembly = Assembly.LoadFrom(asm);
                baseAssemblies.Add(controllersAssembly);
            }
        }

        return baseAssemblies;
    }
}