我在名为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中加载程序集
发布评论