在Startup.cs .net core 2.1中加载程序集 [英] Load assembly in Startup.cs .net core 2.1

查看:95
本文介绍了在Startup.cs .net core 2.1中加载程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为nuqkgs的文件夹中有块状软件包,在项目启动时,我想将这些软件包(存在dll)加载到项目中以在运行时使用.

i have nugget packages in a folder called nuqkgs and on project start up i want to load these packages(there dll's) into the project to use at run time.

我使用以下代码加载它们,并且当我调试时我可以看到该信息,并且找到并打开了dll,但是当应使用它们时,我收到了找不到该dll的错误,并且我可以看到解决方案尝试在bin文件夹中查找它们(它们位于solution/nuqkgs/)

i use the following code to load them, and when i debug i can see the info and that the dll's are found and opened, however when they should be used i get the error that the dll can not be found, and i can see that the solution try's to look for them in the bin folder(they are located solution/nuqkgs/)

我不想将软件包注册到任何文件中,我只是想将一个掘金软件包放到nuqkgs文件夹中,并且它会自动注册

i do NOT want to register the packages in any file i simply want to be able to drop a nugget package into the nuqkgs folder and it gets registered automaticaly

在2.1核心中有任何想法或任何做到这一点的人吗?

Any ideas or anyone that has done this in core 2.1?

这是我的startup.cs:

This i my startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        IList<Assembly> components = new List<Assembly>();
        foreach (string file in Directory.EnumerateFiles(@"C:\Users\myName\source\repos\core2.1test\core2.1test\nuqkgs\", "*.nupkg", SearchOption.AllDirectories))
        {
            using (ZipArchive nuget = ZipFile.Open(file, ZipArchiveMode.Read))
            {
                foreach (ZipArchiveEntry entry in nuget.Entries)
                {
                    if (entry.Name.Contains(".dll"))
                    {
                        using (BinaryReader reader = new BinaryReader(entry.Open()))
                        {
                            components.Add(Assembly.Load(reader.ReadBytes((int)entry.Length)));
                        }
                    }
                }
            }
        }

        services.AddMvc()
          .ConfigureApplicationPartManager(apm =>
          {
              foreach (var c in components)
              {
                  var part = new AssemblyPart(c);
                  var des = part.Assembly.Location.ToString();
                  apm.ApplicationParts.Add(part);
              }
          }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
 }

推荐答案

As described in the docs for AssemblyLoadContext, Assembly.Load(byte[]) loads the assembly in a new unnamed load context, and AssemblyLoadContext (except the default one) currently cannot resolve dependencies. That's probably why your parts don't work. Try using AssemblyLoadContext.Default.LoadFromStream instead of Assembly.Load(byte[]).

这篇关于在Startup.cs .net core 2.1中加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆