我正在使用一个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);
});
}
}
但是在这种情况下,Bus
是空的,因为我的依赖注入框架没有正确配置,或者(我怀疑),从未调用。
如果我依赖于对总线的静态引用,我可以让它工作,但如果可以的话,我宁愿避免这样做。我想做的事有可能吗?在这些Umbraco事件中使用依赖注入吗?如果是,我需要什么配置告诉Umbraco使用Castle。温莎创建我的事件处理程序?
如果你还在寻找答案,最好在contentpublishhedeventhandler构造函数中注入依赖关系,所以代码看起来像这样:
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/