we're trying to upgrade to Umbraco 9 and there is some issue with DI and registration of notification events.
We've followed the guide from the docs (https://vendr.net/docs/core/2.0.0/umbraco-v9/key-concepts/events/) but the app do crash on startup.
The error is :
AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:
Ecommerce.Core.Infrastructure.Handlers.VendrEvents.Actions.OrderSaved Lifetime: Singleton
ImplementationType: Ecommerce.Core.Infrastructure.Handlers.VendrEvents.Actions.OrderSaved':
Cannot consume scoped service 'Ecommerce.Core.Infrastructure.Services.MoveOrderService'
from singleton 'Ecommerce.Core.Infrastructure.Handlers.VendrEvents.Actions.OrderSaved'.)
The DI registration is like this:
public static class VendrDependencies
{
public static IUmbracoBuilder AddVendrDependencies(this IUmbracoBuilder builder)
{
builder.WithNotificationEvent<OrderSavedNotification>()
.RegisterHandler<OrderSaved>();
return builder;
}
}
And the handler itself:
public class OrderSaved : NotificationEventHandlerBase<OrderSavedNotification>
{
private readonly MoveOrderService _moveOrderService;
public OrderSaved(MoveOrderService moveOrderService)
{
_moveOrderService = moveOrderService;
}
public override void Handle(OrderSavedNotification evt)
{
//implementation
}
}
The MoveOrderService contains basically nothing:
public MoveOrderService()
{
}
public void MoveToAnotherShop(OrderReadOnly orderReadOnly, Guid destinationStoreId)
{
}
Both MoveOrderService and OrderSaved services are being added as scoped in the startup class, so I'm not sure why the error says that OrderSaved is a singleton.
Umbraco is 9.2.0 and Vendr is 2.0.5
Could it be something to do with NotificationEventHandlerBase?
Vendrs notification system is built on top of Umbraco collection builders implementation which I believe registers all collection items as singletons. As such, your event handler OrderSaved will be registered in the DI container as a singleton and so can't access your scoped MoveOrderService.
You'd currently either need to make your MoveOrderService be registered as a singleton or look at implementing an Accessor pattern so that the service can be resolved inside the singleton when it is needed.
DI of notification events
Hi Matt,
we're trying to upgrade to Umbraco 9 and there is some issue with DI and registration of notification events.
We've followed the guide from the docs (https://vendr.net/docs/core/2.0.0/umbraco-v9/key-concepts/events/) but the app do crash on startup.
The error is :
The DI registration is like this:
And the handler itself:
The MoveOrderService contains basically nothing:
Both MoveOrderService and OrderSaved services are being added as scoped in the startup class, so I'm not sure why the error says that OrderSaved is a singleton.
Umbraco is 9.2.0 and Vendr is 2.0.5
Could it be something to do with NotificationEventHandlerBase?
Thanks, Fedor
Hi Fedor,
Vendrs notification system is built on top of Umbraco collection builders implementation which I believe registers all collection items as singletons. As such, your event handler
OrderSaved
will be registered in the DI container as a singleton and so can't access your scopedMoveOrderService
.You'd currently either need to make your
MoveOrderService
be registered as a singleton or look at implementing anAccessor
pattern so that the service can be resolved inside the singleton when it is needed.Hope this helps
Matt
Thank you Matt. I'll try both ways.
is working on a reply...