我有一个叫做nuqkgs的文件夹,里面装着nugget包。在项目启动时,我想要将这些包(也就是dll文件)加载到项目中,以便在运行时使用。我使用以下代码来加载它们,在调试时可以看到信息和dll已被找到并打开,但当应该使用它们时,会出现dll无法找到的错误,并且我可以看到解决方案试图在bin文件夹中查找它们(它们位于解决方案/nuqkgs/中)。我不想在任何文件中注册这些包,我只想能够把nugget包放入nuqkgs文件夹中并自动注册它们。有什么想法或者有谁在core 2.1中做过这个吗?这是我的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);
}