C#C
C#4y ago
2 replies
alkasel#159

Dynamically load .dll into .NET 6 Program

Hi,
I've a Blazor WebAssembly Hosted (.NET 6) web application. I've put some part of the application in separated .dll projects: the idea is that each separated .dll represents a feature a customer may or may not want to buy.

If a customer buys features A and B but not C, when building the solution for him/her, I'll include in the build library projects A and B but not C.

From the main application I plan not to directly reference A and B but to load them dynamically since at compile time I don't know which libraries (features) I'll have to include for a given deployment.

The dynamic .dll loading is pretty straighforward, I've taken it from here
https://docs.microsoft.com/en-us/dotnet/framework/app-domains/how-to-load-assemblies-into-an-application-domain

The problem is that if I reference project A from the main project and build the solution, also loading the assembly dynamically using the above-mentioned code works flawlessy; however, if I don't reference project A from the main project and build the solution, loading the assembly dynamically fails with an "file not found" exception.

I've made a research and it seems that, referencing project A from the main project and building the solution has the effect of adding a couple of rows to file ...\Server\obj\Debug\net6.0\Server.csproj.FileListAbsolute.txt, pointing to the location of the .dll files. However even if I manually modify that file I cannot get to have the dynamic loading working if I've not previously referenced project A from the main project.

Is there someone who can help me with this please? thanks
Learn how to load assemblies into an application domain in .NET. The recommended way is to use the static (or Shared) Load method in System.Reflection.Assembly.
How to: Load Assemblies into an Application Domain - .NET Framework
Was this page helpful?