Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class Online : IDisposable

[Inject]
[NotNull]
private WebClientService? WebClientService { get; set; }
private IWebClientService? WebClientService { get; set; }

private DynamicObjectContext? DataTableDynamicContext { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/Components/Samples/Client.razor
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<p class="code-label">@((MarkupString)Localizer["BasicUsageP3"].Value)</p>
<Pre>[Inject]
[NotNull]
private WebClientService? ClientService { get; set; }
private IWebClientService? ClientService { get; set; }

private ClientInfo? ClientInfo { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class Client
{
[Inject]
[NotNull]
private WebClientService? ClientService { get; set; }
private IWebClientService? ClientService { get; set; }

private ClientInfo _clientInfo = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial class Dispatches
{
[Inject]
[NotNull]
private WebClientService? ClientService { get; set; }
private IWebClientService? ClientService { get; set; }

[Inject]
[NotNull]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class Locators

[Inject]
[NotNull]
WebClientService? ClientService { get; set; }
IWebClientService? ClientService { get; set; }

[Inject]
[NotNull]
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3808,7 +3808,7 @@
"BasicUsageP1": "Introduction to usage",
"BasicUsageP2": "1. The <code>UseBootstrapBlazor</code> middleware in the <b>Startup.cs</b> file that client information collection is performed.",
"BasicUsageTips": "<code>app.UseBootstrapBlazor</code> Middleware is located assembly <code>BootstrapBlazor.Middleware</code>, please refer to this package yourself for proper use",
"BasicUsageP3": "2. The component uses the injection service <code>WebClientService</code> to call the <code>GetClientInfo</code> method.",
"BasicUsageP3": "2. The component uses the injection service <code>IWebClientService</code> to call the <code>GetClientInfo</code> method.",
"BasicUsageP4": "3. Turn on IP geolocation",
"LocatorsProviderOptions": "<code>BootstrapBlazorOptions</code> section <code>WebClientOptions</code> By default it is <code>false</code>, which means the IP address location function is not enabled. Please change it to <code>true</code> in the configuration file or code.",
"LocatorsProviderDesc1": "Update the <code>appsetting.json</code> project configuration file",
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor.Server/Locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3808,7 +3808,7 @@
"BasicUsageP1": "用法介绍",
"BasicUsageP2": "1. <b>Startup.cs</b> 文件中使用 <code>UseBootstrapBlazor</code> 中间件进行客户端信息收集",
"BasicUsageTips": "<code>app.UseBootstrapBlazor</code> 中间件位于程序集 <code>BootstrapBlazor.Middleware</code>,请自行引用此包才能正常使用",
"BasicUsageP3": "2. 组件中使用注入服务 <code>WebClientService</code> 调用 <code>GetClientInfo</code> 方法",
"BasicUsageP3": "2. 组件中使用注入服务 <code>IWebClientService</code> 调用 <code>GetClientInfo</code> 方法",
"BasicUsageP4": "3. 开启 IP 地理位置定位功能",
"LocatorsProviderOptions": "全局配置定位器选项 <code>WebClientOptions</code> 默认 <code>false</code> 没有启用 IP 地址定位功能,请在配置文件中或者代码中更改为 <code>true</code>",
"LocatorsProviderDesc1": "更新 <code>appsetting.json</code> 项目配置文件",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv
services.AddScoped<PrintService>();
services.AddScoped<TitleService>();
services.AddScoped<DownloadService>();
services.AddScoped<WebClientService>();
services.AddScoped<IWebClientService, WebClientService>();
services.AddScoped<AjaxService>();
services.AddScoped(typeof(DragDropService<>));
services.AddScoped<ClipboardService>();
Expand Down
23 changes: 23 additions & 0 deletions src/BootstrapBlazor/Services/IWebClientService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

namespace BootstrapBlazor.Components;

/// <summary>
/// WebClient 服务类
/// </summary>
public interface IWebClientService : IAsyncDisposable
{
/// <summary>
/// 获得 ClientInfo 实例方法
/// </summary>
/// <returns></returns>
Task<ClientInfo> GetClientInfo();
/// <summary>
/// SetData 方法由 JS 调用
/// </summary>
/// <param name="client"></param>
void SetData(ClientInfo client);
}
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Services/WebClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using Microsoft.Extensions.Logging;

using System.Text.Json.Serialization;

namespace BootstrapBlazor.Components;
Expand All @@ -20,7 +21,7 @@ public class WebClientService(IIpLocatorFactory ipLocatorFactory,
IOptionsMonitor<BootstrapBlazorOptions> options,
IJSRuntime runtime,
NavigationManager navigation,
ILogger<WebClientService> logger) : IAsyncDisposable
ILogger<WebClientService> logger) : IAsyncDisposable, IWebClientService
{
private TaskCompletionSource? _taskCompletionSource;
private JSModule? _jsModule;
Expand Down
6 changes: 3 additions & 3 deletions test/UnitTest/Services/ConnectionHubTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public async Task Callback_Ok()
BeatInterval = TimeSpan.FromMilliseconds(200)
};

var client = context.Services.GetRequiredService<WebClientService>();
var client = context.Services.GetRequiredService<IWebClientService>();
var service = context.Services.GetRequiredService<IConnectionService>();
var cut = context.RenderComponent<ConnectionHub>();
await cut.InvokeAsync(async () =>
Expand Down Expand Up @@ -60,7 +60,7 @@ await cut.InvokeAsync(async () =>

// 触发内部 ClientInfo 为空情况 覆盖 _clientInfo ??= new();
options.Value.ConnectionHubOptions.Enable = false;
client = context.Services.GetRequiredService<WebClientService>();
client = context.Services.GetRequiredService<IWebClientService>();
cut = context.RenderComponent<ConnectionHub>();
await cut.InvokeAsync(async () =>
{
Expand All @@ -71,7 +71,7 @@ await cut.InvokeAsync(async () =>
// 设置 EnableIpLocator 为 false
options.Value.ConnectionHubOptions.Enable = true;
options.Value.ConnectionHubOptions.EnableIpLocator = false;
client = context.Services.GetRequiredService<WebClientService>();
client = context.Services.GetRequiredService<IWebClientService>();
cut = context.RenderComponent<ConnectionHub>();
await cut.InvokeAsync(async () =>
{
Expand Down
10 changes: 5 additions & 5 deletions test/UnitTest/Services/WebClientServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task WebClientService_Ok()
Engine = "engine",
UserAgent = "test_agent"
};
var service = Context.Services.GetRequiredService<WebClientService>();
var service = Context.Services.GetRequiredService<IWebClientService>();
service.SetData(mockData);
ClientInfo? client = null;
_ = Task.Run(async () => client = await service.GetClientInfo());
Expand Down Expand Up @@ -53,15 +53,15 @@ public async Task WebClientOptions_Ok()
var options = Context.Services.GetRequiredService<IOptionsMonitor<BootstrapBlazorOptions>>();
options.CurrentValue.WebClientOptions.EnableIpLocator = true;

var service = Context.Services.GetRequiredService<WebClientService>();
var service = Context.Services.GetRequiredService<IWebClientService>();
var client = await service.GetClientInfo();
Assert.Null(client.Ip);
}

[Fact]
public async Task GetClientInfo_Error()
{
var service = Context.Services.GetRequiredService<WebClientService>();
var service = Context.Services.GetRequiredService<IWebClientService>();
var client = await service.GetClientInfo();

// TimeoutException
Expand All @@ -75,7 +75,7 @@ public async Task GetClientInfo_Error()
[Fact]
public async Task SetData_Ok()
{
var service = Context.Services.GetRequiredService<WebClientService>();
var service = Context.Services.GetRequiredService<IWebClientService>();

// 内部 ReturnTask 为空
var fieldInfo = service.GetType().GetField("_taskCompletionSource", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Expand All @@ -94,6 +94,6 @@ public async Task SetData_Ok()
[Fact]
public async Task WebClientService_Dispose()
{
await (Context.Services.GetRequiredService<WebClientService>() as IAsyncDisposable).DisposeAsync();
await (Context.Services.GetRequiredService<IWebClientService>() as IAsyncDisposable).DisposeAsync();
}
}
Loading