小编Com*_*nDk的帖子

如何在dotnet核心中动态加载程序集

我正在构建一个Web应用程序,我想要单独关注,即在不同的项目中进行抽象和实现.

为了实现这一点,我尝试实现一个组合根概念,其中所有实现必须具有ICompositionRootComposer注册服务,类型等的实例.

public interface ICompositionRootComposer
{
    void Compose(ICompositionConfigurator configurator);
}
Run Code Online (Sandbox Code Playgroud)

在构建层次结构中直接引用的项目中,将ICompositionRootComposer调用实现,并在基础IoC容器中正确注册服务.

当我尝试在项目中注册服务时出现问题,我在其中设置了一个后期构建任务,将构建的dll复制到Web项目的调试文件夹:

cp -R $(TargetDir)"assembly and symbol name"* $(SolutionDir)src/"webproject path"/bin/Debug/netcoreapp1.1
Run Code Online (Sandbox Code Playgroud)

我正在加载程序集:(灵感:如何加载位于.net核心控制台应用程序中的文件夹中的程序集)

internal class AssemblyLoader : AssemblyLoadContext
{
    private string folderPath;

    internal AssemblyLoader(string folderPath)
    {
        this.folderPath = Path.GetDirectoryName(folderPath);
    }

    internal Assembly Load(string filePath)
    {
        FileInfo fileInfo = new FileInfo(filePath);
        AssemblyName assemblyName = new AssemblyName(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));

        return this.Load(assemblyName);
    }

    protected override Assembly Load(AssemblyName assemblyName)
    {
        var dependencyContext = DependencyContext.Default;
        var ressource = dependencyContext.CompileLibraries.FirstOrDefault(r => r.Name.Contains(assemblyName.Name));

        if(ressource …
Run Code Online (Sandbox Code Playgroud)

c# .net-core asp.net-core

9
推荐指数
2
解决办法
8968
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

c# ×1