Skip to content

Commit f2f2a72

Browse files
committed
refactor: 增加 JSModule 检查逻辑
1 parent e80ffb1 commit f2f2a72

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/BootstrapBlazor/Services/WebClientService.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,17 @@ public async Task<ClientInfo> GetClientInfo()
4343
try
4444
{
4545
_jsModule ??= await runtime.LoadModuleByName("client");
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));
46+
if (_jsModule != null)
47+
{
48+
_interop ??= DotNetObjectReference.Create(this);
49+
await _jsModule.InvokeVoidAsync("ping", "ip.axd", _interop, nameof(SetData));
50+
// 等待 SetData 方法执行完毕
51+
await _taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(3));
52+
}
5153
}
52-
catch (TimeoutException) { }
5354
catch (Exception ex)
5455
{
55-
logger.LogError(ex, "method GetClientInfo failed");
56+
logger.LogError(ex, "{GetClientInfo} throw exception", nameof(GetClientInfo));
5657
}
5758

5859
// 补充 IP 地址信息

0 commit comments

Comments
 (0)