From 6aab24fb88ebebb6046529cb3c5931f90d049cd3 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Sun, 19 Oct 2025 15:24:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9B=B4=E6=94=B9=E4=B8=BA=20TryAd?= =?UTF-8?q?dSingleton=20=E9=98=B2=E6=AD=A2=E9=87=8D=E5=A4=8D=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IP2Region.Net/Extensions/ServiceCollectionExtensions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs b/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs index 7a2f0f4..6f4ddde 100644 --- a/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs +++ b/binding/csharp/IP2Region.Net/Extensions/ServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using IP2Region.Net.Abstractions; using IP2Region.Net.XDB; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.Extensions.DependencyInjection; @@ -14,12 +15,12 @@ public static class IP2RegionExtensions /// public static IServiceCollection AddIP2RegionService(this IServiceCollection services, string path, CachePolicy cachePolicy = CachePolicy.Content) { - services.AddSingleton(provider => + services.TryAddSingleton(provider => { return new Searcher(cachePolicy, path); }); #if NET8_0_OR_GREATER - services.AddKeyedSingleton("IP2Region.Net", (provider, _) => + services.TryAddKeyedSingleton("IP2Region.Net", (provider, _) => { return provider.GetRequiredService(); });