csharp/Azure/azure-signalr/src/Microsoft.Azure.SignalR/HubHost/ServerLifetimeManager.cs

ServerLifetimeManager.cs
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Azure.SignalR
{
    internal clast ServerLifetimeManager
    {
        private readonly ConcurrentBag _shutdownHooks = new ConcurrentBag();

        public ServerLifetimeManager(
            IServiceProvider provider
        )
        {
#if NETCOREAPP
            var lifetime = provider.GetService();
#elif NETSTANDARD
            var lifetime = provider.GetService();
#endif
            lifetime?.ApplicationStopping.Register(Shutdown);
        }

        internal void Register(Func func)
        {
            _shutdownHooks.Add(func);
        }

        private void Shutdown()
        {
            Task.WaitAll(_shutdownHooks.Select(func => func()).ToArray());
        }
    }
}