✅ Reference DLL by path in .NET Core?
I have a DLL I want to reference. It is installed by a third party application. I have the following in my .csproj:
I do not want to include the third party's DLL in my project output, so I have this turned off. Installation of the third party product is a prerequitite to this application functioning, and the path will always be the same so the DLL will always be available.
When doing this, .NET Core complains (at runtime) that
The system cannot find the file specified
and additionally lists the exact version it was looking for.
The third party makes minor patches to the DLL every few months, with no API breakage. We want to reference the DLL at whatever version it is currently at and just use it, but .NET Core seems to not even want to load a specific version let alone any version.7 Replies
What if my windows installation isn't on
C:\
?you can load assemblies from specific paths at runtime: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom?view=net-7.0
Assembly.LoadFrom Method (System.Reflection)
Loads an assembly.
keep in mind this can get tricky, because your code must not try to load any types from that assembly before it's actually loaded
i guess you could hook into the assembly resolver too
Resolve assembly loads - .NET
This article describes the .NET AppDomain.AssemblyResolve event. Use this event for applications that require control over assembly loading.
but yeah to account for different installation paths you'd have to make this configurable
or come up with a strategy to search for it
There is environment.specialfolder
That can point you to the program files folder
I'll take a look at both of those options, thank you both
This appears to be a proposal?
Ah okay. I have it in the Program.cs but I will see if that would be better long term, thank you
I am but the resolver event hook is done before any calls to that
Seems to work so far
Worked perfectly. We have two different versions loading happily now and executing as expected
Thanks all for your help, greatly appreciated