update .net core startup

This commit is contained in:
Luke Pulverenti
2016-11-12 23:33:51 -05:00
parent 3e06bda46b
commit 95341c5c96
20 changed files with 602 additions and 632 deletions

View File

@@ -37,7 +37,7 @@ namespace MediaBrowser.ServerApplication
private static ILogger _logger;
private static bool _isRunningAsService = false;
public static bool IsRunningAsService = false;
private static bool _canRestartService = false;
private static bool _appHostDisposed;
@@ -72,9 +72,9 @@ namespace MediaBrowser.ServerApplication
public static void Main()
{
var options = new StartupOptions();
_isRunningAsService = options.ContainsOption("-service");
IsRunningAsService = options.ContainsOption("-service");
if (_isRunningAsService)
if (IsRunningAsService)
{
//_canRestartService = CanRestartWindowsService();
}
@@ -88,7 +88,7 @@ namespace MediaBrowser.ServerApplication
var success = SetDllDirectory(architecturePath);
var appPaths = CreateApplicationPaths(applicationPath, _isRunningAsService);
var appPaths = CreateApplicationPaths(applicationPath, IsRunningAsService);
var logManager = new NlogManager(appPaths.LogDirectoryPath, "server");
logManager.ReloadLogger(LogSeverity.Debug);
@@ -148,7 +148,7 @@ namespace MediaBrowser.ServerApplication
try
{
RunApplication(appPaths, logManager, _isRunningAsService, options);
RunApplication(appPaths, logManager, IsRunningAsService, options);
}
finally
{
@@ -204,7 +204,7 @@ namespace MediaBrowser.ServerApplication
}
}
if (!_isRunningAsService)
if (!IsRunningAsService)
{
return IsAlreadyRunningAsService(applicationPath);
}
@@ -272,7 +272,7 @@ namespace MediaBrowser.ServerApplication
{
get
{
if (_isRunningAsService)
if (IsRunningAsService)
{
return _canRestartService;
}
@@ -295,7 +295,7 @@ namespace MediaBrowser.ServerApplication
return false;
#endif
if (_isRunningAsService)
if (IsRunningAsService)
{
return _canRestartService;
}
@@ -317,22 +317,16 @@ namespace MediaBrowser.ServerApplication
/// <param name="options">The options.</param>
private static void RunApplication(ServerApplicationPaths appPaths, ILogManager logManager, bool runService, StartupOptions options)
{
var fileSystem = new WindowsFileSystem(logManager.GetLogger("FileSystem"));
var fileSystem = new ManagedFileSystem(logManager.GetLogger("FileSystem"), true, true, true);
fileSystem.AddShortcutHandler(new LnkShortcutHandler());
fileSystem.AddShortcutHandler(new MbLinkShortcutHandler(fileSystem));
var nativeApp = new WindowsApp(fileSystem, _logger)
{
IsRunningAsService = runService
};
var imageEncoder = ImageEncoderHelper.GetImageEncoder(_logger, logManager, fileSystem, options, () => _appHost.HttpClient, appPaths);
_appHost = new ApplicationHost(appPaths,
_appHost = new WindowsAppHost(appPaths,
logManager,
options,
fileSystem,
nativeApp,
new PowerManagement(),
"emby.windows.zip",
new EnvironmentInfo(),
@@ -440,7 +434,7 @@ namespace MediaBrowser.ServerApplication
public static void Invoke(Action action)
{
if (_isRunningAsService)
if (IsRunningAsService)
{
action();
}
@@ -578,7 +572,7 @@ namespace MediaBrowser.ServerApplication
/// <param name="e">The <see cref="SessionEndingEventArgs"/> instance containing the event data.</param>
static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if (e.Reason == SessionEndReasons.SystemShutdown || !_isRunningAsService)
if (e.Reason == SessionEndReasons.SystemShutdown || !IsRunningAsService)
{
Shutdown();
}
@@ -595,7 +589,7 @@ namespace MediaBrowser.ServerApplication
new UnhandledExceptionWriter(_appHost.ServerConfigurationManager.ApplicationPaths, _logger, _appHost.LogManager).Log(exception);
if (!_isRunningAsService)
if (!IsRunningAsService)
{
MessageBox.Show("Unhandled exception: " + exception.Message);
}
@@ -623,7 +617,7 @@ namespace MediaBrowser.ServerApplication
// Update is there - execute update
try
{
var serviceName = _isRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty;
var serviceName = IsRunningAsService ? BackgroundService.GetExistingServiceName() : string.Empty;
new ApplicationUpdater().UpdateApplication(appPaths, updateArchive, logger, serviceName);
// And just let the app exit so it can update
@@ -642,7 +636,7 @@ namespace MediaBrowser.ServerApplication
public static void Shutdown()
{
if (_isRunningAsService)
if (IsRunningAsService)
{
ShutdownWindowsService();
}
@@ -658,7 +652,7 @@ namespace MediaBrowser.ServerApplication
{
DisposeAppHost();
if (_isRunningAsService)
if (IsRunningAsService)
{
RestartWindowsService();
}

View File

@@ -138,7 +138,6 @@
<Compile Include="Native\PowerManagement.cs" />
<Compile Include="Native\Standby.cs" />
<Compile Include="Native\ServerAuthorization.cs" />
<Compile Include="Native\WindowsApp.cs" />
<Compile Include="Networking\NativeMethods.cs" />
<Compile Include="Networking\NetworkManager.cs" />
<Compile Include="Networking\NetworkShares.cs" />
@@ -156,6 +155,7 @@
<DependentUpon>SplashForm.cs</DependentUpon>
</Compile>
<Compile Include="Updates\ApplicationUpdater.cs" />
<Compile Include="WindowsAppHost.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View File

@@ -1,131 +1,31 @@
using System;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Server.Startup.Common;
using MediaBrowser.ServerApplication.Networking;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Emby.Server.Core;
using Emby.Server.Core.Data;
using Emby.Server.Core.FFMpeg;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.System;
using MediaBrowser.ServerApplication.Native;
namespace MediaBrowser.ServerApplication.Native
namespace MediaBrowser.ServerApplication
{
public class WindowsApp : INativeApp
public class WindowsAppHost : ApplicationHost
{
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
public WindowsApp(IFileSystem fileSystem, ILogger logger)
public WindowsAppHost(ServerApplicationPaths applicationPaths, ILogManager logManager, StartupOptions options, IFileSystem fileSystem, IPowerManagement powerManagement, string releaseAssetFilename, IEnvironmentInfo environmentInfo, MediaBrowser.Controller.Drawing.IImageEncoder imageEncoder, ISystemEvents systemEvents, IMemoryStreamFactory memoryStreamFactory, MediaBrowser.Common.Net.INetworkManager networkManager, Action<string, string> certificateGenerator, Func<string> defaultUsernameFactory)
: base(applicationPaths, logManager, options, fileSystem, powerManagement, releaseAssetFilename, environmentInfo, imageEncoder, systemEvents, memoryStreamFactory, networkManager, certificateGenerator, defaultUsernameFactory)
{
_fileSystem = fileSystem;
_logger = logger;
}
public List<Assembly> GetAssembliesWithParts()
public override bool IsRunningAsService
{
var list = new List<Assembly>();
if (!System.Environment.Is64BitProcess)
{
//list.Add(typeof(PismoIsoManager).Assembly);
}
list.Add(GetType().Assembly);
return list;
get { return MainStartup.IsRunningAsService; }
}
public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
{
ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory);
}
public bool SupportsLibraryMonitor
{
get { return true; }
}
public bool SupportsRunningAsService
{
get
{
return true;
}
}
public bool IsRunningAsService
{
get;
set;
}
public bool CanSelfRestart
{
get
{
return MainStartup.CanSelfRestart;
}
}
public bool SupportsAutoRunAtStartup
{
get
{
return true;
}
}
public bool CanSelfUpdate
{
get
{
return MainStartup.CanSelfUpdate;
}
}
public void Shutdown()
{
MainStartup.Shutdown();
}
public void Restart(StartupOptions startupOptions)
{
MainStartup.Restart();
}
public void ConfigureAutoRun(bool autorun)
{
var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
if (autorun)
{
//Copy our shortut into the startup folder for this user
var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
_fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath));
File.Copy(shortcutPath, targetPath, true);
}
else
{
//Remove our shortcut from the startup folder for this user
_fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
}
}
public INetworkManager CreateNetworkManager(ILogger logger)
{
return new NetworkManager(logger);
}
public FFMpegInstallInfo GetFfmpegInstallInfo()
protected override FFMpegInstallInfo GetFfmpegInstallInfo()
{
var info = new FFMpegInstallInfo();
@@ -136,7 +36,61 @@ namespace MediaBrowser.ServerApplication.Native
return info;
}
public void LaunchUrl(string url)
protected override void RestartInternal()
{
MainStartup.Restart();
}
protected override List<Assembly> GetAssembliesWithPartsInternal()
{
var list = new List<Assembly>();
if (!Environment.Is64BitProcess)
{
//list.Add(typeof(PismoIsoManager).Assembly);
}
list.Add(GetType().Assembly);
return list;
}
protected override void ShutdownInternal()
{
MainStartup.Shutdown();
}
protected override void AuthorizeServer(int udpPort, int httpServerPort, int httpsServerPort, string applicationPath, string tempDirectory)
{
ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsServerPort, applicationPath, tempDirectory);
}
protected override IDbConnector GetDbConnector()
{
return new DbConnector(Logger);
}
protected override void ConfigureAutoRunInternal(bool autorun)
{
var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
if (autorun)
{
//Copy our shortut into the startup folder for this user
var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
FileSystemManager.CreateDirectory(Path.GetDirectoryName(targetPath));
File.Copy(shortcutPath, targetPath, true);
}
else
{
//Remove our shortcut from the startup folder for this user
FileSystemManager.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
}
}
public override void LaunchUrl(string url)
{
var process = new Process
{
@@ -156,32 +110,54 @@ namespace MediaBrowser.ServerApplication.Native
}
catch (Exception ex)
{
_logger.ErrorException("Error launching url: {0}", ex, url);
Logger.ErrorException("Error launching url: {0}", ex, url);
throw;
}
}
public IDbConnector GetDbConnector()
{
return new DbConnector(_logger);
}
/// <summary>
/// Processes the exited.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private static void ProcessExited(object sender, EventArgs e)
{
((Process)sender).Dispose();
}
public void EnableLoopback(string appName)
protected override void EnableLoopbackInternal(string appName)
{
LoopUtil.Run(appName);
}
public override bool SupportsRunningAsService
{
get
{
return true;
}
}
public override bool CanSelfRestart
{
get
{
return MainStartup.CanSelfRestart;
}
}
public override bool SupportsAutoRunAtStartup
{
get
{
return true;
}
}
public override bool CanSelfUpdate
{
get
{
return MainStartup.CanSelfUpdate;
}
}
public bool PortsRequireAuthorization(string applicationPath)
{
var appNameSrch = Path.GetFileName(applicationPath);
@@ -209,7 +185,7 @@ namespace MediaBrowser.ServerApplication.Native
if (data.IndexOf("Block", StringComparison.OrdinalIgnoreCase) != -1)
{
_logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
Logger.Info("Found potential windows firewall rule blocking Emby Server: " + data);
}
//var parts = data.Split('\n');
@@ -220,7 +196,7 @@ namespace MediaBrowser.ServerApplication.Native
}
catch (Exception ex)
{
_logger.ErrorException("Error querying windows firewall", ex);
Logger.ErrorException("Error querying windows firewall", ex);
// Hate having to do this
try
@@ -229,12 +205,13 @@ namespace MediaBrowser.ServerApplication.Native
}
catch (Exception ex1)
{
_logger.ErrorException("Error killing process", ex1);
Logger.ErrorException("Error killing process", ex1);
}
throw;
}
}
}
}
}
}