diff --git a/src/BootstrapBlazor.Shared/Samples/Locator.razor b/src/BootstrapBlazor.Shared/Samples/Locator.razor index 3e0fc388fbc28c36815e82a972f7474181fa2017..4ed2419548b7dfee5b16218abdde4326f6000a36 100644 --- a/src/BootstrapBlazor.Shared/Samples/Locator.razor +++ b/src/BootstrapBlazor.Shared/Samples/Locator.razor @@ -49,7 +49,7 @@ private IIPLocatorProvider? IPLocator { get; set; } { services.AddBootstrapBlazor(locatorAction: option => { - option.LocatorFactory = () => new CustomerLocator(); + option.LocatorFactory = provider => new CustomerLocator(); }); }
@((MarkupString)Localizer["BasicUsageP12"].Value)
diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 8e59b974371205951b6f49d9598769522d9ff940..44c06e1c5df115a9de5ee7c4c901a869bf56ec71 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 6.2.8-beta01 + 6.2.8 diff --git a/src/BootstrapBlazor/Components/IPLocator/DefaultIPLocatorProvider.cs b/src/BootstrapBlazor/Components/IPLocator/DefaultIPLocatorProvider.cs index 643fc5e32955b78617283dccda4753581de6f94a..82f1c972b0c7fba50ebeffc7daa9f730c554d5b8 100644 --- a/src/BootstrapBlazor/Components/IPLocator/DefaultIPLocatorProvider.cs +++ b/src/BootstrapBlazor/Components/IPLocator/DefaultIPLocatorProvider.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System; using System.Linq; using System.Net.Http; using System.Threading.Tasks; @@ -17,14 +18,18 @@ internal class DefaultIPLocatorProvider : IIPLocatorProvider { private readonly IPLocatorOption _option; + private readonly IServiceProvider _provider; + /// /// 构造函数 /// + /// /// /// /// - public DefaultIPLocatorProvider(IHttpClientFactory factory, ILogger logger, IOptions option) + public DefaultIPLocatorProvider(IServiceProvider provider, IHttpClientFactory factory, ILogger logger, IOptions option) { + _provider = provider; _option = option.Value; _option.HttpClient = factory.CreateClient(); _option.Logger = logger; @@ -50,7 +55,7 @@ internal class DefaultIPLocatorProvider : IIPLocatorProvider _option.IP = ip; if (_option.LocatorFactory != null) { - var locator = _option.LocatorFactory(); + var locator = _option.LocatorFactory(_provider); if (locator != null) { ret = await locator.Locate(_option); diff --git a/src/BootstrapBlazor/Components/IPLocator/IPLocatorOption.cs b/src/BootstrapBlazor/Components/IPLocator/IPLocatorOption.cs index 3d4d256ee8dc7f431fe4d0548e462f447353cd97..89f3c866658d12b8489ddc32045d711ced2d3b76 100644 --- a/src/BootstrapBlazor/Components/IPLocator/IPLocatorOption.cs +++ b/src/BootstrapBlazor/Components/IPLocator/IPLocatorOption.cs @@ -17,7 +17,7 @@ public class IPLocatorOption /// /// 获得/设置 定位器创建方法未设置使用内部定位器 /// - public Func? LocatorFactory { get; set; } + public Func? LocatorFactory { get; set; } /// /// 获得/设置 IP地址请求超时时间 默认为 3000 毫秒 diff --git a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs index fe74bb5fa979780c943aee7bc967db65be1c7d0f..682f2894cb788548ac4242626cc1e59907d1631f 100644 --- a/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs +++ b/src/BootstrapBlazor/Extensions/BootstrapBlazorServiceCollectionExtensions.cs @@ -22,9 +22,8 @@ public static class BootstrapBlazorServiceCollectionExtensions /// /// /// - /// /// - public static IServiceCollection AddBootstrapBlazor(this IServiceCollection services, Action? configureOptions = null, Action? localizationAction = null, Action? locatorAction = null) + public static IServiceCollection AddBootstrapBlazor(this IServiceCollection services, Action? configureOptions = null, Action? localizationAction = null) { services.AddMemoryCache(); services.AddHttpClient(); @@ -67,13 +66,24 @@ public static class BootstrapBlazorServiceCollectionExtensions services.TryAddSingleton(); services.TryAddSingleton, ConfigureOptions>(); + return services; + } + + /// + /// + /// + /// + /// + /// + public static IServiceCollection ConfigIPLocatorOption(this IServiceCollection services, Action locatorAction) + { services.Configure(options => { - locatorAction?.Invoke(options); + locatorAction(options); if (options.LocatorFactory == null) { - options.LocatorFactory = () => new BaiDuIPLocator(); + options.LocatorFactory = provider => new BaiDuIPLocator(); } }); return services;