Construct ApplicationHost with DI framework instead of manually

This commit is contained in:
Mark Monteiro 2020-03-15 15:23:50 +01:00
parent 6a6293afc7
commit 6fbdf0d6a2
3 changed files with 19 additions and 21 deletions

View File

@ -613,7 +613,7 @@ namespace Emby.Server.Implementations
DiscoverTypes(); DiscoverTypes();
await RegisterResources(serviceCollection, startupConfig).ConfigureAwait(false); await RegisterServices(serviceCollection, startupConfig).ConfigureAwait(false);
ContentRoot = ServerConfigurationManager.Configuration.DashboardSourcePath; ContentRoot = ServerConfigurationManager.Configuration.DashboardSourcePath;
if (string.IsNullOrEmpty(ContentRoot)) if (string.IsNullOrEmpty(ContentRoot))
@ -650,9 +650,9 @@ namespace Emby.Server.Implementations
} }
/// <summary> /// <summary>
/// Registers resources that classes will depend on /// Registers services/resources with the service collection that will be available via DI.
/// </summary> /// </summary>
protected async Task RegisterResources(IServiceCollection serviceCollection, IConfiguration startupConfig) protected async Task RegisterServices(IServiceCollection serviceCollection, IConfiguration startupConfig)
{ {
serviceCollection.AddMemoryCache(); serviceCollection.AddMemoryCache();
@ -770,20 +770,8 @@ namespace Emby.Server.Implementations
CertificateInfo = GetCertificateInfo(true); CertificateInfo = GetCertificateInfo(true);
Certificate = GetCertificate(CertificateInfo); Certificate = GetCertificate(CertificateInfo);
HttpServer = new HttpListenerHost( serviceCollection.AddSingleton<IHttpListener, WebSocketSharpListener>();
this, serviceCollection.AddSingleton<IHttpServer, HttpListenerHost>();
LoggerFactory.CreateLogger<HttpListenerHost>(),
ServerConfigurationManager,
startupConfig,
NetworkManager,
JsonSerializer,
XmlSerializer,
CreateHttpListener())
{
GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading")
};
serviceCollection.AddSingleton(HttpServer);
ImageProcessor = new ImageProcessor(LoggerFactory.CreateLogger<ImageProcessor>(), ServerConfigurationManager.ApplicationPaths, FileSystemManager, ImageEncoder, () => LibraryManager, () => MediaEncoder); ImageProcessor = new ImageProcessor(LoggerFactory.CreateLogger<ImageProcessor>(), ServerConfigurationManager.ApplicationPaths, FileSystemManager, ImageEncoder, () => LibraryManager, () => MediaEncoder);
serviceCollection.AddSingleton(ImageProcessor); serviceCollection.AddSingleton(ImageProcessor);
@ -891,6 +879,14 @@ namespace Emby.Server.Implementations
((LibraryManager)LibraryManager).ItemRepository = ItemRepository; ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
} }
/// <summary>
/// Create services registered with the service container that need to be initialized at application startup.
/// </summary>
public void InitializeServices()
{
HttpServer = Resolve<IHttpServer>();
}
public static void LogEnvironmentInfo(ILogger logger, IApplicationPaths appPaths) public static void LogEnvironmentInfo(ILogger logger, IApplicationPaths appPaths)
{ {
// Distinct these to prevent users from reporting problems that aren't actually problems // Distinct these to prevent users from reporting problems that aren't actually problems
@ -1196,8 +1192,6 @@ namespace Emby.Server.Implementations
}); });
} }
protected IHttpListener CreateHttpListener() => new WebSocketSharpListener(LoggerFactory.CreateLogger<WebSocketSharpListener>());
private CertificateInfo GetCertificateInfo(bool generateCertificate) private CertificateInfo GetCertificateInfo(bool generateCertificate)
{ {
// Custom cert // Custom cert

View File

@ -18,6 +18,7 @@ using MediaBrowser.Controller;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Events; using MediaBrowser.Model.Events;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.Services; using MediaBrowser.Model.Services;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
@ -59,7 +60,8 @@ namespace Emby.Server.Implementations.HttpServer
INetworkManager networkManager, INetworkManager networkManager,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
IXmlSerializer xmlSerializer, IXmlSerializer xmlSerializer,
IHttpListener socketListener) IHttpListener socketListener,
ILocalizationManager localizationManager)
{ {
_appHost = applicationHost; _appHost = applicationHost;
_logger = logger; _logger = logger;
@ -76,6 +78,7 @@ namespace Emby.Server.Implementations.HttpServer
Instance = this; Instance = this;
ResponseFilters = Array.Empty<Action<IRequest, HttpResponse, object>>(); ResponseFilters = Array.Empty<Action<IRequest, HttpResponse, object>>();
GlobalResponse = localizationManager.GetLocalizedString("StartupEmbyServerIsLoading");
} }
public event EventHandler<GenericEventArgs<IWebSocketConnection>> WebSocketConnected; public event EventHandler<GenericEventArgs<IWebSocketConnection>> WebSocketConnected;

View File

@ -191,8 +191,9 @@ namespace Jellyfin.Server
var webHost = CreateWebHostBuilder(appHost, serviceCollection, startupConfig, appPaths).Build(); var webHost = CreateWebHostBuilder(appHost, serviceCollection, startupConfig, appPaths).Build();
// A bit hacky to re-use service provider since ASP.NET doesn't allow a custom service collection. // Re-use the web host service provider in the app host since ASP.NET doesn't allow a custom service collection.
appHost.ServiceProvider = webHost.Services; appHost.ServiceProvider = webHost.Services;
appHost.InitializeServices();
appHost.FindParts(); appHost.FindParts();
Migrations.MigrationRunner.Run(appHost, _loggerFactory); Migrations.MigrationRunner.Run(appHost, _loggerFactory);