public async Task <IEnumerable <Video> > Handle(SearchVideosQuery request, CancellationToken cancellationToken) { if (request.FromCache) { return(videoRepository.SearchAsync(request.SearchTerm)); } var videos = await youtubeApiClient.GetVideosAsync(request.SearchTerm); await videoRepository.AddRangeAsync(videos); // backgroundTaskQueue.QueueBackgroundWorkItem(async token => // { // using (var scope = serviceScopeFactory.CreateScope()) // { // var scopedServices = scope.ServiceProvider; // var loggerFactory = scopedServices.GetRequiredService<ILoggerFactory>(); // var innerLogger = loggerFactory.CreateLogger("SaveVideosWorker"); // var vr = scopedServices.GetRequiredService<IVideoRepository>(); // try // { // await vr.AddRangeAsync(videos); // } // catch (System.Exception ex) // { // innerLogger.LogError("Exception on save videos", ex); // } // } // }); return(videos); }