diff options
| author | Bond_009 <bond.009@outlook.com> | 2020-05-02 00:54:04 +0200 |
|---|---|---|
| committer | Bond_009 <bond.009@outlook.com> | 2020-05-02 00:54:04 +0200 |
| commit | 15634a1913e8c1ad4e921364f30055794644d0bd (patch) | |
| tree | f0dfb3502728e4786de32c0cb4eaabd06f8e2d87 /Emby.Server.Implementations/ApplicationHost.cs | |
| parent | 407f54e7764a6bfd8e4ccc0df897fac7e8c658b4 (diff) | |
| parent | 62e251663fce8216cea529f85382299ac2f39fbc (diff) | |
Merge branch 'master' into websocket
Diffstat (limited to 'Emby.Server.Implementations/ApplicationHost.cs')
| -rw-r--r-- | Emby.Server.Implementations/ApplicationHost.cs | 742 |
1 files changed, 226 insertions, 516 deletions
diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index e3e071af9..8b387e195 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -1,5 +1,4 @@ #pragma warning disable CS1591 -#pragma warning disable SA1600 using System; using System.Collections.Concurrent; @@ -31,7 +30,6 @@ using Emby.Server.Implementations.Configuration; using Emby.Server.Implementations.Cryptography; using Emby.Server.Implementations.Data; using Emby.Server.Implementations.Devices; -using Emby.Server.Implementations.Diagnostics; using Emby.Server.Implementations.Dto; using Emby.Server.Implementations.HttpServer; using Emby.Server.Implementations.HttpServer.Security; @@ -44,6 +42,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.TV; using Emby.Server.Implementations.Updates; @@ -85,9 +84,7 @@ using MediaBrowser.MediaEncoding.BdInfo; using MediaBrowser.Model.Activity; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Cryptography; -using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.Dlna; -using MediaBrowser.Model.Events; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.IO; using MediaBrowser.Model.MediaInfo; @@ -99,15 +96,17 @@ using MediaBrowser.Model.Tasks; using MediaBrowser.Model.Updates; using MediaBrowser.Providers.Chapters; using MediaBrowser.Providers.Manager; +using MediaBrowser.Providers.Plugins.TheTvdb; using MediaBrowser.Providers.Subtitles; -using MediaBrowser.Providers.TV.TheTVDB; using MediaBrowser.WebDashboard.Api; using MediaBrowser.XbmcMetadata.Providers; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OperatingSystem = MediaBrowser.Common.System.OperatingSystem; +using Prometheus.DotNetRuntime; namespace Emby.Server.Implementations { @@ -116,15 +115,25 @@ namespace Emby.Server.Implementations /// </summary> public abstract class ApplicationHost : IServerApplicationHost, IDisposable { - private SqliteUserRepository _userRepository; + /// <summary> + /// The environment variable prefixes to log at server startup. + /// </summary> + private static readonly string[] _relevantEnvVarPrefixes = { "JELLYFIN_", "DOTNET_", "ASPNETCORE_" }; + + private readonly IFileSystem _fileSystemManager; + private readonly INetworkManager _networkManager; + private readonly IXmlSerializer _xmlSerializer; + private readonly IStartupOptions _startupOptions; - private SqliteDisplayPreferencesRepository _displayPreferencesRepository; + private IMediaEncoder _mediaEncoder; + private ISessionManager _sessionManager; + private IHttpServer _httpServer; + private IHttpClient _httpClient; /// <summary> /// Gets a value indicating whether this instance can self restart. /// </summary> - /// <value><c>true</c> if this instance can self restart; otherwise, <c>false</c>.</value> - public abstract bool CanSelfRestart { get; } + public bool CanSelfRestart => _startupOptions.RestartPath != null; public virtual bool CanLaunchWebBrowser { @@ -135,7 +144,7 @@ namespace Emby.Server.Implementations return false; } - if (StartupOptions.IsService) + if (_startupOptions.IsService) { return false; } @@ -165,10 +174,9 @@ namespace Emby.Server.Implementations public bool IsShuttingDown { get; private set; } /// <summary> - /// Gets or sets the logger. + /// Gets the logger. /// </summary> - /// <value>The logger.</value> - protected ILogger Logger { get; set; } + protected ILogger Logger { get; } private IPlugin[] _plugins; @@ -179,10 +187,9 @@ namespace Emby.Server.Implementations public IReadOnlyList<IPlugin> Plugins => _plugins; /// <summary> - /// Gets or sets the logger factory. + /// Gets the logger factory. /// </summary> - /// <value>The logger factory.</value> - public ILoggerFactory LoggerFactory { get; protected set; } + protected ILoggerFactory LoggerFactory { get; } /// <summary> /// Gets or sets the application paths. @@ -207,21 +214,6 @@ namespace Emby.Server.Implementations /// <value>The configuration manager.</value> protected IConfigurationManager ConfigurationManager { get; set; } - public IFileSystem FileSystemManager { get; set; } - - /// <inheritdoc /> - public PackageVersionClass SystemUpdateLevel - { - get - { -#if BETA - return PackageVersionClass.Beta; -#else - return PackageVersionClass.Release; -#endif - } - } - /// <summary> /// Gets or sets the service provider. /// </summary> @@ -238,125 +230,12 @@ namespace Emby.Server.Implementations public int HttpsPort { get; private set; } /// <summary> - /// Gets the content root for the webhost. - /// </summary> - public string ContentRoot { get; private set; } - - /// <summary> /// Gets the server configuration manager. /// </summary> /// <value>The server configuration manager.</value> public IServerConfigurationManager ServerConfigurationManager => (IServerConfigurationManager)ConfigurationManager; /// <summary> - /// Gets or sets the user manager. - /// </summary> - /// <value>The user manager.</value> - public IUserManager UserManager { get; set; } - - /// <summary> - /// Gets or sets the library manager. - /// </summary> - /// <value>The library manager.</value> - internal ILibraryManager LibraryManager { get; set; } - - /// <summary> - /// Gets or sets the directory watchers. - /// </summary> - /// <value>The directory watchers.</value> - private ILibraryMonitor LibraryMonitor { get; set; } - - /// <summary> - /// Gets or sets the provider manager. - /// </summary> - /// <value>The provider manager.</value> - private IProviderManager ProviderManager { get; set; } - - /// <summary> - /// Gets or sets the HTTP server. - /// </summary> - /// <value>The HTTP server.</value> - private IHttpServer HttpServer { get; set; } - - private IDtoService DtoService { get; set; } - - public IImageProcessor ImageProcessor { get; set; } - - /// <summary> - /// Gets or sets the media encoder. - /// </summary> - /// <value>The media encoder.</value> - private IMediaEncoder MediaEncoder { get; set; } - - private ISubtitleEncoder SubtitleEncoder { get; set; } - - private ISessionManager SessionManager { get; set; } - - private ILiveTvManager LiveTvManager { get; set; } - - public LocalizationManager LocalizationManager { get; set; } - - private IEncodingManager EncodingManager { get; set; } - - private IChannelManager ChannelManager { get; set; } - - /// <summary> - /// Gets or sets the user data repository. - /// </summary> - /// <value>The user data repository.</value> - private IUserDataManager UserDataManager { get; set; } - - internal SqliteItemRepository ItemRepository { get; set; } - - private INotificationManager NotificationManager { get; set; } - - private ISubtitleManager SubtitleManager { get; set; } - - private IChapterManager ChapterManager { get; set; } - - private IDeviceManager DeviceManager { get; set; } - - internal IUserViewManager UserViewManager { get; set; } - - private IAuthenticationRepository AuthenticationRepository { get; set; } - - private ITVSeriesManager TVSeriesManager { get; set; } - - private ICollectionManager CollectionManager { get; set; } - - private IMediaSourceManager MediaSourceManager { get; set; } - - private readonly IConfiguration _configuration; - - /// <summary> - /// Gets the installation manager. - /// </summary> - /// <value>The installation manager.</value> - protected IInstallationManager InstallationManager { get; private set; } - - protected IAuthService AuthService { get; private set; } - - public IStartupOptions StartupOptions { get; } - - internal IImageEncoder ImageEncoder { get; private set; } - - protected IProcessFactory ProcessFactory { get; private set; } - - protected readonly IXmlSerializer XmlSerializer; - - protected ISocketFactory SocketFactory { get; private set; } - - protected ITaskManager TaskManager { get; private set; } - - public IHttpClient HttpClient { get; private set; } - - protected INetworkManager NetworkManager { get; set; } - - public IJsonSerializer JsonSerializer { get; private set; } - - protected IIsoManager IsoManager { get; private set; } - - /// <summary> /// Initializes a new instance of the <see cref="ApplicationHost" /> class. /// </summary> public ApplicationHost( @@ -364,32 +243,39 @@ namespace Emby.Server.Implementations ILoggerFactory loggerFactory, IStartupOptions options, IFileSystem fileSystem, - IImageEncoder imageEncoder, - INetworkManager networkManager, - IConfiguration configuration) + INetworkManager networkManager) { - _configuration = configuration; - - XmlSerializer = new MyXmlSerializer(); + _xmlSerializer = new MyXmlSerializer(); - NetworkManager = networkManager; + _networkManager = networkManager; networkManager.LocalSubnetsFn = GetConfiguredLocalSubnets; ApplicationPaths = applicationPaths; LoggerFactory = loggerFactory; - FileSystemManager = fileSystem; + _fileSystemManager = fileSystem; - ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, XmlSerializer, FileSystemManager); + ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager); - Logger = LoggerFactory.CreateLogger("App"); + Logger = LoggerFactory.CreateLogger<ApplicationHost>(); - StartupOptions = options; + _startupOptions = options; - ImageEncoder = imageEncoder; + // Initialize runtime stat collection + if (ServerConfigurationManager.Configuration.EnableMetrics) + { + DotNetRuntimeStatsBuilder.Default().StartCollecting(); + } fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem)); - NetworkManager.NetworkChanged += OnNetworkChanged; + _networkManager.NetworkChanged += OnNetworkChanged; + + CertificateInfo = new CertificateInfo + { + Path = ServerConfigurationManager.Configuration.CertificatePath, + Password = ServerConfigurationManager.Configuration.CertificatePassword + }; + Certificate = GetCertificate(CertificateInfo); } public string ExpandVirtualPath(string path) @@ -457,10 +343,7 @@ namespace Emby.Server.Implementations } } - /// <summary> - /// Gets the name. - /// </summary> - /// <value>The name.</value> + /// <inheritdoc/> public string Name => ApplicationProductName; /// <summary> @@ -550,7 +433,7 @@ namespace Emby.Server.Implementations ConfigurationManager.ConfigurationUpdated += OnConfigurationUpdated; - MediaEncoder.SetFFmpegPath(); + _mediaEncoder.SetFFmpegPath(); Logger.LogInformation("ServerId: {0}", SystemId); @@ -562,7 +445,7 @@ namespace Emby.Server.Implementations Logger.LogInformation("Executed all pre-startup entry points in {Elapsed:g}", stopWatch.Elapsed); Logger.LogInformation("Core startup complete"); - HttpServer.GlobalResponse = null; + _httpServer.GlobalResponse = null; stopWatch.Restart(); await Task.WhenAll(StartEntryPoints(entryPoints, false)).ConfigureAwait(false); @@ -585,7 +468,8 @@ namespace Emby.Server.Implementations } } - public async Task InitAsync(IServiceCollection serviceCollection) + /// <inheritdoc/> + public void Init(IServiceCollection serviceCollection) { HttpPort = ServerConfigurationManager.Configuration.HttpServerPortNumber; HttpsPort = ServerConfigurationManager.Configuration.HttpsPortNumber; @@ -597,8 +481,6 @@ namespace Emby.Server.Implementations HttpsPort = ServerConfiguration.DefaultHttpsPort; } - JsonSerializer = new JsonSerializer(FileSystemManager); - if (Plugins != null) { var pluginBuilder = new StringBuilder(); @@ -618,23 +500,19 @@ namespace Emby.Server.Implementations DiscoverTypes(); - await RegisterResources(serviceCollection).ConfigureAwait(false); - - ContentRoot = ServerConfigurationManager.Configuration.DashboardSourcePath; - if (string.IsNullOrEmpty(ContentRoot)) - { - ContentRoot = ServerConfigurationManager.ApplicationPaths.WebPath; - } + RegisterServices(serviceCollection); } public Task ExecuteHttpHandlerAsync(HttpContext context, Func<Task> next) => HttpServer.RequestHandler(context); /// <summary> - /// Registers resources that classes will depend on + /// Registers services/resources with the service collection that will be available via DI. /// </summary> - protected async Task RegisterResources(IServiceCollection serviceCollection) + protected virtual void RegisterServices(IServiceCollection serviceCollection) { + serviceCollection.AddSingleton(_startupOptions); + serviceCollection.AddMemoryCache(); serviceCollection.AddSingleton(ConfigurationManager); @@ -642,223 +520,169 @@ namespace Emby.Server.Implementations serviceCollection.AddSingleton<IApplicationPaths>(ApplicationPaths); - serviceCollection.AddSingleton<IConfiguration>(_configuration); - - serviceCollection.AddSingleton(JsonSerializer); + serviceCollection.AddSingleton<IJsonSerializer, JsonSerializer>(); - serviceCollection.AddSingleton(LoggerFactory); - serviceCollection.AddLogging(); - serviceCollection.AddSingleton(Logger); - - serviceCollection.AddSingleton(FileSystemManager); - serviceCollection.AddSingleton<TvDbClientManager>(); + // TODO: Remove support for injecting ILogger completely + serviceCollection.AddSingleton((provider) => + { + Logger.LogWarning("Injecting ILogger directly is deprecated and should be replaced with ILogger<T>"); + return Logger; + }); - HttpClient = new HttpClientManager.HttpClientManager( - ApplicationPaths, - LoggerFactory.CreateLogger<HttpClientManager.HttpClientManager>(), - FileSystemManager, - () => ApplicationUserAgent); - serviceCollection.AddSingleton(HttpClient); + serviceCollection.AddSingleton(_fileSystemManager); + serviceCollection.AddSingleton<TvdbClientManager>(); - serviceCollection.AddSingleton(NetworkManager); + serviceCollection.AddSingleton<IHttpClient, HttpClientManager.HttpClientManager>(); - IsoManager = new IsoManager(); - serviceCollection.AddSingleton(IsoManager); + serviceCollection.AddSingleton(_networkManager); - TaskManager = new TaskManager(ApplicationPaths, JsonSerializer, LoggerFactory, FileSystemManager); - serviceCollection.AddSingleton(TaskManager); + serviceCollection.AddSingleton<IIsoManager, IsoManager>(); - serviceCollection.AddSingleton(XmlSerializer); + serviceCollection.AddSingleton<ITaskManager, TaskManager>(); - ProcessFactory = new ProcessFactory(); - serviceCollection.AddSingleton(ProcessFactory); + serviceCollection.AddSingleton(_xmlSerializer); - serviceCollection.AddSingleton(typeof(IStreamHelper), typeof(StreamHelper)); + serviceCollection.AddSingleton<IStreamHelper, StreamHelper>(); - var cryptoProvider = new CryptographyProvider(); - serviceCollection.AddSingleton<ICryptoProvider>(cryptoProvider); + serviceCollection.AddSingleton<ICryptoProvider, CryptographyProvider>(); - SocketFactory = new SocketFactory(); - serviceCollection.AddSingleton(SocketFactory); + serviceCollection.AddSingleton<ISocketFactory, SocketFactory>(); - serviceCollection.AddSingleton(typeof(IInstallationManager), typeof(InstallationManager)); + serviceCollection.AddSingleton<IInstallationManager, InstallationManager>(); - serviceCollection.AddSingleton(typeof(IZipClient), typeof(ZipClient)); + serviceCollection.AddSingleton<IZipClient, ZipClient>(); - serviceCollection.AddSingleton(typeof(IHttpResultFactory), typeof(HttpResultFactory)); + serviceCollection.AddSingleton<IHttpResultFactory, HttpResultFactory>(); serviceCollection.AddSingleton<IServerApplicationHost>(this); serviceCollection.AddSingleton<IServerApplicationPaths>(ApplicationPaths); serviceCollection.AddSingleton(ServerConfigurationManager); - LocalizationManager = new LocalizationManager(ServerConfigurationManager, JsonSerializer, LoggerFactory.CreateLogger<LocalizationManager>()); - await LocalizationManager.LoadAll().ConfigureAwait(false); - serviceCollection.AddSingleton<ILocalizationManager>(LocalizationManager); + serviceCollection.AddSingleton<ILocalizationManager, LocalizationManager>(); - serviceCollection.AddSingleton<IBlurayExaminer>(new BdInfoExaminer(FileSystemManager)); + serviceCollection.AddSingleton<IBlurayExaminer, BdInfoExaminer>(); - UserDataManager = new UserDataManager(LoggerFactory, ServerConfigurationManager, () => UserManager); - serviceCollection.AddSingleton(UserDataManager); + serviceCollection.AddSingleton<IUserDataRepository, SqliteUserDataRepository>(); + serviceCollection.AddSingleton<IUserDataManager, UserDataManager>(); - _displayPreferencesRepository = new SqliteDisplayPreferencesRepository( - LoggerFactory.CreateLogger<SqliteDisplayPreferencesRepository>(), - ApplicationPaths, - FileSystemManager); - serviceCollection.AddSingleton<IDisplayPreferencesRepository>(_displayPreferencesRepository); + serviceCollection.AddSingleton<IDisplayPreferencesRepository, SqliteDisplayPreferencesRepository>(); - ItemRepository = new SqliteItemRepository(ServerConfigurationManager, this, LoggerFactory.CreateLogger<SqliteItemRepository>(), LocalizationManager); - serviceCollection.AddSingleton<IItemRepository>(ItemRepository); + serviceCollection.AddSingleton<IItemRepository, SqliteItemRepository>(); - AuthenticationRepository = GetAuthenticationRepository(); - serviceCollection.AddSingleton(AuthenticationRepository); + serviceCollection.AddSingleton<IAuthenticationRepository, AuthenticationRepository>(); - _userRepository = GetUserRepository(); + serviceCollection.AddSingleton<IUserRepository, SqliteUserRepository>(); - UserManager = new UserManager( - LoggerFactory.CreateLogger<UserManager>(), - _userRepository, - XmlSerializer, - NetworkManager, - () => ImageProcessor, - () => DtoService, - this, - JsonSerializer, - FileSystemManager, - cryptoProvider); + // TODO: Refactor to eliminate the circular dependency here so that Lazy<T> isn't required + serviceCollection.AddTransient(provider => new Lazy<IDtoService>(provider.GetRequiredService<IDtoService>)); + serviceCollection.AddSingleton<IUserManager, UserManager>(); - serviceCollection.AddSingleton(UserManager); + // TODO: Refactor to eliminate the circular dependency here so that Lazy<T> isn't required + // TODO: Add StartupOptions.FFmpegPath to IConfiguration and remove this custom activation + serviceCollection.AddTransient(provider => new Lazy<EncodingHelper>(provider.GetRequiredService<EncodingHelper>)); + serviceCollection.AddSingleton<IMediaEncoder>(provider => + ActivatorUtilities.CreateInstance<MediaBrowser.MediaEncoding.Encoder.MediaEncoder>(provider, _startupOptions.FFmpegPath ?? string.Empty)); - LibraryManager = new LibraryManager(this, LoggerFactory, TaskManager, UserManager, ServerConfigurationManager, UserDataManager, () => LibraryMonitor, FileSystemManager, () => ProviderManager, () => UserViewManager); - serviceCollection.AddSingleton(LibraryManager); + // TODO: Refactor to eliminate the circular dependencies here so that Lazy<T> isn't required + serviceCollection.AddTransient(provider => new Lazy<ILibraryMonitor>(provider.GetRequiredService<ILibraryMonitor>)); + serviceCollection.AddTransient(provider => new Lazy<IProviderManager>(provider.GetRequiredService<IProviderManager>)); + serviceCollection.AddTransient(provider => new Lazy<IUserViewManager>(provider.GetRequiredService<IUserViewManager>)); + serviceCollection.AddSingleton<ILibraryManager, LibraryManager>(); - var musicManager = new MusicManager(LibraryManager); - serviceCollection.AddSingleton<IMusicManager>(musicManager); + serviceCollection.AddSingleton<IMusicManager, MusicManager>(); - LibraryMonitor = new LibraryMonitor(LoggerFactory, LibraryManager, ServerConfigurationManager, FileSystemManager); - serviceCollection.AddSingleton(LibraryMonitor); + serviceCollection.AddSingleton<ILibraryMonitor, LibraryMonitor>(); - serviceCollection.AddSingleton<ISearchEngine>(new SearchEngine(LoggerFactory, LibraryManager, UserManager)); + serviceCollection.AddSingleton<ISearchEngine, SearchEngine>(); - CertificateInfo = GetCertificateInfo(true); - Certificate = GetCertificate(CertificateInfo); + serviceCollection.AddSingleton<ServiceController>(); + serviceCollection.AddSingleton<IHttpListener, WebSocketSharpListener>(); + serviceCollection.AddSingleton<IHttpServer, HttpListenerHost>(); - HttpServer = new HttpListenerHost( - this, - LoggerFactory.CreateLogger<HttpListenerHost>(), - ServerConfigurationManager, - _configuration, - NetworkManager, - JsonSerializer, - XmlSerializer, - LoggerFactory) - { - GlobalResponse = LocalizationManager.GetLocalizedString("StartupEmbyServerIsLoading") - }; + serviceCollection.AddSingleton<IImageProcessor, ImageProcessor>(); - serviceCollection.AddSingleton(HttpServer); + serviceCollection.AddSingleton<ITVSeriesManager, TVSeriesManager>(); - ImageProcessor = new ImageProcessor(LoggerFactory.CreateLogger<ImageProcessor>(), ServerConfigurationManager.ApplicationPaths, FileSystemManager, ImageEncoder, () => LibraryManager, () => MediaEncoder); - serviceCollection.AddSingleton(ImageProcessor); + serviceCollection.AddSingleton<IDeviceManager, DeviceManager>(); - TVSeriesManager = new TVSeriesManager(UserManager, UserDataManager, LibraryManager, ServerConfigurationManager); - serviceCollection.AddSingleton(TVSeriesManager); + serviceCollection.AddSingleton<IMediaSourceManager, MediaSourceManager>(); - DeviceManager = new DeviceManager(AuthenticationRepository, JsonSerializer, LibraryManager, LocalizationManager, UserManager, FileSystemManager, LibraryMonitor, ServerConfigurationManager); - serviceCollection.AddSingleton(DeviceManager); + serviceCollection.AddSingleton<ISubtitleManager, SubtitleManager>(); - MediaSourceManager = new MediaSourceManager(ItemRepository, ApplicationPaths, LocalizationManager, UserManager, LibraryManager, LoggerFactory, JsonSerializer, FileSystemManager, UserDataManager, () => MediaEncoder); - serviceCollection.AddSingleton(MediaSourceManager); + serviceCollection.AddSingleton<IProviderManager, ProviderManager>(); - SubtitleManager = new SubtitleManager(LoggerFactory, FileSystemManager, LibraryMonitor, MediaSourceManager, LocalizationManager); - serviceCollection.AddSingleton(SubtitleManager); + // TODO: Refactor to eliminate the circular dependency here so that Lazy<T> isn't required + serviceCollection.AddTransient(provider => new Lazy<ILiveTvManager>(provider.GetRequiredService<ILiveTvManager>)); + serviceCollection.AddSingleton<IDtoService, DtoService>(); - ProviderManager = new ProviderManager(HttpClient, SubtitleManager, ServerConfigurationManager, LibraryMonitor, LoggerFactory, FileSystemManager, ApplicationPaths, () => LibraryManager, JsonSerializer); - serviceCollection.AddSingleton(ProviderManager); + serviceCollection.AddSingleton<IChannelManager, ChannelManager>(); - DtoService = new DtoService(LoggerFactory, LibraryManager, UserDataManager, ItemRepository, ImageProcessor, ProviderManager, this, () => MediaSourceManager, () => LiveTvManager); - serviceCollection.AddSingleton(DtoService); + serviceCollection.AddSingleton<ISessionManager, SessionManager>(); - ChannelManager = new ChannelManager(UserManager, DtoService, LibraryManager, LoggerFactory, ServerConfigurationManager, FileSystemManager, UserDataManager, JsonSerializer, ProviderManager); - serviceCollection.AddSingleton(ChannelManager); + serviceCollection.AddSingleton<IDlnaManager, DlnaManager>(); - SessionManager = new SessionManager(UserDataManager, LoggerFactory, LibraryManager, UserManager, musicManager, DtoService, ImageProcessor, this, AuthenticationRepository, DeviceManager, MediaSourceManager); - serviceCollection.AddSingleton(SessionManager); + serviceCollection.AddSingleton<ICollectionManager, CollectionManager>(); - serviceCollection.AddSingleton<IDlnaManager>( - new DlnaManager(XmlSerializer, FileSystemManager, ApplicationPaths, LoggerFactory, JsonSerializer, this)); + serviceCollection.AddSingleton<IPlaylistManager, PlaylistManager>(); - CollectionManager = new CollectionManager(LibraryManager, ApplicationPaths, LocalizationManager, FileSystemManager, LibraryMonitor, LoggerFactory, ProviderManager); - serviceCollection.AddSingleton(CollectionManager); + serviceCollection.AddSingleton<LiveTvDtoService>(); + serviceCollection.AddSingleton<ILiveTvManager, LiveTvManager>(); - serviceCollection.AddSingleton(typeof(IPlaylistManager), typeof(PlaylistManager)); + serviceCollection.AddSingleton<IUserViewManager, UserViewManager>(); - LiveTvManager = new LiveTvManager(this, ServerConfigurationManager, LoggerFactory, ItemRepository, ImageProcessor, UserDataManager, DtoService, UserManager, LibraryManager, TaskManager, LocalizationManager, JsonSerializer, FileSystemManager, () => ChannelManager); - serviceCollection.AddSingleton(LiveTvManager); + serviceCollection.AddSingleton<INotificationManager, NotificationManager>(); - UserViewManager = new UserViewManager(LibraryManager, LocalizationManager, UserManager, ChannelManager, LiveTvManager, ServerConfigurationManager); - serviceCollection.AddSingleton(UserViewManager); + serviceCollection.AddSingleton<IDeviceDiscovery, DeviceDiscovery>(); - NotificationManager = new NotificationManager(LoggerFactory, UserManager, ServerConfigurationManager); - serviceCollection.AddSingleton(NotificationManager); + serviceCollection.AddSingleton<IChapterManager, ChapterManager>(); - serviceCollection.AddSingleton<IDeviceDiscovery>(new DeviceDiscovery(ServerConfigurationManager)); + serviceCollection.AddSingleton<IEncodingManager, MediaEncoder.EncodingManager>(); - ChapterManager = new ChapterManager(LibraryManager, LoggerFactory, ServerConfigurationManager, ItemRepository); - serviceCollection.AddSingleton(ChapterManager); + serviceCollection.AddSingleton<IActivityRepository, ActivityRepository>(); + serviceCollection.AddSingleton<IActivityManager, ActivityManager>(); - MediaEncoder = new MediaBrowser.MediaEncoding.Encoder.MediaEncoder( - LoggerFactory.CreateLogger<MediaBrowser.MediaEncoding.Encoder.MediaEncoder>(), - ServerConfigurationManager, - FileSystemManager, - ProcessFactory, - LocalizationManager, - () => SubtitleEncoder, - _configuration, - StartupOptions.FFmpegPath); - serviceCollection.AddSingleton(MediaEncoder); + serviceCollection.AddSingleton<IAuthorizationContext, AuthorizationContext>(); + serviceCollection.AddSingleton<ISessionContext, SessionContext>(); - EncodingManager = new MediaEncoder.EncodingManager(FileSystemManager, LoggerFactory, MediaEncoder, ChapterManager, LibraryManager); - serviceCollection.AddSingleton(EncodingManager); + serviceCollection.AddSingleton<IAuthService, AuthService>(); - var activityLogRepo = GetActivityLogRepository(); - serviceCollection.AddSingleton(activityLogRepo); - serviceCollection.AddSingleton<IActivityManager>(new ActivityManager(LoggerFactory, activityLogRepo, UserManager)); + serviceCollection.AddSingleton<ISubtitleEncoder, MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder>(); - var authContext = new AuthorizationContext(AuthenticationRepository, UserManager); - serviceCollection.AddSingleton<IAuthorizationContext>(authContext); - serviceCollection.AddSingleton<ISessionContext>(new SessionContext(UserManager, authContext, SessionManager)); - - AuthService = new AuthService(LoggerFactory.CreateLogger<AuthService>(), authContext, ServerConfigurationManager, SessionManager, NetworkManager); - serviceCollection.AddSingleton(AuthService); - - SubtitleEncoder = new MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder( - LibraryManager, - LoggerFactory.CreateLogger<MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder>(), - ApplicationPaths, - FileSystemManager, - MediaEncoder, - HttpClient, - MediaSourceManager, - ProcessFactory); - serviceCollection.AddSingleton(SubtitleEncoder); - - serviceCollection.AddSingleton(typeof(IResourceFileManager), typeof(ResourceFileManager)); + serviceCollection.AddSingleton<IResourceFileManager, ResourceFileManager>(); serviceCollection.AddSingleton<EncodingHelper>(); - serviceCollection.AddSingleton(typeof(IAttachmentExtractor), typeof(MediaBrowser.MediaEncoding.Attachments.AttachmentExtractor)); + serviceCollection.AddSingleton<IAttachmentExtractor, MediaBrowser.MediaEncoding.Attachments.AttachmentExtractor>(); + } + + /// <summary> + /// Create services registered with the service container that need to be initialized at application startup. + /// </summary> + /// <returns>A task representing the service initialization operation.</returns> + public async Task InitializeServices() + { + var localizationManager = (LocalizationManager)Resolve<ILocalizationManager>(); + await localizationManager.LoadAll().ConfigureAwait(false); - _displayPreferencesRepository.Initialize(); + _mediaEncoder = Resolve<IMediaEncoder>(); + _sessionManager = Resolve<ISessionManager>(); + _httpServer = Resolve<IHttpServer>(); + _httpClient = Resolve<IHttpClient>(); - var userDataRepo = new SqliteUserDataRepository(LoggerFactory.CreateLogger<SqliteUserDataRepository>(), ApplicationPaths); + ((SqliteDisplayPreferencesRepository)Resolve<IDisplayPreferencesRepository>()).Initialize(); + ((AuthenticationRepository)Resolve<IAuthenticationRepository>()).Initialize(); + ((SqliteUserRepository)Resolve<IUserRepository>()).Initialize(); + ((ActivityRepository)Resolve<IActivityRepository>()).Initialize(); SetStaticProperties(); - ((UserManager)UserManager).Initialize(); + var userManager = (UserManager)Resolve<IUserManager>(); + userManager.Initialize(); - ((UserDataManager)UserDataManager).Repository = userDataRepo; - ItemRepository.Initialize(userDataRepo, UserManager); - ((LibraryManager)LibraryManager).ItemRepository = ItemRepository; + var userDataRepo = (SqliteUserDataRepository)Resolve<IUserDataRepository>(); + ((SqliteItemRepository)Resolve<IItemRepository>()).Initialize(userDataRepo, userManager); + + FindParts(); } public static void LogEnvironmentInfo(ILogger logger, IApplicationPaths appPaths) @@ -868,6 +692,18 @@ namespace Emby.Server.Implementations .GetCommandLineArgs() .Distinct(); + // Get all relevant environment variables + var allEnvVars = Environment.GetEnvironmentVariables(); + var relevantEnvVars = new Dictionary<object, object>(); + foreach (var key in allEnvVars.Keys) + { + if (_relevantEnvVarPrefixes.Any(prefix => key.ToString().StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) + { + relevantEnvVars.Add(key, allEnvVars[key]); + } + } + + logger.LogInformation("Environment Variables: {EnvVars}", relevantEnvVars); logger.LogInformation("Arguments: {Args}", commandLineArgs); logger.LogInformation("Operating system: {OS}", OperatingSystem.Name); logger.LogInformation("Architecture: {Architecture}", RuntimeInformation.OSArchitecture); @@ -916,110 +752,37 @@ namespace Emby.Server.Implementations } /// <summary> - /// Gets the user repository. - /// </summary> - /// <returns><see cref="Task{SqliteUserRepository}" />.</returns> - private SqliteUserRepository GetUserRepository() - { - var repo = new SqliteUserRepository( - LoggerFactory.CreateLogger<SqliteUserRepository>(), - ApplicationPaths); - - repo.Initialize(); - - return repo; - } - - private IAuthenticationRepository GetAuthenticationRepository() - { - var repo = new AuthenticationRepository(LoggerFactory, ServerConfigurationManager); - - repo.Initialize(); - - return repo; - } - - private IActivityRepository GetActivityLogRepository() - { - var repo = new ActivityRepository(LoggerFactory, ServerConfigurationManager.ApplicationPaths, FileSystemManager); - - repo.Initialize(); - - return repo; - } - - /// <summary> /// Dirty hacks. /// </summary> private void SetStaticProperties() { - ItemRepository.ImageProcessor = ImageProcessor; - // For now there's no real way to inject these properly - BaseItem.Logger = LoggerFactory.CreateLogger("BaseItem"); + BaseItem.Logger = Resolve<ILogger<BaseItem>>(); BaseItem.ConfigurationManager = ServerConfigurationManager; - BaseItem.LibraryManager = LibraryManager; - BaseItem.ProviderManager = ProviderManager; - BaseItem.LocalizationManager = LocalizationManager; - BaseItem.ItemRepository = ItemRepository; - User.UserManager = UserManager; - BaseItem.FileSystem = FileSystemManager; - BaseItem.UserDataManager = UserDataManager; - BaseItem.ChannelManager = ChannelManager; - Video.LiveTvManager = LiveTvManager; - Folder.UserViewManager = UserViewManager; - UserView.TVSeriesManager = TVSeriesManager; - UserView.CollectionManager = CollectionManager; - BaseItem.MediaSourceManager = MediaSourceManager; - CollectionFolder.XmlSerializer = XmlSerializer; - CollectionFolder.JsonSerializer = JsonSerializer; + BaseItem.LibraryManager = Resolve<ILibraryManager>(); + BaseItem.ProviderManager = Resolve<IProviderManager>(); + BaseItem.LocalizationManager = Resolve<ILocalizationManager>(); + BaseItem.ItemRepository = Resolve<IItemRepository>(); + User.UserManager = Resolve<IUserManager>(); + BaseItem.FileSystem = _fileSystemManager; + BaseItem.UserDataManager = Resolve<IUserDataManager>(); + BaseItem.ChannelManager = Resolve<IChannelManager>(); + Video.LiveTvManager = Resolve<ILiveTvManager>(); + Folder.UserViewManager = Resolve<IUserViewManager>(); + UserView.TVSeriesManager = Resolve<ITVSeriesManager>(); + UserView.CollectionManager = Resolve<ICollectionManager>(); + BaseItem.MediaSourceManager = Resolve<IMediaSourceManager>(); + CollectionFolder.XmlSerializer = _xmlSerializer; + CollectionFolder.JsonSerializer = Resolve<IJsonSerializer>(); CollectionFolder.ApplicationHost = this; - AuthenticatedAttribute.AuthService = AuthService; - } - - private async void PluginInstalled(object sender, GenericEventArgs<PackageVersionInfo> args) - { - string dir = Path.Combine(ApplicationPaths.PluginsPath, args.Argument.name); - var types = Directory.EnumerateFiles(dir, "*.dll", SearchOption.AllDirectories) - .Select(x => Assembly.LoadFrom(x)) - .SelectMany(x => x.ExportedTypes) - .Where(x => x.IsClass && !x.IsAbstract && !x.IsInterface && !x.IsGenericType) - .ToArray(); - - int oldLen = _allConcreteTypes.Length; - Array.Resize(ref _allConcreteTypes, oldLen + types.Length); - types.CopyTo(_allConcreteTypes, oldLen); - - var plugins = types.Where(x => x.IsAssignableFrom(typeof(IPlugin))) - .Select(CreateInstanceSafe) - .Where(x => x != null) - .Cast<IPlugin>() - .Select(LoadPlugin) - .Where(x => x != null) - .ToArray(); - - oldLen = _plugins.Length; - Array.Resize(ref _plugins, oldLen + plugins.Length); - plugins.CopyTo(_plugins, oldLen); - - var entries = types.Where(x => x.IsAssignableFrom(typeof(IServerEntryPoint))) - .Select(CreateInstanceSafe) - .Where(x => x != null) - .Cast<IServerEntryPoint>() - .ToList(); - - await Task.WhenAll(StartEntryPoints(entries, true)).ConfigureAwait(false); - await Task.WhenAll(StartEntryPoints(entries, false)).ConfigureAwait(false); + AuthenticatedAttribute.AuthService = Resolve<IAuthService>(); } /// <summary> - /// Finds the parts. + /// Finds plugin components and register them with the appropriate services. /// </summary> - public void FindParts() + private void FindParts() { - InstallationManager = ServiceProvider.GetService<IInstallationManager>(); - InstallationManager.PluginInstalled += PluginInstalled; - if (!ServerConfigurationManager.Configuration.IsPortAuthorized) { ServerConfigurationManager.Configuration.IsPortAuthorized = true; @@ -1032,36 +795,34 @@ namespace Emby.Server.Implementations .Where(i => i != null) .ToArray(); - HttpServer.Init(GetExports<IService>(false), GetExports<IWebSocketListener>(), GetUrlPrefixes()); + _httpServer.Init(GetExportTypes<IService>(), GetExports<IWebSocketListener>(), GetUrlPrefixes()); - LibraryManager.AddParts( + Resolve<ILibraryManager>().AddParts( GetExports<IResolverIgnoreRule>(), GetExports<IItemResolver>(), GetExports<IIntroProvider>(), GetExports<IBaseItemComparer>(), GetExports<ILibraryPostScanTask>()); - ProviderManager.AddParts( + Resolve<IProviderManager>().AddParts( GetExports<IImageProvider>(), GetExports<IMetadataService>(), GetExports<IMetadataProvider>(), GetExports<IMetadataSaver>(), GetExports<IExternalId>()); - ImageProcessor.ImageEnhancers = GetExports<IImageEnhancer>(); - - LiveTvManager.AddParts(GetExports<ILiveTvService>(), GetExports<ITunerHost>(), GetExports<IListingsProvider>()); + Resolve<ILiveTvManager>().AddParts(GetExports<ILiveTvService>(), GetExports<ITunerHost>(), GetExports<IListingsProvider>()); - SubtitleManager.AddParts(GetExports<ISubtitleProvider>()); + Resolve<ISubtitleManager>().AddParts(GetExports<ISubtitleProvider>()); - ChannelManager.AddParts(GetExports<IChannel>()); + Resolve<IChannelManager>().AddParts(GetExports<IChannel>()); - MediaSourceManager.AddParts(GetExports<IMediaSourceProvider>()); + Resolve<IMediaSourceManager>().AddParts(GetExports<IMediaSourceProvider>()); - NotificationManager.AddParts(GetExports<INotificationService>(), GetExports<INotificationTypeFactory>()); - UserManager.AddParts(GetExports<IAuthenticationProvider>(), GetExports<IPasswordResetProvider>()); + Resolve<INotificationManager>().AddParts(GetExports<INotificationService>(), GetExports<INotificationTypeFactory>()); + Resolve<IUserManager>().AddParts(GetExports<IAuthenticationProvider>(), GetExports<IPasswordResetProvider>()); - IsoManager.AddParts(GetExports<IIsoMounter>()); + Resolve<IIsoManager>().AddParts(GetExports<IIsoMounter>()); } private IPlugin LoadPlugin(IPlugin plugin) @@ -1128,7 +889,7 @@ namespace Emby.Server.Implementations { exportedTypes = ass.GetExportedTypes(); } - catch (TypeLoadException ex) + catch (FileNotFoundException ex) { Logger.LogError(ex, "Error getting exported types from {Assembly}", ass.FullName); continue; @@ -1168,16 +929,6 @@ namespace Emby.Server.Implementations }); } - private CertificateInfo GetCertificateInfo(bool generateCertificate) - { - // Custom cert - return new CertificateInfo - { - Path = ServerConfigurationManager.Configuration.CertificatePath, - Password = ServerConfigurationManager.Configuration.CertificatePassword - }; - } - /// <summary> /// Called when [configuration updated]. /// </summary> @@ -1204,14 +955,13 @@ namespace Emby.Server.Implementations } } - if (!HttpServer.UrlPrefixes.SequenceEqual(GetUrlPrefixes(), StringComparer.OrdinalIgnoreCase)) + if (!_httpServer.UrlPrefixes.SequenceEqual(GetUrlPrefixes(), StringComparer.OrdinalIgnoreCase)) { requiresRestart = true; } var currentCertPath = CertificateInfo?.Path; - var newCertInfo = GetCertificateInfo(false); - var newCertPath = newCertInfo?.Path; + var newCertPath = ServerConfigurationManager.Configuration.CertificatePath; if (!string.Equals(currentCertPath, newCertPath, StringComparison.OrdinalIgnoreCase)) { @@ -1264,7 +1014,7 @@ namespace Emby.Server.Implementations { try { - await SessionManager.SendServerRestartNotification(CancellationToken.None).ConfigureAwait(false); + await _sessionManager.SendServerRestartNotification(CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { @@ -1368,7 +1118,7 @@ namespace Emby.Server.Implementations IsShuttingDown = IsShuttingDown, Version = ApplicationVersionString, WebSocketPortNumber = HttpPort, - CompletedInstallations = InstallationManager.CompletedInstallations.ToArray(), + CompletedInstallations = Resolve<IInstallationManager>().CompletedInstallations.ToArray(), Id = SystemId, ProgramDataPath = ApplicationPaths.ProgramDataPath, WebPath = ApplicationPaths.WebPath, @@ -1388,15 +1138,14 @@ namespace Emby.Server.Implementations ServerName = FriendlyName, LocalAddress = localAddress, SupportsLibraryMonitor = true, - EncoderLocation = MediaEncoder.EncoderLocation, + EncoderLocation = _mediaEncoder.EncoderLocation, SystemArchitecture = RuntimeInformation.OSArchitecture, - SystemUpdateLevel = SystemUpdateLevel, - PackageName = StartupOptions.PackageName + PackageName = _startupOptions.PackageName }; } public IEnumerable<WakeOnLanInfo> GetWakeOnLanInfo() - => NetworkManager.GetMacAddresses() + => _networkManager.GetMacAddresses() .Select(i => new WakeOnLanInfo(i)) .ToList(); @@ -1419,7 +1168,7 @@ namespace Emby.Server.Implementations public bool SupportsHttps => Certificate != null || ServerConfigurationManager.Configuration.IsBehindProxy; - public async Task<string> GetLocalApiUrl(CancellationToken cancellationToken) + public async Task<string> GetLocalApiUrl(CancellationToken cancellationToken, bool forceHttp = false) { try { @@ -1428,7 +1177,7 @@ namespace Emby.Server.Implementations foreach (var address in addresses) { - return GetLocalApiUrl(address); + return GetLocalApiUrl(address, forceHttp); } return null; @@ -1458,7 +1207,7 @@ namespace Emby.Server.Implementations } /// <inheritdoc /> - public string GetLocalApiUrl(IPAddress ipAddress) + public string GetLocalApiUrl(IPAddress ipAddress, bool forceHttp = false) { if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) { @@ -1468,33 +1217,26 @@ namespace Emby.Server.Implementations str.CopyTo(span.Slice(1)); span[^1] = ']'; - return GetLocalApiUrl(span); + return GetLocalApiUrl(span, forceHttp); } - return GetLocalApiUrl(ipAddress.ToString()); + return GetLocalApiUrl(ipAddress.ToString(), forceHttp); } /// <inheritdoc /> - public string GetLocalApiUrl(ReadOnlySpan<char> host) + public string GetLocalApiUrl(ReadOnlySpan<char> host, bool forceHttp = false) { var url = new StringBuilder(64); - if (EnableHttps) - { - url.Append("https://"); - } - else - { - url.Append("http://"); - } - - url.Append(host) + bool useHttps = EnableHttps && !forceHttp; + url.Append(useHttps ? "https://" : "http://") + .Append(host) .Append(':') - .Append(HttpPort); + .Append(useHttps ? HttpsPort : HttpPort); string baseUrl = ServerConfigurationManager.Configuration.BaseUrl; if (baseUrl.Length != 0) { - url.Append('/').Append(baseUrl); + url.Append(baseUrl); } return url.ToString(); @@ -1516,7 +1258,7 @@ namespace Emby.Server.Implementations if (addresses.Count == 0) { - addresses.AddRange(NetworkManager.GetLocalIpAddresses(ServerConfigurationManager.Configuration.IgnoreVirtualInterfaces)); + addresses.AddRange(_networkManager.GetLocalIpAddresses(ServerConfigurationManager.Configuration.IgnoreVirtualInterfaces)); } var resultList = new List<IPAddress>(); @@ -1583,7 +1325,7 @@ namespace Emby.Server.Implementations try { - using (var response = await HttpClient.SendAsync( + using (var response = await _httpClient.SendAsync( new HttpRequestOptions { Url = apiUrl, @@ -1636,7 +1378,7 @@ namespace Emby.Server.Implementations try { - await SessionManager.SendServerShutdownNotification(CancellationToken.None).ConfigureAwait(false); + await _sessionManager.SendServerShutdownNotification(CancellationToken.None).ConfigureAwait(false); } catch (Exception ex) { @@ -1679,29 +1421,6 @@ namespace Emby.Server.Implementations _plugins = list.ToArray(); } - /// <summary> - /// This returns localhost in the case of no external dns, and the hostname if the - /// dns is prefixed with a valid Uri prefix. - /// </summary> - /// <param name="externalDns">The external dns prefix to get the hostname of.</param> - /// <returns>The hostname in <paramref name="externalDns"/>.</returns> - private static string GetHostnameFromExternalDns(string externalDns) - { - if (string.IsNullOrEmpty(externalDns)) - { - return "localhost"; - } - - try - { - return new Uri(externalDns).Host; - } - catch - { - return externalDns; - } - } - public virtual void LaunchUrl(string url) { if (!CanLaunchWebBrowser) @@ -1709,15 +1428,17 @@ namespace Emby.Server.Implementations throw new NotSupportedException(); } - var process = ProcessFactory.Create(new ProcessOptions + var process = new Process { - FileName = url, - EnableRaisingEvents = true, - UseShellExecute = true, - ErrorDialog = false - }); - - process.Exited += ProcessExited; + StartInfo = new ProcessStartInfo + { + FileName = url, + UseShellExecute = true, + ErrorDialog = false + }, + EnableRaisingEvents = true + }; + process.Exited += (sender, args) => ((Process)sender).Dispose(); try { @@ -1730,11 +1451,6 @@ namespace Emby.Server.Implementations } } - private static void ProcessExited(object sender, EventArgs e) - { - ((IProcess)sender).Dispose(); - } - public virtual void EnableLoopback(string appName) { } @@ -1783,14 +1499,8 @@ namespace Emby.Server.Implementations Logger.LogError(ex, "Error disposing {Type}", part.GetType().Name); } } - - _userRepository?.Dispose(); - _displayPreferencesRepository.Dispose(); } - _userRepository = null; - _displayPreferencesRepository = null; - _disposed = true; } } |
