From e84832f9eea53a9672e3f62e9db9a4fa53b4dfe4 Mon Sep 17 00:00:00 2001 From: Lucio Maranta Date: Thu, 21 Dec 2017 17:01:14 +0100 Subject: [PATCH] * Fixed no website directory * Fixed message errros and startup orders --- Step.Tasks/ThreadsFunctions.cs | 17 ++++++++++++++--- Step.Utils/Constants.cs | 6 +++++- Step/App_Start/Startup.cs | 11 ++++++----- Step/program.cs | 25 ++++++++++++------------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Step.Tasks/ThreadsFunctions.cs b/Step.Tasks/ThreadsFunctions.cs index 45230beb..e3fb62fb 100644 --- a/Step.Tasks/ThreadsFunctions.cs +++ b/Step.Tasks/ThreadsFunctions.cs @@ -31,13 +31,16 @@ public static class ThreadsFunctions MessageServices.Current.Publish(SEND_ALARMS, null, alarms); } Thread.Sleep(200); - } } + catch (ThreadAbortException ex) + { + ncHandler.Dispose(); + } catch (Exception ex) { - ncHandler.Dispose(); - TryNcConnection(); + ncHandler.Dispose(); + TryNcConnection(); } } @@ -60,6 +63,10 @@ public static class ThreadsFunctions Thread.Sleep(1000); } } + catch (ThreadAbortException ex) + { + ncHandler.Dispose(); + } catch (Exception ex) { ncHandler.Dispose(); @@ -87,6 +94,10 @@ public static class ThreadsFunctions Thread.Sleep(200); } } + catch (ThreadAbortException ex) + { + ncHandler.Dispose(); + } catch (Exception ex) { ncHandler.Dispose(); diff --git a/Step.Utils/Constants.cs b/Step.Utils/Constants.cs index 0f45e967..b2ea08fc 100644 --- a/Step.Utils/Constants.cs +++ b/Step.Utils/Constants.cs @@ -1,4 +1,7 @@ -namespace Step.Utils +using System.IO; +using System.Reflection; + +namespace Step.Utils { public static class Constants { @@ -49,6 +52,7 @@ // Filenames public const string STARTUP_CONFIG_SCHEMA_PATH = "serverConfigValidator.xsd"; public const string STARTUP_CONFIG_PATH = "serverConfig.xml"; + public static string WEBSITE_DIRECTORY = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", "wwwroot"); // MVVM Messages names public const string STOP_SERVER = "STOP_SERVER"; diff --git a/Step/App_Start/Startup.cs b/Step/App_Start/Startup.cs index 08353758..980d0c76 100644 --- a/Step/App_Start/Startup.cs +++ b/Step/App_Start/Startup.cs @@ -3,8 +3,6 @@ using Microsoft.Owin; using Owin; using System.Web.Http; using Microsoft.Owin.StaticFiles; -using System.IO; -using System.Reflection; using Microsoft.Owin.FileSystems; using System.Configuration; using Microsoft.Owin.Security.OAuth; @@ -13,6 +11,8 @@ using Step.Provider; using Microsoft.AspNet.SignalR; using Microsoft.AspNet.SignalR.Hubs; using Step.Attributes; +using Step.Utils; +using System.IO; [assembly: OwinStartup(typeof(Step.App_Start.Startup))] @@ -39,16 +39,17 @@ namespace Step.App_Start // SignalR config & startup SignalRConfig(app); - var directoryBrowsing = ConfigurationManager.AppSettings["enableDirectoryBrowsing"] == "true"; + // Check if web site directory exists + if (!Directory.Exists(Constants.WEBSITE_DIRECTORY)) + ExceptionManager.Manage(Constants.ERROR_LEVEL.FATAL, "Main window directory not found!"); - string rootDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "..", "wwwroot"); var options = new FileServerOptions() { EnableDefaultFiles = !directoryBrowsing, EnableDirectoryBrowsing = directoryBrowsing, RequestPath = PathString.Empty, - FileSystem = new PhysicalFileSystem(rootDir) + FileSystem = new PhysicalFileSystem(Constants.WEBSITE_DIRECTORY) }; app.UseFileServer(options); diff --git a/Step/program.cs b/Step/program.cs index 7aab43f1..48e92738 100644 --- a/Step/program.cs +++ b/Step/program.cs @@ -30,33 +30,32 @@ namespace Step // Start self host application string configuredUri = "http://localhost:" + ServerConfig.ServerPort.ToString(); - // Start WinForm - ServerControlWindow.Start(); // Register listener to "close application" messages MessageServices.Current.Subscribe(STOP_SERVER, (a, b) => { StopRequest.Set(); - }); - // Start Threads - ThreadsHandler.Start(); - // Start listeners - ListenersHandler.Start(); + }); // Start server services using (WebApp.Start(url: configuredUri)) { + // Start WinForm + ServerControlWindow.Start(); + // Start Threads + ThreadsHandler.Start(); + // Start listeners + ListenersHandler.Start(); + // Wait interrupt from client StopRequest.WaitOne(); - - // Stop Threads - ThreadsHandler.Stop(); - // Stop messageservice listeners - ListenersHandler.Stop(); LogInfo("Application closed"); } - + // Stop Threads + ThreadsHandler.Stop(); + // Stop messageservice listeners + ListenersHandler.Stop(); // Close WinForm ServerControlWindow.Stop(); }