我在一个MVC项目中使用Umbraco7.1.1,并且我已经将它配置为使用依赖注入(在我的例子中是Castle.Windsor)。我也在使用NServiceBus发送消息等,它工作得很好。
我现在正在尝试连接到ContentService发布的事件-尝试并发布一个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是空的,因为我的依赖注入框架要么没有正确配置,要么(正如我怀疑的)从未被调用过。
如果我依赖于对总线的静态引用,我可以让它工作,但如果可以的话,我更愿意避免这种情况。我想要做的事情是可能的吗?Ie使用依赖注入与这些Umbraco事件?如果是这样,我需要什么配置来告诉Umbraco使用Castle.Windsor来创建我的事件处理程序?
发布于 2015-03-27 06:35:44
如果你还在寻找答案,最好是在ContentPublishedEventHandler构造函数中注入依赖项,这样代码看起来就像这样:
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/
https://stackoverflow.com/questions/24048127
复制相似问题