public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider, IApplicationLifetime applicationLifetime) { SocketHandler.Map(app); serviceProvider.GetService <CapHandler>().MessageHandler(); app.UseWebApi(_env); app.UseStateAutoMapper(); IWebSocketServer socket = null; applicationLifetime.ApplicationStarted.Register(() => { socket = app.ApplicationServices.GetRequiredService <ISocketService>() .StartAsync().ConfigureAwait(false).GetAwaiter().GetResult(); }); applicationLifetime.ApplicationStopping.Register(() => { socket?.Close(); }); }