You can use the System.Addin framework (sometimes called MAF ), which is a little difficult to configure correctly, but it is designed to provide isolation (protection against crashes). System.Addin based on removal. Using this environment, you can let plugins work with limited permissions, in the same process or in a different application domain, or even in another process.
If you need full protection against crashes, you may need to use the process separation option. Perhaps this is due to the price of performance.
You can use this code to load addin in another application domain:
AppDomain addInDomain = AppDomain.CreateDomain("addin domain"); // addInDomain.PermissionSet = ... AddInEnvironment env = new AddInEnvironment(addInDomain); // Activate the add-in IHostView addinInstance = addinToken.Activate<IHostView>(env); Console.WriteLine(addinInstance.DoSomething()); AppDomain.Unload(addInDomain);
If you want to load the add in another process, for complete isolation:
AddInProcess process = new AddInProcess(); process.Start(); // Activate the add-in IHostView addinInstance = addinToken.Activate<IHostView>(process, AddInSecurityLevel.Internet); try { // use a catch block, prevent exceptions from the addin crashing the main app Console.WriteLine(addinInstance.DoSomething()); } catch (Exception e) { Console.WriteLine(e); } process.Shutdown();
This blog gives a good description of customizing this.
You can combine System.Addin with MEF, these are free toolkits, see this article .
Please note that the System.Addin model can provide protection against crashes, you still have to deal with slowdowns or deadlocks in addin code. Asynchronous use will help here.
oษษฏวษน
source share