Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fedor Krutko 13 posts 123 karma points
    Jan 28, 2022 @ 22:42
    Fedor Krutko
    0

    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 :

    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?

    Thanks, Fedor

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Jan 31, 2022 @ 09:29
    Matt Brailsford
    100

    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 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.

    Hope this helps

    Matt

  • Fedor Krutko 13 posts 123 karma points
    Jan 31, 2022 @ 09:33
    Fedor Krutko
    0

    Thank you Matt. I'll try both ways.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies