有关在WCF服务中使用MEF的问题 [英] questions about using MEF in a WCF service

查看:87
本文介绍了有关在WCF服务中使用MEF的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始玩MEF,并且有几个问题.

I'm just starting to play with MEF and have a couple questions.

1)我编写了一个WCF服务,该服务接收一些xml并将xml传递给解析器.解析器是使用MEF组成的. (xml中的元数据可让我确定要使用的解析器).通过将dll放在目录中,我可以添加一个新的解析器并支持新的XML.那部分全部起作用.但是,WCF服务可以被实例化多次,我希望我的解析器目录是静态的,也就是说,如果我的服务的多个实例被启动,并且它们获得相同的XML,我只需要运行一个解析器实例,它们就可以运行了.被编写为线程安全的.我似乎无法配置MEF来做到这一点.有人知道吗?

1) I wrote a WCF service that takes in some xml and passes the xml off to a parser. The parsers are composed using MEF. (metadata in the xml lets me determine which parser to use). I can add a new parser, and support new XML, by just dropping the dll in a directory. That part all works. But, WCF services can be instantiated multiple times, I want my parser catalog to be static, that is, if multiple instances of my service are spun up, and they get the same XML, I only need one instance of the parser running, they are written to be thread safe. I can't seem to configure MEF to do this. Anyone know how?

2)我可以将新的解析器放入目录中,并且目录刷新会自动发现它,效果很好.但是,如果我尝试将修改后的dll放入目录中,并且该解析器已在服务中激活,则会收到错误消息,指出文件正在使用中.有没有办法覆盖这个?

2) I can drop in a new parser into the directory and a catalog refresh will automatically discover it, that works great. But if I try to drop a modified dll into the directory, and that parser has been activated in the service, I get an error saying the file is in use. Is there a way to override this?

推荐答案

1)听起来您应该将您的MEF容器和目录设置为静态,以便它们仅创建一次.通过将构造函数与isThreadSafe参数一起使用并将其设置为true,请确保指定CompositionContainer应该是线程安全的.

1) It sounds like you should make your MEF container and catalogs static so they only get created once. Make sure you specify that the CompositionContainer should be thread safe by using the constructor with the isThreadSafe parameter and setting it to true.

2)您可以启用卷影复制,这将防止在加载DLL时锁定文件.但是,您无法从.NET中的AppDomain卸载DLL,此外,重新组合可在多个线程上使用的CompositionContainer是不安全的.换句话说,使用isThreadSafe参数只能使容器具有线程安全性,以读取"/拉出容器中的导出内容,而不能通过组合/重组对其进行修改.

2) You can enable shadow copying which will prevent the file from being locked when the DLL is loaded. However, you can't unload DLLs from an AppDomain in .NET, and furthermore it is not safe to recompose a CompositionContainer that can be used on multiple threads. In other words, using the isThreadSafe parameter only makes the container thread-safe for "reading"/pulling exports from the container, not modifying it via composition/recomposition.

因此,如果您想添加新的解析器,则最好重新启动服务.

So if you want to add a new parser it's probably best to restart the service.

这篇关于有关在WCF服务中使用MEF的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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