public async Task StartAsync(CancellationToken cancellationToken) { using (IServiceScope scope = services.CreateScope()) { IWebSocket iWebSocketScopedService = scope.ServiceProvider.GetRequiredService <IWebSocket>(); var connect = await iWebSocketScopedService.ConnectAndRegisterAsync(); } }
/// <summary> /// It contains the logic to start the background task. /// When using the Web Host, StartAsync is called after the server has started and IApplicationLifetime.ApplicationStarted is triggered. /// When using the Generic Host, StartAsync is called before ApplicationStarted is triggered. /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task StartAsync(CancellationToken cancellationToken) { logger.LogInformation("WebSocketHostingService is starting."); using (IServiceScope scope = services.CreateScope()) { IWebSocket iWebSocketScopedService = scope.ServiceProvider.GetRequiredService <IWebSocket>(); var connect = await iWebSocketScopedService.ConnectAndRegisterAsync(); logger.LogInformation($"iWebSocketScopedService.ConnectAndRegisterAsync() is connected: {connect}"); } }