C
C#14mo ago
Pokey

✅ 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:
<Project>
...
<ItemGroup>
<Reference Include="MyDll">
<HintPath>C:\Program Files\Vendor\Product\MyDll.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
...
</Project>
<Project>
...
<ItemGroup>
<Reference Include="MyDll">
<HintPath>C:\Program Files\Vendor\Product\MyDll.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
...
</Project>
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
Pobiega
Pobiega14mo ago
What if my windows installation isn't on C:\?
Jimmacle
Jimmacle14mo ago
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
Jimmacle
Jimmacle14mo ago
Resolve assembly loads - .NET
This article describes the .NET AppDomain.AssemblyResolve event. Use this event for applications that require control over assembly loading.
Jimmacle
Jimmacle14mo ago
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
Pobiega
Pobiega14mo ago
There is environment.specialfolder That can point you to the program files folder
Pokey
PokeyOP14mo ago
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
Want results from more Discord servers?
Add your server