18

I am attempting to load a dll into a console app and then unload it and delete the file completely. The problem I am having is that the act of loading the dll in its own AppDomain creates a reference in the Parent AppDomain thus not allowing me to destroy the dll file unless I totally shut down the program. Any thoughts on making this code work?

string fileLocation = @"C:\Collector.dll";
AppDomain domain = AppDomain.CreateDomain(fileLocation);
domain.Load(@"Services.Collector");
AppDomain.Unload(domain);

BTW I have also tried this code with no luck either

string fileLocation = @"C:\Collector.dll";
byte[] assemblyFileBuffer = File.ReadAllBytes(fileLocation);

AppDomainSetup domainSetup = new AppDomainSetup();
domainSetup.ApplicationBase = Environment.CurrentDirectory;
domainSetup.ShadowCopyFiles = "true";
domainSetup.CachePath = Environment.CurrentDirectory;
AppDomain tempAppDomain = AppDomain.CreateDomain("Services.Collector", AppDomain.CurrentDomain.Evidence, domainSetup);

//Load up the temp assembly and do stuff 
Assembly projectAssembly = tempAppDomain.Load(assemblyFileBuffer);

//Then I'm trying to clean up 
AppDomain.Unload(tempAppDomain);
tempAppDomain = null;
File.Delete(fileLocation); 
0

2 Answers 2

4

OK so I solved my own issue here. Apparently if you call AppDomain.Load it will register it with your parent AppDomain. So simply enough the answer is not to reference it at all. This is the link to a site that shows how to set this up properly.

https://bookie.io/bmark/readable/9503538d6bab80

3
  • added new link to answer Commented Nov 26, 2014 at 14:42
  • 5
    the new link is dead now too
    – Mr Bell
    Commented Sep 10, 2015 at 2:44
  • Links are all dead
    – Kingpin
    Commented Jul 6, 2017 at 12:45
4

This should be easy enough:

namespace Parent {
  public class Constants
  {
    // adjust
    public const string LIB_PATH = @"C:\Collector.dll";
  }

  public interface ILoader
  {
    string Execute();
  }

  public class Loader : MarshalByRefObject, ILoader
  {
    public string Execute()
    {
        var assembly = Assembly.LoadFile(Constants.LIB_PATH);
        return assembly.FullName;
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      var domain = AppDomain.CreateDomain("child");
      var loader = (ILoader)domain.CreateInstanceAndUnwrap(typeof(Loader).Assembly.FullName, typeof(Loader).FullName);
      Console.Out.WriteLine(loader.Execute());
      AppDomain.Unload(domain);
      File.Delete(Constants.LIB_PATH);
    }
  }
}
3
  • Does not work. I get an unauthorized access exception when i try to delete the file. Did you test your code? Commented Oct 10, 2017 at 7:42
  • @WolfgangRoth - it does work for me. if you're having an specific issue I suggest you ask a new question. Commented Oct 10, 2017 at 23:24
  • I solved my problem: in fact the visual studio debugger connects itself to the assembly while debugging, and it doesnt disconnect any more. But when i run without debugger or when i first load the assembly file into a byte array, everything works fine... Commented Oct 11, 2017 at 13:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.