在MEF出口组件自定义配置节 [英] Custom configuration sections in MEF exporting assemblies

查看:146
本文介绍了在MEF出口组件自定义配置节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含了进口数量的类未在编译时引用但通过目录编目被发现在运行时不同的组件类的组件。出口类要定义自定义配置节在进口组装的主机应用程序配置文件。但是,由于进口组装的主机应用程序不知道出口组件在编译时,它无法加载程序集使用自定义节处理程序实现在其中。

I have an assembly that contains classes that import a number of classes from different assemblies that are not referenced at compile time but are discovered at runtime via a directory catalog. The exporting classes want to define custom configuration sections for the config file in the importing assembly's host application. However, because the importing assembly's host application does not know the exporting assemblies at compile time, it cannot load the assembly to use the custom section handler implementations in them.

我已经找到了解决方法之一是把输出组件在同一文件夹作为进口组装的主机应用程序组件。但我想,以允许其他开发人员配置,他们希望保持自己的出口组件的任何文件夹。

One way I have found to work around this is to put the exporting assemblies in the same folder as the importing assembly's host application assembly. But I would like to allow other developers to configure any folder they want to hold their exporting assemblies.

有一件事情我可以做的是开发人员的配置文件夹中的内容复制到启动主机的文件夹中。但我宁愿避免这些额外的移动部件,code。如果我能保持。有没有解决这个更好的办法?有没有办法寻找定义自定义配置部分的组件时指向应用程序的附加目录?

One thing I can do is copy the contents of the developer's configured folder to the host's folder on startup. But I'd rather avoid those extra moving parts and code to maintain if I can. Is there a better way around this? Is there a way to point an application to additional directories when looking for assemblies that define custom config sections?

推荐答案

我遇到了同样的问题,在使用StructureMap动态发现组件。 ConfigurationManager中似乎寻找指定的大会对的ConfigurationSection只在滨文件夹和GAC。它似乎并不即使大会被加载到当前的AppDomain工作。

I ran into the same problem while using StructureMap to discover Assemblies dynamically. The ConfigurationManager seems to look for the specified Assembly for the ConfigurationSection only in the Bin-Folder and the GAC. It doesn't seem to work even if the Assembly was loaded into the current AppDomain.

但事实上,认为的Con​​figurationSection议会已经装载,可用于一个简单的解决方法:

But the fact, that the ConfigurationSection's Assembly is already loaded can be used for a simple workaround:

AppDomain.CurrentDomain.AssemblyResolve += (o, args) =>
        {
            var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            return loadedAssemblies.FirstOrDefault(asm => asm.FullName == args.Name);
        };

在AssemblyResolve-事件被触发每当CLR无法找到一定的大会。只要确保到GetSection()在第一次调用之前注册回调。

The AssemblyResolve-Event is fired whenever the CLR cannot find a certain Assembly. Just ensure to register the callback before the first call to GetSection().

我的作品。

这篇关于在MEF出口组件自定义配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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