Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to load assemblies with MAF

Tags:

c#

.net

maf

I have a program with a plugin based architecture using the Managed Add-In Framework (MAF). I am trying to load my Add-In assemblies in a way where they run in their own process and I can specify where they should look for other assemblies to load. Below are two different methods I have tried and why they don't work 100%:

AppDomain _domain;

// Create application domain setup information.
AppDomainSetup domaininfo = new AppDomainSetup();

// Configure
domaininfo.ApplicationName = PluginName;
domaininfo.ApplicationBase = MyPath;
domaininfo.PrivateBinPath = MyPath;
domaininfo.LoaderOptimization = LoaderOptimization.MultiDomain;
domaininfo.DisallowApplicationBaseProbing = false;
domaininfo.DisallowBindingRedirects = false;
domaininfo.DisallowCodeDownload = false;
domaininfo.DisallowPublisherPolicy = false;

System.Security.Policy.Evidence adevidence = AppDomain.CurrentDomain.Evidence;
// Create the new application domain using setup information.
_domain = AppDomain.CreateDomain(PluginName + "_Domain", adevidence, domaininfo);

_addin = _token.Activate<IOpenSourceAutomationAddInv2>(_domain);

This method allows me to tell each add-in to run in a new app domain and I can specify where to look for additional assemblies. This is important because each add-in is in its own subdirectory and needs to look in the same directory as the host to load other assemblies. The problem with this method is if an add-in has an unhandled exception it will cause the host to crash since it is running in the same process.

AddInProcess _process;
_process = new AddInProcess(Platform.AnyCpu);

_addin = _token.Activate<IOpenSourceAutomationAddInv2>(_process,AddInSecurityLevel.FullTrust);

This method loads each add-in into its own process so it an individual add-in crashes it doesn't affect the host. The problem with this is I haven't been able to figure out if it is possible to tell the add-ins where to look for additional assemblies. They will only look in their directory instead of the host's directory.

What is the best way to use MAF in order to accomplish what I am looking for? I need to be able to load my add-ins is such a way to separate them from the host because I am not writing the add-ins. I don't have control over their code so I need to be sure they won't crash the host. I also need to be to able to specify where the add-ins should load assemblies from since they will be in their own subdirectory and need to load an assembly from the host directory. I would also prefer to not use the GAC if possible.

like image 683
Brian Avatar asked Dec 26 '12 15:12

Brian


Video Answer


1 Answers

If addins are not trusted I think that the better solution is to load it using a separate process as in your second solution.

About the references probably it is correct that each addin look inside its private directory otherwise in case you have 2 addins compiled with different incompatible references you are in trouble.

If you really want to force a common directory I can think of 2 solutions:

  • using AddInProcess.exe.Config you can specify a probing directory as inhttp://msdn.microsoft.com/en-us/library/823z9h8w.aspx
  • before activating the addin copy all the host references to the addin directory
like image 104
Davide Icardi Avatar answered Oct 15 '22 14:10

Davide Icardi