Copied to clipboard

Flag this post as spam?

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


  • JMC 4 posts 74 karma points
    Jan 05, 2024 @ 20:25
    JMC
    0

    INotificationAsyncHandler and BackgroundTaskQueue

    I am creating an implementation using MediaSavedNotification, in which I need to process the information of a CSV file, which has thousands of records and I am using this interface IBackgroundTaskQueue But I get the following notification Authorization error: Unauthorized access to URL: /umbraco/backoffice/umbracoapi/media/PostSave Contact your administrator for information. The whole process works correctly in the background service. When implementing the non-background processing, this works correctly, but the Media Library stays busy until it finishes processing the whole file. My code is below

    enter image description here

    public class MediaSavedNotificationHandler(IBackgroundTaskQueue queue, IServiceProvider serviceProvider) 
        : INotificationAsyncHandler<MediaSavedNotification>
    {
        public async Task HandleAsync(MediaSavedNotification notification, CancellationToken cancellationToken)
        {
            var mediaItems = notification.SavedEntities;
            foreach (var mediaItem in mediaItems)
            {
                if (mediaItem.Key == MediaNodeIdIdentifiers.MaintenanceProvidersFileNodeId)
                {
                    queue.QueueBackgroundWorkItem(async cancellationToken =>
                    {
                        using IServiceScope scope = serviceProvider.CreateScope();
                        var maintenanceProviderService = scope.ServiceProvider.GetRequiredService<IMaintenanceProviderService>();
                        await maintenanceProviderService.ImportProviders(mediaItem.Id);
                    });
                    notification.Messages.Add(new EventMessage("Maintenance Provider Notification",
                        "The Maintenance Providers import process has started and is running in the background.",
                        EventMessageType.Info));
                    return;
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft