[英].Net core 6: How to have output directory without assemblies
[英].net core does not pick assemblies from Base directory
我有要求要在配置文件中指定要加載類型的位置,在.net框架中,我們在配置文件中使用了如下所示的配置條目-
"PaasLogger": "HsopPaas, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
並使用如下方法-
var assembly = Assembly.Load(configuration["PaasLogger"]);
現在,如果HsopPaas.dll在基本目錄中,.net框架可以將其加載並加載到.net核心中,這將不起作用,並且我會收到FileNotFoundException,根據.net核心,這是設計使然
但是我們現在不知道該怎么辦,如何告訴.net core從Base Directory加載?
如果您有組裝的路徑,它將變得很容易:
使用這些using語句:
using System.Reflection;
using System.Runtime.Loader;
嘗試這個:
var myAssembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(pathTodll);
從根文件夾加載所有程序集:
獲取應用程序的基本目錄,並從此處加載所有.dll
文件:
在startup.cs
Configure
方法中:
string appPath = env.ContentRootPath; //Application Base Path
List<Assembly> allAssemblies = new List<Assembly>();
foreach (string dll in Directory.GetFiles(appPath , "*.dll"))
allAssemblies.Add(Assembly.LoadFile(dll));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.