Dependency injection 是否可以将依赖项注入与Umbraco 7 ContentService事件处理程序一起使用?

Dependency injection 是否可以将依赖项注入与Umbraco 7 ContentService事件处理程序一起使用?,dependency-injection,umbraco7,Dependency Injection,Umbraco7,我在MVC项目中使用了Umbraco 7.1.1,并将其配置为使用依赖项注入(在我的例子中是Castle.Windsor)。我还使用NServiceBus发送消息等,它工作得很好 我现在正尝试挂接ContentService Published事件—尝试并发布一个NServiceBus事件,以提醒其他服务内容已更改。我想做的是这样的: public class ContentPublishedEventHandler : ApplicationEventHandler { public

我在MVC项目中使用了Umbraco 7.1.1,并将其配置为使用依赖项注入(在我的例子中是Castle.Windsor)。我还使用NServiceBus发送消息等,它工作得很好

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

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);
        });
    }
}
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);
        });
    }
}
公共类ContentPublishedEventHandler:ApplicationEventHandler
{
公共IBus总线{get;set;}
公共内容PublishedEventHandler()
{
ContentService.Published+=ContentServiceOnPublished;
}
私有void ContentServiceOnPublished(IPPublishingStrategy发件人,PublishEventArgs PublishEventArgs)
{
发布总线(e=>
{
e、 UpdatedNodeIds=publishEventArgs.publishedenties.Select(c=>c.Id);
});
}
}
但在这种情况下,
Bus
为空,因为我的依赖注入框架要么配置不正确,要么(我怀疑)从未调用过


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

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

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);
        });
    }
}
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);
        });
    }
}
公共类ContentPublishedEventHandler:ApplicationEventHandler
{
公共IBus总线{get;set;}
公共内容发布器(IBus总线)
{
总线=总线;
}
受保护的覆盖无效应用程序启动(UmbracoApplicationBase umbracoApplication、ApplicationContext ApplicationContext)
{
ContentService.Published+=ContentServiceOnPublished;
base.ApplicationStarting(umbraco应用程序、applicationContext);
}
私有void ContentServiceOnPublished(IPPublishingStrategy发件人,PublishEventArgs PublishEventArgs)
{
发布总线(e=>
{
e、 UpdatedNodeIds=publishEventArgs.publishedenties.Select(c=>c.Id);
});
}
}
如果您想了解有关在Umbraco 7中使用依赖项注入的更多信息,请参阅