MEF和ShadowCopying的DLL,这样我可以在运行时覆盖它们 [英] MEF and ShadowCopying DLLs so that I can overwrite them at runtime

查看:267
本文介绍了MEF和ShadowCopying的DLL,这样我可以在运行时覆盖它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图阻止我的MEF插件目录中我的应用程序的DLL锁定,这样我可以覆盖在运行时组件(注意,我实际上并不想有MEF重新加载它们的飞行,在下次启动的应用程序是罚款,我只是不希望有停止的应用程序进行复制)

I am trying to stop my application locking DLLs in my MEF plugin directory so that I can overwrite the assemblies at runtime (note I'm not actually trying to have MEF reload them on the fly, at the next app start is fine, i just dont want to have to stop the app to do the copy)

我想通过创建一个影子做这个复制应用程序域为我的MEF加载的程序集作为如下:

I am trying to do this by creating a shadow copied app domain for my mef loaded assemblies as below:

[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 { get; private set; }
    }



,然后通过CompositionContainer中访问该类我MEF组件目录。然而该组合物的容器,似乎只有在影拷贝域内初始化(这是有意义的),这意味着其在我的应用领域空。我只是想知道如果theres做到这一点还是某种方式跨域查询,以获得更好的方法我MEF组件

and then access my MEF component catalog via the CompositionContainer on this class. However the composition container seems to only be initialised inside the shadowcopy domain (which makes sense) and this means that its null in my application domain. I was just wondering if theres a better way to do this or some way to cross domain query to get my MEF components

推荐答案

如果你不想遵循丹科比和zync的解决方案,你可以创建一个shell应用程序,只需在一个新的的AppDomain

If you don't want to follow the solution from Dan Bryant and zync, you could create a shell application that simply executes your application in a new AppDomain.

这是做法是:


  1. 创建,这将是外壳应用程序的新应用程序项目

  2. 在外壳应用程序,创建的AppDomain ,使shadowcopying,如果你愿意的话,指定的影子拷贝将被启用的目录。

  3. 使用 AppDomain.ExecuteAssembly 打电话给你的当前应用程序

  1. Create a new application project which will be the shell application.
  2. In the shell application, create the AppDomain, enable shadowcopying and if you wish, specify the directory where shadow copying will be enabled.
  3. Use AppDomain.ExecuteAssembly to call your current application.

如果不是一个应用程序,你有一个类库,你可以尝试以下方法:

If instead of an application you have a class library you can try the following:


  1. 创建一个新的类库项目

  2. 下面的接口添加到新的类库项目。

  1. Create a new class library project.
  2. Add the following interface to the new class library project:

public接口IRemoteLoader结果
{结果
无效负载();结果
无效卸载(); < BR>
}

public interface IRemoteLoader
{
void Load();
void Unload();
}

添加此接口的实现您的类库,需要在新的AppDomain中执行。在加载,卸载方法,你应该添加代码分别执行初始化和清理。使类从 MarchsalByRefObject 派生。这是需要.NET远程对双方的AppDomain创建代理对象

Add an implementation of this interface to your class library that needs to execute in a new AppDomain. In the Load, Unload methods you should add code to perform initialization and cleanup respectively. Make the class derive from MarchsalByRefObject. This is needed for .NET Remoting to create proxy objects on both AppDomains.

在创建新的AppDomain之后,使用的 CreateInstanceAndUnwrap 从第3步打造装载机类的一个实例。

After you create the new AppDomain, use CreateInstanceAndUnwrap to create an instance of the loader class from step 3.

使用的加载,卸载步骤4中创建的对象。

Use the Load, Unload on the object created from step 4.

这将是不够的,如果你不这样做细粒度控制和简单的启动/停止就够了。

This will be enough if you do not fine-grained control and simply starting/stopping is enough.

这篇关于MEF和ShadowCopying的DLL,这样我可以在运行时覆盖它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆