首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可能将依赖注入与Umbraco7 ContentService事件处理程序一起使用?

有没有可能将依赖注入与Umbraco7 ContentService事件处理程序一起使用?
EN

Stack Overflow用户
提问于 2014-06-05 05:44:26
回答 1查看 1K关注 0票数 3

我在一个MVC项目中使用Umbraco7.1.1,并且我已经将它配置为使用依赖注入(在我的例子中是Castle.Windsor)。我也在使用NServiceBus发送消息等,它工作得很好。

我现在正在尝试连接到ContentService发布的事件-尝试并发布一个NServiceBus事件,以提醒其他服务内容已更改。我想做的事情是这样的:

代码语言:javascript
复制
public class ContentPublishedEventHandler : ApplicationEventHandler
{
    public IBus Bus { get; set; }

    public ContentPublishedEventHandler()
    {
        ContentService.Published += ContentServiceOnPublished;
    }

    private void ContentServiceOnPublished(IPublishingStrategy sender, PublishEventArgs<IContent> publishEventArgs)
    {
        Bus.Publish<ContentUpdatedEvent>(e =>
        {
            e.UpdatedNodeIds = publishEventArgs.PublishedEntities.Select(c => c.Id);
        });
    }
}

但是在这种情况下,Bus是空的,因为我的依赖注入框架要么没有正确配置,要么(正如我怀疑的)从未被调用过。

如果我依赖于对总线的静态引用,我可以让它工作,但如果可以的话,我更愿意避免这种情况。我想要做的事情是可能的吗?Ie使用依赖注入与这些Umbraco事件?如果是这样,我需要什么配置来告诉Umbraco使用Castle.Windsor来创建我的事件处理程序?

EN

回答 1

Stack Overflow用户

发布于 2015-03-27 06:35:44

如果你还在寻找答案,最好是在ContentPublishedEventHandler构造函数中注入依赖项,这样代码看起来就像这样:

代码语言:javascript
复制
public class ContentPublishedEventHandler : ApplicationEventHandler
{
    public IBus Bus { get; set; }
    
    public ContentPublishedEventHandler(IBus bus)
    {
        Bus = bus;
    }

    protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
    {
        ContentService.Published += ContentServiceOnPublished;

        base.ApplicationStarting(umbracoApplication, applicationContext);
    }
        
    
    private void ContentServiceOnPublished(IPublishingStrategy sender, PublishEventArgs<IContent> publishEventArgs)
    {
        Bus.Publish<ContentUpdatedEvent>(e =>
        {
            e.UpdatedNodeIds = publishEventArgs.PublishedEntities.Select(c => c.Id);
        });
    }
}

如果您正在查找有关在Umbraco7中使用依赖注入的更多信息,请参阅https://web.archive.org/web/20160325201135/http://www.wearesicc.com/getting-started-with-umbraco-7-and-structuremap-v3/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24048127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档