From fa9c23cf21054469237d0cf4d2a9575ccda16b9e Mon Sep 17 00:00:00 2001 From: Rodion Mostovoi Date: Wed, 6 Dec 2023 01:13:04 +0800 Subject: [PATCH] Update AiClientStartupValidationBackgroundService and version Updated AiClientStartupValidationBackgroundService to use IServiceScopeFactory for creating a new scope, allowing access to IAiClient service inside ExecuteAsync method. A null check is also added for serviceScopeFactory. This change accommodates scenarios where AiClient may be required in the background service. Also, incremented the project version to 4.0.2-alpha in the Directory.Build.props file to reflect latest changes and maintain proper versioning. --- src/Directory.Build.props | 2 +- .../AiClientStartupValidationBackgroundService.cs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index d5d0f3b..f7784f0 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,6 +1,6 @@ - 4.0.1-alpha + 4.0.2-alpha net6.0;net7.0;net8.0 enable enable diff --git a/src/OpenAI.ChatGpt.AspNetCore/AiClientStartupValidationBackgroundService.cs b/src/OpenAI.ChatGpt.AspNetCore/AiClientStartupValidationBackgroundService.cs index 8ed0f4c..33ba57a 100644 --- a/src/OpenAI.ChatGpt.AspNetCore/AiClientStartupValidationBackgroundService.cs +++ b/src/OpenAI.ChatGpt.AspNetCore/AiClientStartupValidationBackgroundService.cs @@ -1,13 +1,20 @@ -using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace OpenAI.ChatGpt.AspNetCore; internal class AiClientStartupValidationBackgroundService : BackgroundService { + private readonly IServiceScopeFactory _serviceScopeFactory; - public AiClientStartupValidationBackgroundService(AiClientFromConfiguration _) + public AiClientStartupValidationBackgroundService(IServiceScopeFactory serviceScopeFactory) { + _serviceScopeFactory = serviceScopeFactory ?? throw new ArgumentNullException(nameof(serviceScopeFactory)); } - protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.CompletedTask; + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + await using var scope = _serviceScopeFactory.CreateAsyncScope(); + _ = scope.ServiceProvider.GetRequiredService(); + } } \ No newline at end of file