In my programs I want to use libraries (dlls) located in a custom location. The solution I tried to implement is given here net-reference-dll-from-other-location. Unfortunately this works perfectly in one case but does not in another. What puzzles me is that where it does not work the AppDomain.CurrentDomain.AssemblyResolve event fires in Debug even though the references are set to "local copy" and so all the libraries are in the Debug folder. When I checked the event's ResolveEventArgs args.Name I noticed it is "SMLogger.resources" (should be UtilityTools, my program's name is SMLogger). In the case of the program where the method works the event does not fire in Debug, but fires only when I copy the exe to another location and the dll is not there (which is the behaviour I expected).
I figured there must be some difference in the projects' structures, but I compared csproj files and there is no difference in the way the references are attached
<ItemGroup>
<ProjectReference Include="..\..\UtilityTools\UtilityTools\UtilityTools.csproj">
<Project>{958e338d-e246-40fb-b1c4-8b362756bbb5}</Project>
<Name>UtilityTools</Name>
</ProjectReference>
Also they are both local copy, Alias global and Embedded interop types is false.
In both programs the bare-bones code which I implemented to look for libraries is identical and looks like this
static class Loader
{
public static string mainPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase.ToString();
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += FindAssem;
Program.Go(args);
}
static Assembly FindAssem(object sender, ResolveEventArgs args)
{
string simpleName = new AssemblyName(args.Name).Name;
string path = mainPath + @"..\lib\" + simpleName + ".dll";
MessageBox.Show("looking for library " + simpleName); //just for debug purposes
return Assembly.LoadFrom(path);
}
}
also in the Program class I changed
static void Main(string[] args) { ... body ...}
to
[MethodImpl(MethodImplOptions.NoInlining)]
public static void Go(string[] args) { ... body ...}
What could be the reason for the different behaviours?