diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 4fbc933a8..ef5f2966d 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(); @@ -1063,7 +1065,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 655130fcf..7a812f320 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) { @@ -594,17 +597,15 @@ 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/Emby.Server.Implementations/Services/ServiceController.cs b/Emby.Server.Implementations/Services/ServiceController.cs index d963f9043..e24a95dbb 100644 --- a/Emby.Server.Implementations/Services/ServiceController.cs +++ b/Emby.Server.Implementations/Services/ServiceController.cs @@ -3,14 +3,27 @@ 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 { public delegate object ActionInvokerFn(object intance, object request); + public delegate void VoidActionInvokerFn(object intance, object request); public class ServiceController { + private readonly ILogger _logger; + + /// + /// Initializes a new instance of the class. + /// + /// The logger. + public ServiceController(ILogger logger) + { + _logger = logger; + } + public void Init(HttpListenerHost appHost, IEnumerable serviceTypes) { foreach (var serviceType in serviceTypes) @@ -21,6 +34,13 @@ namespace Emby.Server.Implementations.Services public void RegisterService(HttpListenerHost appHost, Type serviceType) { + // Make sure the provided type implements IService + if (!typeof(IService).IsAssignableFrom(serviceType)) + { + _logger.LogWarning("Tried to register a service that does not implement IService: {ServiceType}", serviceType); + return; + } + var processedReqs = new HashSet(); var actions = ServiceExecGeneral.Reset(serviceType); diff --git a/MediaBrowser.Controller/Net/IHttpServer.cs b/MediaBrowser.Controller/Net/IHttpServer.cs index 46933c046..806478864 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