我试图阻止我的应用程序锁定我的MEF插件目录中的DLL,以便我可以在运行时覆盖程序集(注意我实际上并没有尝试让MEF在运行时重新加载它们,在下一个应用程序启动很好,我只是不想要停止应用程序来复制)
我试图通过为我的mef加载的程序集创建一个阴影复制的应用程序域来执行此操作,如下所示:
[Serializable]
public class Composer:IComposer
{
private readonly string _pluginPath;
public Composer(IConfigurePluginDirectory pluginDirectoryConfig)
{
_pluginPath = pluginDirectoryConfig.Path;
var setup = new AppDomainSetup();
setup.ShadowCopyFiles = "true"; // really??? is bool not good enough for you?
var appDomain = AppDomain.CreateDomain(AppDomain.CurrentDomain.FriendlyName + "_PluginDomain", AppDomain.CurrentDomain.Evidence, setup);
appDomain.DoCallBack(new CrossAppDomainDelegate(DoWorkInShadowCopiedDomain));
}
private void DoWorkInShadowCopiedDomain()
{
// This work will happen in the shadow copied AppDomain.
var catalog = new AggregateCatalog();
var dc = new DirectoryCatalog(_pluginPath);
catalog.Catalogs.Add(dc);
Container = new CompositionContainer(catalog);
}
public CompositionContainer Container { …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用mef创建一个POC,我需要在一个准备好运行的项目中动态加载dll,为此我创建了一个控制台应用程序项目和一个类库
项目.
控制台应用程序项目的代码如下 -
namespace MefProjectExtension
{
class Program
{
DirectoryCatalog catalog = new DirectoryCatalog(@"D:\MefDll", "*.dll");
[Import("Method1", AllowDefault = true, AllowRecomposition = true)]
public Func<string> method1;
static void Main(string[] args)
{
AppDomainSetup asp = new AppDomainSetup();
asp.ShadowCopyFiles = "true";
AppDomain sp = AppDomain.CreateDomain("sp",null,asp);
string exeassembly = Assembly.GetEntryAssembly().ToString();
BaseClass p = (BaseClass)sp.CreateInstanceAndUnwrap(exeassembly, "MefProjectExtension.BaseClass");
p.run();
}
}
public class BaseClass : MarshalByRefObject
{
[Import("Method1",AllowDefault=true,AllowRecomposition=true)]
public Func<string> method1;
DirectoryCatalog catalog = new DirectoryCatalog(@"D:\MefDll", "*.dll");
public void run()
{
FileSystemWatcher sw = new FileSystemWatcher(@"D:\MefDll", …Run Code Online (Sandbox Code Playgroud)