4

Using Prism with WPF, I want to allow users to select from a repository which modules they'd like to use. Each module is essentially an add-on, and selecting a module to use would just move it into their 'Modules' folder of DLL to load.

But, in trying to move DLLs around when the application is running, an error is thrown because the DLLs are in use at that moment. How can you get around this and allow users to Add/Remove modules at will?

TrialAndError
  • 1,784
  • 3
  • 20
  • 41
  • 1
    Are you using Unity or MEF. You need to use MEF to allow modules to be dynamically loaded. http://danielvg.dk/post/2010/02/13/Unity-vs-MEF-vs-PRISM.aspx .... http://forums.silverlight.net/t/227834.aspx/1 – Colin Smith Oct 17 '12 at 12:22
  • I am using Unity and I am loading dynamically from a folder successfully at this time. Many people say only MEF can do this, which is incorrect, at least as far as discovering modules. I guess more specifically, I am looking to see if I can unload a module at runtime (so the DLL can be updated/removed). Is this possible with MEF instead of Unity? – TrialAndError Oct 18 '12 at 15:12
  • 1
    Once an assembly is loaded into an AppDomain, it does not get unloaded until the AppDomain is torn down....I guess that is your problem. There's some techniques to get around that if you look on the net...e.g. creating an additional AppDomain which you can then shutdown...but then you have to marshall calls between AppDomains using MarshalByRef. http://stackoverflow.com/questions/6578170/using-appdomain-in-c-sharp-to-dynamically-load-and-unload-dll – Colin Smith Oct 18 '12 at 20:15
  • That makes sense. Thank you for your input. I think I figured out a way around it, at least for allowing users to 'add/remove' modules. I am just putting in a 'Configure Modules' option on the login screen that launches before the bootstrapper. This will the copy/delete modules from the folder as needed before they are loaded. Also, do you want to put that in a response so I can mark it as the answer? – TrialAndError Oct 24 '12 at 21:33

1 Answers1

6

Once an assembly is loaded into an AppDomain, it does not (cannot) get unloaded until the AppDomain is torn down....I guess that is your problem.

There's some techniques to get around that if you look on the net.....

Create An Additional AppDomain

Create an additional AppDomain which you can then load your assembly into....when you are finished you just call Unload to shutdown the AppDomain and this will release the assembly.

However the types you want to be accessible from the other AppDomains have to derive from MarshalByRefObject so that your object is remoteable....and calls from other AppDomains can be marshalled across.

Load Assembly into a MemoryStream

A very interesting technique here....it loads the assembly into a MemoryStream first, then it gets .NET to load the Assembly from the MemoryStream...that means the "file" on disk, isn't locked.

Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47