public CrashService(IHubContext<CrashHub> hubContext, GamesContext db)
{
_db = db;
_hubContext = hubContext;
}
scope.ServiceProvider.GetRequiredService<IHubContext<CrashHub>>()
, перенес это в ConfigureServices var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
IHubContext<CrashHub> crashHub = (IHubContext<CrashHub>)scope.ServiceProvider.GetService(typeof(IHubContext<CrashHub>));
GamesContext сrashService = new GamesContext(connection);
services.AddSingleton<IHostedService, CrashService>(s => new CrashService(crashHub, сrashService));
}
services.AddSingleton<IHostedService, CrashService>(s => new CrashService(new GamesContext(null, connection)));
, но теперь незнаю как передать IhubContext через ConfigureServices? Это вообще возможно? using (var scope = _scopeService.CreateScope())
{
GamesContext db = scope.ServiceProvider.GetRequiredService<GamesContext>();
_hubContext = scope.ServiceProvider.GetRequiredService<IHubContext<CrashHub>>();
Random rand = new Random();
endCoef = (float)(rand.Next(150) + rand.NextDouble());
CrashGame game = new CrashGame()
{
Status = "wait",
EndValue = endCoef,
WinnerId = 0
};
db.CrashGames.Add(game);
await db.SaveChangesAsync();
Task timer = Task.Run(async () => {
for (int i = interval; interval >= 0; i--)
{
await _hubContext.Clients.All.SendAsync("wait", i);
await Task.Delay(1000);
}
});
game.Status = "playing";
db.CrashGames.Update(game);
await db.SaveChangesAsync();
bool gameStatus = await UpdateCoefAsync();
if (gameStatus)
{
game.Status = "ended";
db.CrashGames.Update(game);
await db.SaveChangesAsync();
}
}
Программа "[4528] dotnet.exe" завершилась с кодом -1073741819 (0xc0000005) 'Access violation'.