Skip to content

Commit c001d4f

Browse files
authored
feat(WebClientService): add TimeoutException catch (#5283)
* refactor: 过滤掉超时异常 * refactor: 更新代码 * test: 更新单元测试 * refactor: 重构代码 * refactor: 更新代码
1 parent 73e6f95 commit c001d4f

File tree

4 files changed

+18
-10
lines changed

4 files changed

+18
-10
lines changed

src/BootstrapBlazor/Components/Affix/Affix.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public partial class Affix
4141

4242
private string? StyleString => CssBuilder.Default("position: sticky;")
4343
.AddStyle("z-index", $"{ZIndex}", ZIndex.HasValue)
44-
.AddStyle($"{Position.ToDescriptionString()}", $"{Offset}px")
44+
.AddStyle(Position.ToDescriptionString(), $"{Offset}px")
4545
.AddStyleFromAttributes(AdditionalAttributes)
4646
.Build();
4747
}

src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public partial class Drawer
1919
.Build();
2020

2121
private string? StyleString => CssBuilder.Default()
22-
.AddStyle("--bb-drawer-position", $"{Position}", !string.IsNullOrEmpty(Position))
22+
.AddStyle("--bb-drawer-position", Position)
2323
.AddStyleFromAttributes(AdditionalAttributes)
2424
.Build();
2525

2626
/// <summary>
2727
/// 获得 抽屉 Style 字符串
2828
/// </summary>
2929
private string? DrawerStyleString => CssBuilder.Default()
30-
.AddStyle("--bb-drawer-width", $"{Width}", !string.IsNullOrEmpty(Width) && Placement != Placement.Top && Placement != Placement.Bottom)
31-
.AddStyle("--bb-drawer-height", $"{Height}", !string.IsNullOrEmpty(Height) && (Placement == Placement.Top || Placement == Placement.Bottom))
30+
.AddStyle("--bb-drawer-width", Width, Placement != Placement.Top && Placement != Placement.Bottom)
31+
.AddStyle("--bb-drawer-height", Height, Placement == Placement.Top || Placement == Placement.Bottom)
3232
.Build();
3333

3434
/// <summary>

src/BootstrapBlazor/Services/WebClientService.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ public async Task<ClientInfo> GetClientInfo()
3939
{
4040
RequestUrl = navigation.Uri
4141
};
42-
_jsModule ??= await runtime.LoadModule("./_content/BootstrapBlazor/modules/client.js");
43-
_interop ??= DotNetObjectReference.Create(this);
44-
await _jsModule.InvokeVoidAsync("ping", "ip.axd", _interop, nameof(SetData));
4542

46-
// 等待 SetData 方法执行完毕
4743
try
4844
{
49-
await _taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(1));
45+
_jsModule ??= await runtime.LoadModule("./_content/BootstrapBlazor/modules/client.js");
46+
_interop ??= DotNetObjectReference.Create(this);
47+
await _jsModule.InvokeVoidAsync("ping", "ip.axd", _interop, nameof(SetData));
48+
49+
// 等待 SetData 方法执行完毕
50+
await _taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(3));
5051
}
52+
catch (TimeoutException) { }
5153
catch (Exception ex)
5254
{
5355
logger.LogError(ex, "method GetClientInfo failed");

test/UnitTest/Services/WebClientServiceTest.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,17 @@ public async Task WebClientOptions_Ok()
5959
}
6060

6161
[Fact]
62-
public async Task Timeout_Ok()
62+
public async Task GetClientInfo_Error()
6363
{
6464
var service = Context.Services.GetRequiredService<WebClientService>();
6565
var client = await service.GetClientInfo();
66+
67+
// TimeoutException
6668
Assert.Null(client.Ip);
69+
70+
// Exception
71+
Context.JSInterop.SetupVoid("ping", _ => true).SetException(new Exception("test-exception"));
72+
client = await service.GetClientInfo();
6773
}
6874

6975
[Fact]

0 commit comments

Comments
 (0)