99namespace BootstrapBlazor . Components ;
1010
1111/// <summary>
12- /// 默认 IP2Region 实现
12+ /// 默认 IP2Region 实现
1313/// </summary>
1414class IP2RegionService : DefaultIpLocatorProvider
1515{
@@ -25,26 +25,29 @@ public IP2RegionService(IOptions<BootstrapBlazorOptions> options, IOptions<IP2Re
2525 _ipOptions = ipRegionOptions ;
2626 _logger = logger ;
2727
28- InitSearch ( ) ;
28+ Task . Run ( InitSearch , CancellationToken . None ) . ConfigureAwait ( false ) ;
2929 }
3030
3131 private readonly IOptions < BootstrapBlazorOptions > _options ;
3232 private readonly IOptions < IP2RegionOptions > _ipOptions ;
3333 private readonly ILogger < IP2RegionService > _logger ;
34+ private readonly TaskCompletionSource _tcs = new ( ) ;
3435 private Searcher ? _search ;
3536
3637 /// <summary>
3738 /// <inheritdoc/>
3839 /// </summary>
3940 /// <param name="ip"></param>
40- protected override Task < string ? > LocateByIp ( string ip )
41+ protected override async Task < string ? > LocateByIp ( string ip )
4142 {
43+ await _tcs . Task ;
44+
4245 string ? result = null ;
4346 if ( _search != null && _options . Value . WebClientOptions . EnableIpLocator )
4447 {
4548 result = _search . Search ( ip ) ;
4649 }
47- return Task . FromResult ( result ) ;
50+ return result ;
4851 }
4952
5053 private void InitSearch ( )
@@ -60,9 +63,11 @@ private void InitSearch()
6063 {
6164 _search = new Searcher ( CachePolicy . Content , xdbPath ) ;
6265 _logger . LogInformation ( "IP2Region xdb file {dbPath} loaded" , xdbPath ) ;
66+ _tcs . TrySetResult ( ) ;
6367 }
6468 catch ( Exception ex )
6569 {
70+ _tcs . TrySetException ( ex ) ;
6671 _logger . LogError ( ex , "IP2Region xdb file path: {dbPath}" , xdbPath ) ;
6772 }
6873 }
0 commit comments