diff --git a/MTSC.UnitTests/E2ETests.cs b/MTSC.UnitTests/E2ETests.cs index c469b1c..9a0b65b 100644 --- a/MTSC.UnitTests/E2ETests.cs +++ b/MTSC.UnitTests/E2ETests.cs @@ -1,4 +1,5 @@ using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.VisualStudio.TestTools.UnitTesting; using MTSC.Common.Http; @@ -65,8 +66,8 @@ namespace MTSC.UnitTests .AddExceptionHandler(new ExceptionConsoleLogger()) .SetScheduler(new ParallelScheduler()) .WithSslAuthenticationTimeout(TimeSpan.FromMilliseconds(100)); - Server.ServiceManager.RegisterSingleton(); - Server.ServiceManager.RegisterSingleton(); + Server.ServiceCollection.AddSingleton(); + Server.ServiceCollection.AddSingleton(); Server.RunAsync(); } diff --git a/MTSC/MTSC.csproj b/MTSC/MTSC.csproj index 16c17bf..7d4285f 100644 --- a/MTSC/MTSC.csproj +++ b/MTSC/MTSC.csproj @@ -5,13 +5,13 @@ netstandard2.0 - 5.4.3 + 5.5 latest Alexandru-Victor Macocian MTSC Modular TCP Server and Client - 5.4.3.0 - 5.4.3.0 + 5.5.0.0 + 5.5.0.0 true AnyCPU;x64 https://github.com/AlexMacocian/MTSC @@ -27,9 +27,10 @@ - + - + + diff --git a/MTSC/ServerSide/BackgroundServices/BackgroundServicesHolder.cs b/MTSC/ServerSide/BackgroundServices/BackgroundServicesHolder.cs index 1c562b3..8a83885 100644 --- a/MTSC/ServerSide/BackgroundServices/BackgroundServicesHolder.cs +++ b/MTSC/ServerSide/BackgroundServices/BackgroundServicesHolder.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.DependencyInjection; +using System; using System.Collections.Generic; namespace MTSC.ServerSide.BackgroundServices @@ -16,7 +17,7 @@ namespace MTSC.ServerSide.BackgroundServices public void RegisterBackgroundService(TimeSpan activationInterval) where T : BackgroundServiceBase { - this.server.ServiceManager.RegisterSingleton(); + this.server.ServiceCollection.AddSingleton(); this.backgroundServices.Add(new BackgroundServiceMetadata { ActivationInterval = activationInterval, RegisteredType = typeof(T) }); } diff --git a/MTSC/ServerSide/Server.cs b/MTSC/ServerSide/Server.cs index 05a6c5d..aa85c5b 100644 --- a/MTSC/ServerSide/Server.cs +++ b/MTSC/ServerSide/Server.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; using MTSC.Common; using MTSC.Exceptions; using MTSC.ServerSide.BackgroundServices; @@ -7,6 +8,7 @@ using MTSC.ServerSide.Listeners; using MTSC.ServerSide.Schedulers; using MTSC.ServerSide.UsageMonitors; using Slim; +using Slim.Integration.ServiceCollection; using System; using System.Collections.Generic; using System.Linq; @@ -104,9 +106,13 @@ namespace MTSC.ServerSide /// public IReadOnlyCollection Clients { get => this.clients.AsReadOnly(); } /// - /// for configuring and retrieving services. + /// for configuring and retrieving services. Will be initialized at server startup from the . /// - public IServiceManager ServiceManager { get; } = new ServiceManager(); + public IServiceManager ServiceManager { get; private set; } + /// + /// used to create the at server startup. + /// + public IServiceCollection ServiceCollection { get; } = new ServiceCollection(); #endregion #region Constructors /// @@ -114,8 +120,7 @@ namespace MTSC.ServerSide /// public Server() { - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(sp => this); + this.ServiceCollection.AddSingleton(this); this.backgroundServicesHolder = new(this); } /// @@ -125,8 +130,7 @@ namespace MTSC.ServerSide public Server(int port) { this.Port = port; - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(sp => this); + this.ServiceCollection.AddSingleton(this); this.backgroundServicesHolder = new(this); } /// @@ -138,8 +142,7 @@ namespace MTSC.ServerSide { this.certificate = certificate; this.Port = port; - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(sp => this); + this.ServiceCollection.AddSingleton(this); this.backgroundServicesHolder = new(this); } /// @@ -151,8 +154,7 @@ namespace MTSC.ServerSide { this.IPAddress = ipAddress; this.Port = port; - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(sp => this); + this.ServiceCollection.AddSingleton(this); this.backgroundServicesHolder = new(this); } /// @@ -166,97 +168,12 @@ namespace MTSC.ServerSide this.certificate = certificate; this.Port = port; this.IPAddress = ipAddress; - this.ServiceManager.RegisterServiceManager(); - this.ServiceManager.RegisterSingleton(sp => this); + this.ServiceCollection.AddSingleton(this); this.backgroundServicesHolder = new(this); } #endregion #region Public Methods /// - /// Adds a service with transient lifetime. - /// - /// This server object. - public Server AddTransientService() - where TService : TInterface - where TInterface : class - { - this.ServiceManager.RegisterTransient(); - return this; - } - /// - /// Adds a service with transient lifetime. Registers the service for all the interfaces it implements. - /// - /// This server object. - public Server AddTransientService() - where TService : class - { - this.ServiceManager.RegisterTransient(); - return this; - } - /// - /// Adds a service with transient lifetime. - /// - /// This server object. - public Server AddTransientService(Func serviceFactory) - where TService : TInterface - where TInterface : class - { - this.ServiceManager.RegisterTransient(serviceFactory); - return this; - } - /// - /// Adds a service with transient lifetime. Registers the service for all the interfaces it implements. - /// - /// This server object. - public Server AddTransientService(Func serviceFactory) - where TService : class - { - this.ServiceManager.RegisterTransient(serviceFactory); - return this; - } - /// - /// Adds a service with singleton lifetime. - /// - /// This server object. - public Server AddSingletonService() - where TService : TInterface - where TInterface : class - { - this.ServiceManager.RegisterSingleton(); - return this; - } - /// - /// Adds a service with singleton lifetime. Registers the service for all the interfaces it implements. - /// - /// This server object. - public Server AddSingletonService() - where TService : class - { - this.ServiceManager.RegisterSingleton(); - return this; - } - /// - /// Adds a service with singleton lifetime. - /// - /// This server object. - public Server AddSingletonService(Func serviceFactory) - where TService : TInterface - where TInterface : class - { - this.ServiceManager.RegisterSingleton(serviceFactory); - return this; - } - /// - /// Adds a service with singleton lifetime. Registers the service for all the interfaces it implements. - /// - /// This server object. - public Server AddSingletonService(Func serviceFactory) - where TService : class - { - this.ServiceManager.RegisterSingleton(serviceFactory); - return this; - } - /// /// Adds a to the server. /// /// Interval between ticks. @@ -419,16 +336,6 @@ namespace MTSC.ServerSide return this; } /// - /// Gets a required service. - /// - /// Type of the service used during registration. - /// - public T GetService() - where T : class - { - return this.ServiceManager.GetService(); - } - /// /// Get handler of provided type /// /// @@ -521,6 +428,8 @@ namespace MTSC.ServerSide return; } + this.ServiceManager?.Dispose(); + this.ServiceManager = this.ServiceCollection.BuildSlimServiceProvider() as IServiceManager; this.Listener?.Stop(); if (this.logger is null) {