0

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?

  • 1
    "*.resources" sounds like it searches for localized resources. I would consider that normal expected behavior. And not finding them is also expected behavior. The framework will try to find localized satellite assemblies and will fallback to the "default" language if not found. Did you when you said "copy to another location" also used a different system with different OS language? – Ralf Sep 12 '22 at 09:05
  • Ralf, by "another location" I mean a folder structure for this application on the client's desktop, which obviously is not the same as my Visual Studio project's. However, this is beside the point. The problem is that the two applications behave differently run in Debug mode from VS. – Marcin Skowronek Sep 12 '22 at 10:47

0 Answers0