在 ASP.NET MVC 5 中,我使用该BuildManager.GetReferencedAssemblies()方法获取 bin 文件夹中的所有程序集并在依赖项启动之前加载它们,因此所有 dll 都可供扫描和注入。
ASP.NET Core 有替代方案吗?
我尝试了这段代码,但它开始确实给了我加载错误,例如文件未找到异常。
foreach (var compilationLibrary in deps.CompileLibraries)
{
foreach (var resolveReferencePath in compilationLibrary.ResolveReferencePaths())
{
Console.WriteLine($"\t\tReference path: {resolveReferencePath}");
dlls.Add(resolveReferencePath);
}
}
dlls = dlls.Distinct().ToList();
var infolder = dlls.Where(x => x.Contains(Directory.GetCurrentDirectory())).ToList();
foreach (var item in infolder)
{
try
{
Assembly.LoadFile(item);
}
catch (System.IO.FileLoadException loadEx)
{
} // The Assembly has already been loaded.
catch (BadImageFormatException imgEx)
{
} // If a BadImageFormatException exception is thrown, the file is …Run Code Online (Sandbox Code Playgroud) 我创建了一个 ASP.NET Core 2.2 应用程序,其中Startup.cs有:
services.AddEntityFrameworkSqlServer();
services.AddScoped<ILazyLoader, MyLazyLoader>();
services.AddDbContext<ModelContext>(builder =>
{
builder.UseLazyLoadingProxies();
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
var options = builder.Options;
services.AddSingleton(options);
});
Run Code Online (Sandbox Code Playgroud)
但是lazyloader没有改变,我需要改变什么才能做出这个改变?