I'm trying to make a simple plugin system in which a plugins are dynamically loaded from .dll files on application's startup and show up in the UI.
This Answer seems to be exactly what I'm looking for. It uses MEF to load the plugins. I tried to create a simple project and follow the instructions. My solution has the following structure:
MeftTest (contains main MefTest.exe, references only MefTest.SDK and not plugins)
MefTest.SDK (contains the IPlugin.cs and IPluginViewModel.cs and the Engine.cs which loads the plugins from the application's directory)
MefTest.Plugin1 (contains first plugin, references MefTest.SDK)
MefTest.Plugin2 (contains second plugin, references MefTest.SDK)
MefTest.SDK -> IPlugin.cs
public interface IPlugin
{
IPluginViewModel ViewModel { get; }
ResourceDictionary View { get; }
string Title { get; }
}
MefTest.SDK -> Engine.cs
public class Engine
{
[ImportMany]
private IEnumerable<IPlugin> plugins { get; set; }
public async Task<ObservableCollection<IPlugin>> GetPlugins()
{
try
{
var folder = AppDomain.CurrentDomain.BaseDirectory;
var catalog = new AggregateCatalog();
//catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
catalog.Catalogs.Add(new DirectoryCatalog(folder));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
var result = new ObservableCollection<IPlugin>();
foreach (var p in plugins)
{
result.Add(p);
}
return result;
}
catch (Exception ex)
{
//I get the exception here...
var t = ex;
throw;
}
}
}
MefTest.Plugin1 -> Plugin1.cs
[Export(typeof(IPlugin))]
public class Plugin1 : IPlugin
{
private MainViewModel viewModel { get; set; }
public IPluginViewModel ViewModel
{
get { return viewModel; }
}
public ResourceDictionary View
{
get { return viewDictionary; }
}
private ResourceDictionary viewDictionary = new ResourceDictionary();
public string Title
{
get { return "Plugin 1"; }
}
[ImportingConstructor]
public Plugin1()
{
//I get the error here. tried both of these, none of them work
viewDictionary.Source =
// new Uri("pack://application:,,,/MefTest.Plugin1;component/views/main.xaml", UriKind.Absolute);
new Uri("/MefTest.Plugin1;component/views/main.xaml",
UriKind.Relative);
}
public override string ToString()
{
return Title;
}
}
however, I get the error Could not load file or assembly 'MefTest.Plugin1.dll, Culture=neutral' or one of its dependencies. The system cannot find the file specified.
The Main.xaml file is in MefTest.Plugin1\Views\Main.xaml folder. Output type of the project is ClassLibrary and Build Action of the xaml file is Page.
PS: I tried to reference the plugin directly and add it without the MEF (Plugins.Add(new Plugin3.Plugin3());) and it still threw the same exception. So I don't think the problem is with the MEF part of the solution.
How can I fix this? Also, is there a better option to this approach?
i do it this way in an xbap application...to retreive distant xaml. Try to do the same with your local resourtce
private ResourceDictionary LoadDictionary(string source)
{
Stream streamInfo = null;
ResourceDictionary dictionary = null;
try
{
streamInfo = DistantManager.Instance.GetResource(source);
if (streamInfo != null)
{
Uri baseUri = DistantManager.Instance.GetUri(source);
dictionary = XamlReader.Load(streamInfo) as ResourceDictionary;
dictionary.Source = baseUri;
}
}
catch (Exception e)
{
BusinessLogger.Manage(e);
return null;
}
return dictionary;
}
something like
Uri baseUri = new Uri(Mysource);
dictionary = XamlReader.Load(XXXX) as ResourceDictionary;
dictionary.Source = baseUri;
but on the other hand I do not understand why you want a ResourceDictionary as your plugin view...? just create the plugin user control ??
Related
I am attempting to retrieve the typical AssemblyInfo attributes from an executable file, but not from the currently executing assembly. I wish to 'look into' a program file (.exe) elsewhere on the drive that I have written in C#.NET and check the AssemblyProduct string.
This is fairly easy and straightforward when you're looking for this information from the currently executing assembly. However, apparently not so much when you attempt to pull it from an unloaded assembly.
When I use the following code, it returns "Microsoft® .NET Framework" instead of the Product name that I put in my AssemblyInfo.cs file.
Note: I use the System.Reflection.AssemblyName object to pull the version info e.g:AssemblyName.GetAssemblyName(pathToAssembly) and this works correctly, but I'm unable to pull my assembly's attributes using that class or by any means I've tried thus far. Is there some other special class, or what am I missing or doing incorrectly here?
public static string GetAppProdIDFromPath(string pathToForeignAssembly)
{
var atts = GetForeignAssemblyAttributes(pathToForeignAssembly);
var id = string.Empty;
foreach (var att in atts)
{
if (att.GetType() == typeof(AssemblyProductAttribute))
{
id = ((AssemblyProductAttribute)att).Product;
}
}
return id;
}
private static object[] GetForeignAssemblyAttributes(string pathToAssembly)
{
if(File.Exists(pathToAssembly))
{
try
{
var assm = System.Reflection.Assembly.LoadFrom(pathToAssembly);
return assm.GetType().Assembly.GetCustomAttributes(false);
}
catch(Exception ex)
{
// logger etc
}
}
else
{
throw...
}
return null;
}
As Duncanp mentioned, there is a bug in my code. Posting it for clarity and for anyone down the road who looks for the same solution:
public static string GetAppProdIDFromPath(string pathToForeignAssembly)
{
var atts = GetForeignAssemblyAttributes(pathToForeignAssembly);
var id = string.Empty;
foreach (var att in atts)
{
if (att.GetType() == typeof(AssemblyProductAttribute))
{
id = ((AssemblyProductAttribute)att).Product;
}
}
return id;
}
private static object[] GetForeignAssemblyAttributes(string pathToAssembly)
{
if(File.Exists(pathToAssembly))
{
try
{
var assm = System.Reflection.Assembly.LoadFrom(pathToAssembly);
return assm.GetCustomAttributes(false); // fixed line
}
catch(Exception ex)
{
// logger etc
}
}
else
{
throw...
}
return null;
}
I am trying to log the connected scanner on a pc.
I am using NTwain.dll from https://bitbucket.org/soukoku/ntwain.
If I run my app on a server some dependency dlls from ntwain fail to load so I will load the dll at runtime and if it will fail I just want to return an empty list. There is no reference to NTwain in the project references anymore.
Problem:
If I have the NTwain.dll in the folder with the exe and I run it on a server the app crashes. It doesn't return an empty list. If I delete the dll and run the app the empty list gets returned.
Code:
public class Scanner : IDB
{
private enum DataGroups : uint
{
None = 0,
Control = 0x1,
Image = 0x2,
Audio = 0x4,
Mask = 0xffff,
}
public string Name { get; private set; }
public string ProductFamily { get; private set; }
public string Version { get; private set; }
public Scanner()
{
Name = String.Empty;
}
public static List<Scanner> getScanners()
{
List<Scanner> scanners = new List<Scanner>();
try
{
Assembly assembly = Assembly.LoadFrom(Environment.CurrentDirectory + "\\NTwain.dll");
Type tident = assembly.GetType("NTwain.Data.TWIdentity");
Type tsession = assembly.GetType("NTwain.TwainSession");
object appId = tident.GetMethod("CreateFromAssembly").Invoke(null, new object[] { DataGroups.Image, System.Reflection.Assembly.GetExecutingAssembly() });
object session = Activator.CreateInstance(tsession, appId);
tsession.GetMethod("Open", new Type[0]).Invoke(session, null);
object sources = session.GetType().GetMethod("GetSources").Invoke(session, null);
foreach (var item in (IEnumerable)sources)
{
Scanner scanner = new Scanner();
scanner.Name = (string)item.GetType().GetProperty("Name").GetValue(item, null);
scanner.ProductFamily = (string)item.GetType().GetProperty("ProductFamily").GetValue(item, null);
object version = item.GetType().GetProperty("Version").GetValue(item, null);
scanner.Version = (string)version.GetType().GetProperty("Info").GetValue(version, null);
scanners.Add(scanner);
}
return scanners;
}
catch (Exception e)
{
return new List<Scanner>();
}
}
}
I would guess to catch a DllNotFoundException works in your code:
If u delete the dll then
Assembly assembly = Assembly.LoadFrom(Environment.CurrentDirectory + "\\NTwain.dll");
throws an DllNotFoundException which is catched by your catch block which says to return an empty list (which works as u say).
If u don't delete the dll then the code passes above line successfully. In case by the following lines a different thread is started and an error occurs that is not catched within that thread, then your catch block will not catch that error (whatever it might be) and the application crashes.
I'm working on some usercontrol plugin system so it's possible to add a dll to a directory and the usercontrol is visible on specified pages.
When I load the control to the page nothing is shown!
Below is some code shown, the simpletestcontrol is compiled in a dll and read in the PluginFactory. (I removed or changed some code to try making is easier to read.)
In Default.aspx.cs the plugin is available and I see when I debug that the control is loaded.. but when I set it in the PluginGrid (a div) nothing is shown when I run the website... What is it that I am missing?
SimpleTestControl.ascx.cs:
public partial class SimpleTest : BasePluginControl
{
public SimpleTest()
: base(Type.Test)
{
}
}
SimpleTestControl.ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="simpletest.ascx.cs" Inherits="simpletest" %>
TESTESTETS
<asp:Button ID="Button1" runat="server" Text="testButton"/>
BasePluginControl.cs:
public abstract class BasePluginControl : UserControl
{
public string Name { get; set; }
public PluginType Type { get; set; }
}
Default.aspx.cs
public partial class Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
BasePluginControl plugin =
PluginFactory.GetPluginByType(PluginType.Test);
PluginGrid.Controls.Clear();
if (plugin != null)
{
PluginGrid.Controls.Add(plugin);
}
base.OnInit(e);
}
PluginFactory.cs:
public static BasePluginControl GetPluginByType(PluginType pluginType)
{
BasePluginControl plugin = plugins.FirstOrDefault(p => p.PluginType == pluginType);
if(plugin== null)
throw new DllNotFoundException("A plugin of the " + pluginType.ToString() + " is not found!");
return plugin;
}
public static ICollection<BasePluginControl> LoadPlugins()
{
string path = ConfigurationManager.AppSettings["PluginPath"];
plugins.Clear();
if (!Directory.Exists(path))
return null;
string[] dllFileNames = Directory.GetFiles(path, "*.dll");
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
foreach (string dllFile in dllFileNames)
{
AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
Assembly assembly = Assembly.Load(an);
assemblies.Add(assembly);
}
IEnumerable<Type> controlPlugins = GetPluginsOf<BasePluginControl>(assemblies);
foreach (Type controlPlugin in controlPlugins)
{
BasePluginControl plugin = (BasePluginControl)Activator.CreateInstance(controlPlugin);
if (plugin != null)
plugins.Add(plugin});
}
return plugins;
}
private static IEnumerable<Type> GetPluginsOf<T>(IEnumerable<Assembly> assemblies)
{
Type pluginType = typeof(T);
ICollection<Type> pluginTypes = new List<Type>();
foreach (Assembly assembly in assemblies)
{
if (assembly != null)
{
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.IsInterface || type.IsAbstract)
{
continue;
}
else
{
if( type.GetInterface(pluginType.FullName) != null ||
(type.BaseType != null && type.BaseType.FullName != null && type.BaseType.FullName.Equals(pluginType.FullName)))
{
pluginTypes.Add(type);
}
}
}
}
}
return pluginTypes;
}
UPDATE:
When I add the folliwing code to default.aspx.cs the strBuild variable is empty also...
string strBuild;
// a string writer to write on it
using (TextWriter stringWriter = new StringWriter())
{
// a html writer
using (HtmlTextWriter renderOnMe = new HtmlTextWriter(stringWriter))
{
// now render the control inside the htm writer
pluginModel.Control.RenderControl(renderOnMe);
// here is your control rendered output.
strBuild = stringWriter.ToString();
}
}
PluginGrid.InnerHtml = strBuild;
Two things,
You didn't add the plugin to the pages control's collection, so it won't be processd in the render loop or get tied into the page life cycle. You either have to add it to a control's collection, or you have to manually render it, by calling Render on the plugin (since it is an ASP.Net Control).
It could be the fact that you are loading the dll's in the same domain as the web application.
As such it cannot unload them. Once loaded, they are loaded. If they change you will have to reset the application pool to get the new version to load.
Optionally, you should create an AppDomain for your plugins. Then use a FileSystemWatcher to monitor the plugin directory.
Use the file system watcher to check when files are added, modied, or deleted from the plugin directory.
If the file that changed was a dll, do the following
On Delete: Unload all plugins from the dll from the new AppDomain
On Modified: Unload and Reload the plugins from the App Domain
On Add: Load the plugins from the new dll
I am working on a project where allowing 3rd-party plugins is required. I have worked with plugins before and I never had a problem.
I'm sure my problem is because WPF doesn't like me using Assembly.LoadFile(file) & Activator.CreateInstance(t)!
The error I encounter is:
The component 'Servus.Forms.MainWindow' does not have a resource identified by the URI '/Servus;component/forms/mainwindow.xaml'.
which shows in my MainForm constructor at:
InitializeComponent();
If I load the plugins after loading the MainForm it loads without issues, however when opening any other forms(there are many in my application) I experience the same issue as about but with the relevant error for that particular form.
I have also tried to load the plugins in there own AppDomain like this:
PluginDomain temp = new PluginDomain();
PluginBase tempPlug = temp.GetPlugin(file);
With the following classes:
public class PluginDomain
{
public AppDomain CurrentDomain { get; set; }
public ServusAssemblyLoader CurrentAssemblyLoader { get; set; }
private readonly Random _rand = new Random();
public PluginDomain()
{
}
public PluginBase GetPlugin(string assemblyName)
{
try
{
string appBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var ads = new AppDomainSetup { ApplicationBase = appBase, PrivateBinPath = appBase, ShadowCopyFiles = "true" };
CurrentDomain = AppDomain.CreateDomain("ServusDomain_Plugin_" + _rand.Next(0, 100000), null, ads);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
CurrentAssemblyLoader = (ServusAssemblyLoader)
CurrentDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(ServusAssemblyLoader).FullName);
AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
return CurrentAssemblyLoader.Load(assemblyName);
}
catch (Exception e)
{
CConsole.WriteLine("Error: " + e.Message);
}
finally
{
CurrentAssemblyLoader = null;
AppDomain.Unload(CurrentDomain);
}
return null;
}
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string[] parts = args.Name.Split(',');
string file = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + parts[0].Trim() + ".dll";
return Assembly.LoadFrom(file);
}
}
public class ServusAssemblyLoader : MarshalByRefObject, IAssemblyLoader
{
public PluginBase Load(string file)
{
Assembly asm = Assembly.LoadFrom(file);
foreach (Type t in asm.GetTypes())
{
if (t.IsSubclassOf(typeof(PluginBase)))
{
return (PluginBase)Activator.CreateInstance(t);
}
}
return null;
}
}
public interface IAssemblyLoader
{
PluginBase Load(string file);
}
This returns an TransparentProxy object like this:
{System.Runtime.Remoting.Proxies.__TransparentProxy}
However I am unsure how to use this as I was expecting it to return a PluginBase Object.
I have read that many people have also have this issue, they have answers that say to use a new AppDomain, but as you can see this doesn't help me right now.
I hope I have provided you enough information, can anyone help?
It turns out I had a few things wrong in my PluginDomain Class.
Fix #1:
Replace:
return (PluginBase)Activator.CreateInstance(t);
With:
(PluginBase)asm.CreateInstance(t.ToString());
Fix #2:
Remove:
AppDomain.Unload(CurrentDomain);
Fix #3: (Purely for debugging)
Replace:
return CurrentAssemblyLoader.Load(assemblyName);
With:
PluginBase obj = CurrentAssemblyLoader.Load(assemblyName);
return obj;
EDIT:
It should be noted that the new AppDomain wont be able to access objects in the old one; so my problem is only half fixed.
I've got a property defined on a class that has the importManyAttribute defined for it, the declaration is as follows:
public const string FontStyleProvidersPropertyName = "FontStyleProviders";
[ImportMany(typeof(IFontStyleProvider), RequiredCreationPolicy = CreationPolicy.Shared, AllowRecomposition=true)]
public List<IFontStyleProvider> FontStyleProviders { get; set; }
on first run I build my composition container as follows
private CompositionContainer BuildCompositionContainer()
{
//build our composable parts catalog
Assembly executingAssembly = Assembly.GetExecutingAssembly();
CompositionContainer applicationContainer;
string localPath = Path.GetDirectoryName(executingAssembly.Location);
try
{
aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(executingAssembly));
if (!Directory.Exists(Path.Combine(localPath, ApplicationExtensionsPath)))
{
Directory.CreateDirectory(Path.Combine(localPath, ApplicationExtensionsPath));
}
exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));
//create a composition container
return applicationContainer = new CompositionContainer(aggregateCatalog);
}
catch (Exception e)
{
Debug.Fail("Catalog Construction Failed", e.StackTrace);
throw;
}
}
At this point, everything works as expected, but I can't seem to trigger re-composition on "this" class instance. I have an import method as follows:
private void Import()
{
exportsCatalog.Refresh();
CompositionBatch batch = new CompositionBatch();
batch.AddPart(this);
applicationContainer.Compose(batch);
var copy = PropertyChanged;
if (copy != null)
{
copy(this, new PropertyChangedEventArgs(FontStyleProvidersPropertyName));
copy(this, new PropertyChangedEventArgs(MessageContainerViewModelsPropertyName));
}
}
It finds new types in the ApplicationExtensionPath folder used by the exportsCatalog just fine, but it never actually re-builds FontStyleProviders (or MessageContainerViewModels)
I've been through the documents a few times and I can't seem to figure out why.
The problem is you aren't actually adding the catalog which you call Refresh() on to the AggregateCatalog. Change this:
exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath)));
To this:
exportsCatalog = new DirectoryCatalog(Path.Combine(localPath, ApplicationExtensionsPath));
aggregateCatalog.Catalogs.Add(exportsCatalog);
Also, once your class has been composed once you shouldn't compose it again. Just calling exportsCatalog.Refresh() will be enough to cause recomposition.