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
How to: Load Assemblies into an Application Domain - .NET Framework
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.
2 Replies
Susko3
Susko32y ago
1. Don't modify FileListAbsolute.txt, it's used as an in-between step in the compile process and modifying it after the compile will not affect the program. 2. If you're getting FileNotFound, maybe the A .dll is not getting copied to the output folder. - try specifying the full path, or manually copying a recent build of A
alkasel#159
alkasel#1592y ago
In the end I was able to make it work by using
Assembly.LoadFile()
Assembly.LoadFile()
Instead of
Assembly.Load()
Assembly.Load()
Following this post https://stackoverflow.com/questions/12520212/exception-from-hresult-0x80131047 Thank you for pointing me in the right direction!
Stack Overflow
Exception from HRESULT: 0x80131047
I am having this problem whenever I try to debug my project: It's in French, here is my translation: "Error while trying to run project: Failed Loading assembly "DBZ buu's Fury Text Editor" ...