Skip to content

Commit 2b0baaa

Browse files
committed
Fix NeDesignsService singleton
1 parent 2504ba9 commit 2b0baaa

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/OpenRCT2.API/Services/NeDesignsService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Net.Http;
33
using System.Text.RegularExpressions;
44
using System.Threading.Tasks;
5+
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Logging;
67
using Microsoft.VisualStudio.Threading;
78
using 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
}

src/OpenRCT2.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void ConfigureServices(IServiceCollection services)
5252
services.AddSingleton<ILocalisationService, LocalisationService>();
5353
services.AddSingleton<Emailer>();
5454
services.AddSingleton<GoogleRecaptchaService>();
55-
services.AddScoped<NeDesignsService>();
55+
services.AddSingleton<NeDesignsService>();
5656
services.AddScoped<UserAccountService>();
5757
services.AddScoped<UserAuthenticationService>();
5858

0 commit comments

Comments
 (0)