动态加载ASP.NET核心程序集

时间:2017-06-14 12:59:26

标签: asp.net-core asp.net-core-mvc .net-core typeloadexception asp.net-core-testhost

我正在尝试动态加载包含ASP.NET Core Startup类的.NET Core DLL。然后我想实例化Startup类并将其交给TestHost API,这样我就可以在内存中启动该站点。我写了以下代码:

var directoryPath = @"C:\Dlls";
var assemblyFilePath = Path.Combine(directoryPath, "Foo.dll");
var assemblyName = AssemblyLoadContext.GetAssemblyName(assemblyFilePath);
var assembly = new AssemblyLoader(directoryPath).LoadFromAssemblyName(assemblyName);
var startupType = assembly.ExportedTypes
    .FirstOrDefault(x => string.Equals(x.Name, "Startup"));
var webHostBuilder = new WebHostBuilder()
    .UseStartup(startupType)
    .UseUrls(new string[] { "http://localhost" });
using (var testServer = new TestServer(webHostBuilder))
{
    var response = testServer.CreateClient().GetAsync("/");
}

public class AssemblyLoader : AssemblyLoadContext
{
    private readonly string directoryPath;

    public AssemblyLoader(string directoryPath) =>
        this.directoryPath = directoryPath;

    protected override Assembly Load(AssemblyName assemblyName)
    {
        var dependencyContext = DependencyContext.Default;
        var compilationLibraries = dependencyContext
            .CompileLibraries
            .Where(x => x.Name.Contains(assemblyName.Name))
            .ToList();
        if (compilationLibraries.Count > 0)
        {
            return Assembly.Load(new AssemblyName(compilationLibraries.First().Name));
        }
        else
        {
            var file = new FileInfo($"{this.directoryPath}{Path.DirectorySeparatorChar}{assemblyName.Name}.dll");
            if (File.Exists(file.FullName))
            {
                var asemblyLoader = new AssemblyLoader(file.DirectoryName);
                return asemblyLoader.LoadFromAssemblyPath(file.FullName);
            }
        }

        return Assembly.Load(assemblyName);
    }
}

但是,当TypeLoadException被调用时,这会引发assembly.ExportedTypes。完整的堆栈跟踪:

  

发生System.TypeLoadException HResult = 0x80131522
  Message =方法'ConfigureServices',类型为'Foo.Startup',来自程序集   'Foo,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'没有   有一个实现。 Source = StackTrace:at   System.Reflection.RuntimeAssembly.GetExportedTypes(RuntimeAssembly   assembly,ObjectHandleOnStack retTypes)at   System.Reflection.RuntimeAssembly.GetExportedTypes()

我认为加载ASP.NET Core和其他第三方DLL(如Newtonsoft.Json)时遇到问题。

1 个答案:

答案 0 :(得分:-1)

我对这个答案不满意我会发布它,它可以帮助某人。

我正在执行dotnet publish,然后尝试从发布的输出中运行我的应用。我设法通过使用以下命令发布一个自包含的应用程序来实现它:

dotnet publish --self-contained --framework netcoreapp1.1 --runtime win7-x64

自包含的应用程序包含运行应用程序所需的所有程序集,包括.NET Core运行时。我想避免这样做,因为它需要你知道运行时标识符(RID)以及你手头正在运行的平台。

鉴于我上面的代码缺少.NET Core运行时程序集的知识,我试图从.NET Core运行时文件夹加载程序集,但到目前为止这对我来说都失败了。