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

编程入门 行业动态 更新时间:2024-10-27 17:24:49
本文介绍了在Startup.cs 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); }

推荐答案

如AssemblyLoadContext 的md"rel =" nofollow noreferrer>文档,Assembly.Load(byte[])在新的未命名的加载上下文中加载程序集,而AssemblyLoadContext(默认值除外)当前无法解析依赖关系.这可能就是为什么您的零件无法正常工作的原因.尝试使用AssemblyLoadContext.Default.LoadFromStream代替Assembly.Load(byte[]).

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中加载程序集

本文发布于:2023-11-14 13:20:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587320.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   程序   cs   Startup   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!