简体   繁体   English

在Startup.cs .net core 2.1中加载程序集

[英]Load assembly in Startup.cs .net core 2.1

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. 我有一个名为nuqkgs的文件夹中的块包,在项目启动时我想将这些包(有dll)加载到项目中以便在运行时使用。

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/) 我使用以下代码加载它们,当我调试时,我可以看到信息,并且找到并打开了dll,但是当它们应该被使用时,我得到了无法找到dll的错误,我可以看到解决方案尝试在bin文件夹中查找它们(它们位于solution / 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 我不想在任何文件中注册包我只是希望能够将一个块包放入nuqkgs文件夹并自动注册

Any ideas or anyone that has done this in core 2.1? 在核心2.1中有任何想法或任何人这样做过吗?

This i my startup.cs: 这是我的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. AssemblyLoadContext文档中所述, Assembly.Load(byte[])将程序集加载到新的未命名加载上下文中,而AssemblyLoadContext (默认值除外)当前无法解析依赖项。 That's probably why your parts don't work. 这可能是你的部件不起作用的原因。 Try using AssemblyLoadContext.Default.LoadFromStream instead of Assembly.Load(byte[]) . 尝试使用AssemblyLoadContext.Default.LoadFromStream而不是Assembly.Load(byte[])

I got it working, the way i use it now: 我得到它的工作,我现在使用它的方式:

  1. Open the nuget foreach DLL and I write the dll to a file on the disk. 打开nuget foreach DLL,然后将dll写入磁盘上的文件。
  2. Open the file in a memory stream and read it into memory and from there it can be used in my application. 在内存流中打开文件并将其读入内存,然后可以在我的应用程序中使用它。

Alternatively to step 2 you could use : 作为第2步的替代,您可以使用:

AssemblyLoadContext.Default.LoadFromAssemblyPath(createPathSource);

that way you refer to the dll file when using it in your solution 这样你在解决方案中使用它时就会引用dll文件

    public void ConfigureServices(IServiceCollection services)
    {
        IList<Assembly> components = new List<Assembly>();

        foreach (string file in Directory.EnumerateFiles(@"C:\Users\userName\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"))
                    {
                        string createPathSource = @"C:\Users\userName\source\repos\core2.1test\core2.1test\nuqkgs\"+ entry.Name;

                        using (BinaryReader reader = new BinaryReader(entry.Open()))
                        {
                            // Step 1
                            using (FileStream fsNew = new FileStream(createPathSource, FileMode.Create, FileAccess.Write))
                            {
                                fsNew.Write(reader.ReadBytes((int)entry.Length), 0, (int)entry.Length);
                            }

                            // Step 2
                            using (FileStream readStream = File.Open(createPathSource, FileMode.Open))
                            {
                                var assempbly = AssemblyLoadContext.Default.LoadFromStream(readStream);
                                components.Add(assempbly);
                            }
                        }
                    }
                }
            }
        }
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddMvc()
          .ConfigureApplicationPartManager(apm =>
          {
              foreach (var c in components)
              {
                  var part = new AssemblyPart(c);
                  apm.ApplicationParts.Add(part);
              }
          });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM