如何告诉MEF重新创建对象?

发布于 2024-11-29 22:48:23 字数 1105 浏览 0 评论 0原文

我正在使用 mef 创建 WCF Web 服务。服务是这样的:

[Export]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MobileService
    {
        [Import] 
        public IEmailService EmailService { get; set; }

        [Import] 
        public ILoggerService LoggerService { get; set; }

        [Import] 
        public IContextManager ContextManager { get; set; }

检索服务实例的代码是这样的:

// Get Service instace via MEF        
    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        var lazyInstance = Container.GetExports(ServiceType, null, null).FirstOrDefault();
        var instance = lazyInstance.Value;

        return instance;
    } 

MEF 创建 EmailService、LoggerService,如果没问题的话,它们会在服务存在时幸福地生活。

现在,ContextManager 是不同的动物。在 GetInstance 中 - 我真的很喜欢“杀死”它并重新创建。 ContextManager 在构建过程中研究 URL 和标头并填充“上下文”。使用像我这样的代码 - 它是第一次创建并且永远不会被破坏。如何改变这种行为?

谢谢!

I'm using mef to create WCF web services. This is how service looks:

[Export]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class MobileService
    {
        [Import] 
        public IEmailService EmailService { get; set; }

        [Import] 
        public ILoggerService LoggerService { get; set; }

        [Import] 
        public IContextManager ContextManager { get; set; }

This is how code to retreive service instance looks:

// Get Service instace via MEF        
    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        var lazyInstance = Container.GetExports(ServiceType, null, null).FirstOrDefault();
        var instance = lazyInstance.Value;

        return instance;
    } 

MEF creates EmailService, LoggerService and this if fine, they live happily while service lives.

Now, ContextManager is different animal. In GetInstance - I really like to "kill" it and re-create. ContextManager studies URL and headers during construction and populates "context". With code like I have - it is created first time and never get's destroyed. How to change this behavior?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-06 22:48:23

在导出 IContextManager 的实现时,您需要使用非共享部件创建策略来标记导出。例如:

[Export(typeof(IContextManager)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ContextManager : IContextManager
{
   ...
}

这将通知 MEF 它应该在每次满足导入时创建一个新的导出实例。默认情况下,MEF 使用 CreationPolicy.Shared 它将仅创建一个导出值(单例),这可能是您在电子邮件和日志记录实现中想要的。

On your export of the implementation of IContextManager, you need to mark the export with the non-shared part creation policy. For example:

[Export(typeof(IContextManager)]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class ContextManager : IContextManager
{
   ...
}

This will inform MEF that it should create a new instance of the export every time it satisfies an import. By default MEF uses CreationPolicy.Shared which will create just a single exported value (a singleton), which is probably what you want for the Email and Logging implementations.

向地狱狂奔 2024-12-06 22:48:23

您可以通过在导出类的 PartCreationPolicyAttribute 或 ImportAttribute 的RequiredCreationPolicy 属性上设置该部件的创建为“NonShared”。

每次满足导入时,这都会通过导出创建该类的新实例。如果这不完全是您想要的,您可能需要查看 ExportFactory 或作用域容器。但是,如果您使用的是 .NET 4 版本的 MEF,则不支持 ExportFactory,并且您必须为范围界定做更多工作。您可以在 mef.codeplex.com 上预览 MEF 的下一版本。

You can make the part's creation be "NonShared" by setting it on a PartCreationPolicyAttribute on the exported class, or the RequiredCreationPolicy property of an ImportAttribute.

This will create a new instance of the class with the export every time an import is satisfied. If this isn't exactly what you want, you may want to look at ExportFactory or scoped containers. However if you are using the .NET 4 version of MEF, ExportFactory isn't supported and you have to do a lot more work for scoping. You can get a preview of the next version of MEF at mef.codeplex.com.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文