是否可以在Umbraco 7 ContentService事件处理程序中使用依赖项注入? [英] Is it possible to use dependency injection with Umbraco 7 ContentService event handlers?

查看:95
本文介绍了是否可以在Umbraco 7 ContentService事件处理程序中使用依赖项注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Umbraco 7.1.1与MVC项目一起使用,并且已将其配置为使用依赖项注入(在我的情况下为Castle.Windsor)。我也在使用NServiceBus发送消息等,并且运行良好。

I am using Umbraco 7.1.1 with an MVC project, and I've configured it to use dependency injection (Castle.Windsor in my case). I'm also using NServiceBus to send messages etc and it's working pretty well.

我现在正尝试加入ContentService发布事件-尝试发布NServiceBus事件提醒其他服务内容已更改。我想做的是这样的:

I am now attempting to hook into the ContentService Published event - to try and publish an NServiceBus event to alert other services that the content has changed. What I would like to do is something like this:

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 为空,因为我的依赖项注入框架未正确配置,或者(据我怀疑)从未调用。

But in this case, Bus is null as my dependency injection framework is either not configured properly, or (as I suspect), never invoked.

我可以得到它如果我依靠对总线的静态引用可以正常工作,但我希望避免这种情况。我想做的事可能吗?即对这些Umbraco事件使用依赖注入?如果是这样,我需要告诉Umbraco使用Castle.Windsor创建我的事件处理程序的什么配置?

I can get it to work if I rely on a static reference to the bus, but I'd prefer to avoid that if I can. Is what I'm trying to do possible? Ie use dependency injection with these Umbraco events? If so, what configuration do I need to tell Umbraco to use Castle.Windsor to create my event handler?

推荐答案

仍在寻找答案,最好在ContentPublishedEventHandler构造函数中注入依赖项,因此代码应如下所示:

if you are still looking for the answer it would be better to inject dependency in ContentPublishedEventHandler constructor, so the code would be looked like this:

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);
        });
    }
}

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

If you are lookin for more info about using Dependency Injection with Umbraco 7 please refer to https://web.archive.org/web/20160325201135/http://www.wearesicc.com/getting-started-with-umbraco-7-and-structuremap-v3/

这篇关于是否可以在Umbraco 7 ContentService事件处理程序中使用依赖项注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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