From dc140429fe5d6718d516a02ee3951e191f9db621 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 23:17:30 +0100 Subject: [PATCH 1/6] Do not create unnecessary instances of IService at server startup --- Emby.Server.Implementations/ApplicationHost.cs | 2 +- .../HttpServer/HttpListenerHost.cs | 11 +++++------ MediaBrowser.Controller/Net/IHttpServer.cs | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index aa22ac8b9f..3c9bd207bb 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -1046,7 +1046,7 @@ namespace Emby.Server.Implementations .Where(i => i != null) .ToArray(); - HttpServer.Init(GetExports(false), GetExports(), GetUrlPrefixes()); + HttpServer.Init(GetExportTypes(), GetExports(), GetUrlPrefixes()); LibraryManager.AddParts( GetExports(), diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index 655130fcfc..a3b61b98bc 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -594,17 +594,16 @@ namespace Emby.Server.Implementations.HttpServer /// /// Adds the rest handlers. /// - /// The services. - /// - /// - public void Init(IEnumerable services, IEnumerable listeners, IEnumerable urlPrefixes) + /// The service types to register with the . + /// The web socket listeners. + /// The URL prefixes. See . + public void Init(IEnumerable serviceTypes, IEnumerable listeners, IEnumerable urlPrefixes) { _webSocketListeners = listeners.ToArray(); UrlPrefixes = urlPrefixes.ToArray(); ServiceController = new ServiceController(); - var types = services.Select(r => r.GetType()); - ServiceController.Init(this, types); + ServiceController.Init(this, serviceTypes); ResponseFilters = new Action[] { diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs index 46933c0465..806478864d 100644 --- a/MediaBrowser.Controller/Net/IHttpServer.cs +++ b/MediaBrowser.Controller/Net/IHttpServer.cs @@ -32,7 +32,7 @@ namespace MediaBrowser.Controller.Net /// /// Inits this instance. /// - void Init(IEnumerable services, IEnumerable listener, IEnumerable urlPrefixes); + void Init(IEnumerable serviceTypes, IEnumerable listener, IEnumerable urlPrefixes); /// /// If set, all requests will respond with this message From 72c822513f746fd30575a6dc2b076d647ffec4f3 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 23:18:41 +0100 Subject: [PATCH 2/6] Create ServiceController via DI instead of manual construction --- .../HttpServer/HttpListenerHost.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs index a3b61b98bc..7a812f3201 100644 --- a/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs +++ b/Emby.Server.Implementations/HttpServer/HttpListenerHost.cs @@ -60,7 +60,8 @@ namespace Emby.Server.Implementations.HttpServer IJsonSerializer jsonSerializer, IXmlSerializer xmlSerializer, IHttpListener socketListener, - ILocalizationManager localizationManager) + ILocalizationManager localizationManager, + ServiceController serviceController) { _appHost = applicationHost; _logger = logger; @@ -71,6 +72,8 @@ namespace Emby.Server.Implementations.HttpServer _jsonSerializer = jsonSerializer; _xmlSerializer = xmlSerializer; _socketListener = socketListener; + ServiceController = serviceController; + _socketListener.WebSocketConnected = OnWebSocketConnected; _funcParseFn = t => s => JsvReader.GetParseFn(t)(s); @@ -90,7 +93,7 @@ namespace Emby.Server.Implementations.HttpServer public string GlobalResponse { get; set; } - public ServiceController ServiceController { get; private set; } + public ServiceController ServiceController { get; } public object CreateInstance(Type type) { @@ -601,7 +604,6 @@ namespace Emby.Server.Implementations.HttpServer { _webSocketListeners = listeners.ToArray(); UrlPrefixes = urlPrefixes.ToArray(); - ServiceController = new ServiceController(); ServiceController.Init(this, serviceTypes); From dc2b8c464672d7bb69a32cf6f95555872f1f7ddf Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 23:19:24 +0100 Subject: [PATCH 3/6] Log a warning if an attempt is made to register an invalid service type --- .../Services/ServiceController.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Emby.Server.Implementations/Services/ServiceController.cs b/Emby.Server.Implementations/Services/ServiceController.cs index d963f9043c..3f9c8778b5 100644 --- a/Emby.Server.Implementations/Services/ServiceController.cs +++ b/Emby.Server.Implementations/Services/ServiceController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using Emby.Server.Implementations.HttpServer; using MediaBrowser.Model.Services; +using Microsoft.Extensions.Logging; namespace Emby.Server.Implementations.Services { @@ -11,6 +12,13 @@ namespace Emby.Server.Implementations.Services public class ServiceController { + private readonly ILogger _log; + + public ServiceController(ILogger log) + { + _log = log; + } + public void Init(HttpListenerHost appHost, IEnumerable serviceTypes) { foreach (var serviceType in serviceTypes) @@ -21,6 +29,13 @@ namespace Emby.Server.Implementations.Services public void RegisterService(HttpListenerHost appHost, Type serviceType) { + // Make sure the provided type implements IService + if (!serviceType.IsAssignableFrom(typeof(IService))) + { + _log.LogWarning("Tried to register a service that does not implement IService: {ServiceType}", serviceType); + return; + } + var processedReqs = new HashSet(); var actions = ServiceExecGeneral.Reset(serviceType); From 6f8c81ff7e8aba4eaa14b715cc180b824c735905 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 23:28:07 +0100 Subject: [PATCH 4/6] Register ServiceController with the DI framework --- Emby.Server.Implementations/ApplicationHost.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 3c9bd207bb..dcd54e651a 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -43,6 +43,7 @@ using Emby.Server.Implementations.Playlists; using Emby.Server.Implementations.ScheduledTasks; using Emby.Server.Implementations.Security; using Emby.Server.Implementations.Serialization; +using Emby.Server.Implementations.Services; using Emby.Server.Implementations.Session; using Emby.Server.Implementations.SocketSharp; using Emby.Server.Implementations.TV; @@ -758,6 +759,7 @@ namespace Emby.Server.Implementations CertificateInfo = GetCertificateInfo(true); Certificate = GetCertificate(CertificateInfo); + serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(); From b8580e58aadbbef6f99dfc15ab0fcc38f5210dd8 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Sat, 21 Mar 2020 23:28:27 +0100 Subject: [PATCH 5/6] Check for implementation of IService correctly --- Emby.Server.Implementations/Services/ServiceController.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Emby.Server.Implementations/Services/ServiceController.cs b/Emby.Server.Implementations/Services/ServiceController.cs index 3f9c8778b5..d546317a95 100644 --- a/Emby.Server.Implementations/Services/ServiceController.cs +++ b/Emby.Server.Implementations/Services/ServiceController.cs @@ -8,12 +8,17 @@ using Microsoft.Extensions.Logging; namespace Emby.Server.Implementations.Services { public delegate object ActionInvokerFn(object intance, object request); + public delegate void VoidActionInvokerFn(object intance, object request); public class ServiceController { private readonly ILogger _log; + /// + /// Initializes a new instance of the class. + /// + /// The logger. public ServiceController(ILogger log) { _log = log; @@ -30,7 +35,7 @@ namespace Emby.Server.Implementations.Services public void RegisterService(HttpListenerHost appHost, Type serviceType) { // Make sure the provided type implements IService - if (!serviceType.IsAssignableFrom(typeof(IService))) + if (!typeof(IService).IsAssignableFrom(serviceType)) { _log.LogWarning("Tried to register a service that does not implement IService: {ServiceType}", serviceType); return; From 2a521b0f68315a765ee1a970fabdd712739c4f31 Mon Sep 17 00:00:00 2001 From: Mark Monteiro Date: Thu, 2 Apr 2020 09:32:43 -0400 Subject: [PATCH 6/6] Rename 'log' to 'logger' --- .../Services/ServiceController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Emby.Server.Implementations/Services/ServiceController.cs b/Emby.Server.Implementations/Services/ServiceController.cs index d546317a95..e24a95dbb3 100644 --- a/Emby.Server.Implementations/Services/ServiceController.cs +++ b/Emby.Server.Implementations/Services/ServiceController.cs @@ -13,15 +13,15 @@ namespace Emby.Server.Implementations.Services public class ServiceController { - private readonly ILogger _log; + private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// - /// The logger. - public ServiceController(ILogger log) + /// The logger. + public ServiceController(ILogger logger) { - _log = log; + _logger = logger; } public void Init(HttpListenerHost appHost, IEnumerable serviceTypes) @@ -37,7 +37,7 @@ namespace Emby.Server.Implementations.Services // Make sure the provided type implements IService if (!typeof(IService).IsAssignableFrom(serviceType)) { - _log.LogWarning("Tried to register a service that does not implement IService: {ServiceType}", serviceType); + _logger.LogWarning("Tried to register a service that does not implement IService: {ServiceType}", serviceType); return; }