22using System . Net . Http ;
33using System . Text . RegularExpressions ;
44using System . Threading . Tasks ;
5+ using Microsoft . Extensions . DependencyInjection ;
56using Microsoft . Extensions . Logging ;
67using Microsoft . VisualStudio . Threading ;
78using OpenRCT2 . DB . Abstractions ;
@@ -13,15 +14,15 @@ public class NeDesignsService
1314 {
1415 private static readonly TimeSpan _queryWait = TimeSpan . FromHours ( 6 ) ;
1516
16- private readonly IRctObjectRepository _rctObjectRepo ;
17+ private readonly IServiceScopeFactory _scopeFactory ;
1718 private readonly HttpClient _httpClient ;
1819 private readonly ILogger < NeDesignsService > _logger ;
1920 private DateTime _lastQueryCheck ;
2021 private AsyncSemaphore _querySemaphore = new AsyncSemaphore ( 1 ) ;
2122
22- public NeDesignsService ( IRctObjectRepository rctObjectRepo , HttpClient httpClient , ILogger < NeDesignsService > logger )
23+ public NeDesignsService ( IServiceScopeFactory scopeFactory , HttpClient httpClient , ILogger < NeDesignsService > logger )
2324 {
24- _rctObjectRepo = rctObjectRepo ;
25+ _scopeFactory = scopeFactory ;
2526 _httpClient = httpClient ;
2627 _logger = logger ;
2728 }
@@ -40,8 +41,11 @@ public async Task SearchForNewObjectsAsync()
4041 {
4142 _lastQueryCheck = DateTime . UtcNow ;
4243
44+ using var scope = _scopeFactory . CreateScope ( ) ;
45+ var rctObjectRepo = scope . ServiceProvider . GetRequiredService < IRctObjectRepository > ( ) ;
46+
4347 // Find maximum NeDesigns ID
44- var obj = await _rctObjectRepo . GetLegacyObjectWithHighestNeIdAsync ( ) ;
48+ var obj = await rctObjectRepo . GetLegacyObjectWithHighestNeIdAsync ( ) ;
4549 var neId = obj == null ? 1 : obj . NeDesignId ;
4650 var fails = 0 ;
4751 while ( fails < 3 )
@@ -56,7 +60,7 @@ public async Task SearchForNewObjectsAsync()
5660 if ( match . Success )
5761 {
5862 var name = match . Groups [ 1 ] . Value . ToUpperInvariant ( ) ;
59- await _rctObjectRepo . UpdateLegacyAsync (
63+ await rctObjectRepo . UpdateLegacyAsync (
6064 new LegacyRctObject ( ) { NeDesignId = neId , Name = name } ) ;
6165 _logger . LogInformation ( $ "Adding new object: #{ neId } [{ name } ]") ;
6266 }
0 commit comments