diff --git a/Forum.BrokerService/Bootstrap.cs b/Forum.BrokerService/Bootstrap.cs index 5b2a7ed..42ac017 100644 --- a/Forum.BrokerService/Bootstrap.cs +++ b/Forum.BrokerService/Bootstrap.cs @@ -1,82 +1,93 @@ -using System; -using System.Configuration; -using ECommon.Autofac; -using ECommon.Components; -using ECommon.Configurations; -using ECommon.JsonNet; -using ECommon.Log4Net; -using ECommon.Logging; -using EQueue.Broker; -using EQueue.Configurations; -using ECommonConfiguration = ECommon.Configurations.Configuration; - -namespace Forum.BrokerService -{ - public class Bootstrap - { - private static ILogger _logger; - private static ECommonConfiguration _ecommonConfiguration; - private static BrokerController _broker; - - public static void Initialize() - { - InitializeECommon(); - try - { - InitializeEQueue(); - } - catch (Exception ex) - { - _logger.Error("Initialize EQueue failed.", ex); - throw; - } - } - public static void Start() - { - try - { - _broker.Start(); - } - catch (Exception ex) - { - _logger.Error("Broker start failed.", ex); - throw; - } - } - public static void Stop() - { - try - { - if (_broker != null) - { - _broker.Shutdown(); - } - } - catch (Exception ex) - { - _logger.Error("Broker stop failed.", ex); - throw; - } - } - - private static void InitializeECommon() - { - _ecommonConfiguration = ECommonConfiguration - .Create() - .UseAutofac() - .RegisterCommonComponents() - .UseLog4Net() - .UseJsonNet() - .RegisterUnhandledExceptionHandler(); - _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); - _logger.Info("ECommon initialized."); - } - private static void InitializeEQueue() - { - _ecommonConfiguration.RegisterEQueueComponents(); - var storePath = ConfigurationManager.AppSettings["equeueStorePath"]; - _broker = BrokerController.Create(new BrokerSetting(storePath)); - _logger.Info("EQueue initialized."); - } - } -} +using System; +using System.Configuration; +using ECommon.Autofac; +using ECommon.Components; +using ECommon.Configurations; +using ECommon.JsonNet; +using ECommon.Log4Net; +using ECommon.Logging; +using EQueue.Broker; +using EQueue.Configurations; +using ECommonConfiguration = ECommon.Configurations.Configuration; +using Topshelf; + +namespace Forum.BrokerService +{ + public class Bootstrap : ServiceControl + { + private ILogger _logger; + private ECommonConfiguration _ecommonConfiguration; + private BrokerController _broker; + + #region Initialize + private void Initialize() + { + InitializeECommon(); + try + { + InitializeEQueue(); + } + catch (Exception ex) + { + _logger.Error("Initialize EQueue failed.", ex); + throw; + } + } + + private void InitializeECommon() + { + _ecommonConfiguration = ECommonConfiguration + .Create() + .UseAutofac() + .RegisterCommonComponents() + .UseLog4Net() + .UseJsonNet() + .RegisterUnhandledExceptionHandler(); + _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); + _logger.Info("ECommon initialized."); + } + private void InitializeEQueue() + { + _ecommonConfiguration.RegisterEQueueComponents(); + var storePath = ConfigurationManager.AppSettings["equeueStorePath"]; + _broker = BrokerController.Create(new BrokerSetting(storePath)); + _logger.Info("EQueue initialized."); + } + + #endregion + + public bool Start(HostControl hostControl) + { + try + { + Initialize(); + + _broker.Start(); + return true; + } + catch (Exception ex) + { + _logger.Error("Broker start failed.", ex); + return false; + } + } + + public bool Stop(HostControl hostControl) + { + try + { + if (_broker != null) + { + _broker.Shutdown(); + } + return true; + } + catch (Exception ex) + { + _logger.Error("Broker stop failed.", ex); + + return false; + } + } + } +} diff --git a/Forum.BrokerService/Forum.BrokerService.csproj b/Forum.BrokerService/Forum.BrokerService.csproj index e67f351..014ca9d 100644 --- a/Forum.BrokerService/Forum.BrokerService.csproj +++ b/Forum.BrokerService/Forum.BrokerService.csproj @@ -1,124 +1,114 @@ - - - - - Debug - AnyCPU - {67C0FA69-1233-4217-B6F0-C65658CAC8CE} - Exe - Properties - Forum.BrokerService - Forum.BrokerService - v4.5 - 512 - true - ..\ - true - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - False - ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll - - - ..\packages\Dapper.1.42\lib\net45\Dapper.dll - - - ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll - True - - - ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll - True - - - ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll - - - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - - - ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll - True - - - - - - - - - - - - - - - - Component - - - ProjectInstaller.cs - - - Component - - - Service1.cs - - - - - - - - Designer - - - Always - - - - - ProjectInstaller.cs - - - - - {86985782-8588-48da-81ab-ffd3b855c73c} - Forum.Infrastructure - - - - - + + + + + Debug + AnyCPU + {67C0FA69-1233-4217-B6F0-C65658CAC8CE} + Exe + Properties + Forum.BrokerService + Forum.BrokerService + v4.5 + 512 + true + ..\ + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + False + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + + + ..\packages\Dapper.1.42\lib\net45\Dapper.dll + + + ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll + True + + + ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll + True + + + ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll + + + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll + True + + + + + + + + + + ..\packages\Topshelf.3.3.1\lib\net40-full\Topshelf.dll + True + + + + + + + + + + + Designer + + + Always + + + Always + + + Always + + + + + {86985782-8588-48da-81ab-ffd3b855c73c} + Forum.Infrastructure + + + + + \ No newline at end of file diff --git a/Forum.BrokerService/Program.cs b/Forum.BrokerService/Program.cs index 9138047..d9175fa 100644 --- a/Forum.BrokerService/Program.cs +++ b/Forum.BrokerService/Program.cs @@ -1,45 +1,25 @@ -using System; -using System.ServiceProcess; - -namespace Forum.BrokerService -{ - static class Program - { - /// - /// 应用程序的主入口点。 - /// - static void Main() - { - if (!Environment.UserInteractive) - { - ServiceBase.Run(new Service1()); - } - else - { - Bootstrap.Initialize(); - Bootstrap.Start(); - - Console.BackgroundColor = ConsoleColor.Black; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Initialize success..."); - Console.ResetColor(); - Console.WriteLine(); - - Console.WriteLine("Press enter to exit..."); - var line = Console.ReadLine(); - while (line != "exit") - { - switch (line) - { - case "cls": - Console.Clear(); - break; - default: - return; - } - line = Console.ReadLine(); - } - } - } - } -} +using Topshelf; + +namespace Forum.BrokerService +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + static int Main() + { + return (int)HostFactory.Run(x => + { + x.RunAsLocalSystem(); + x.SetServiceName("Forum.BrokerService"); + x.SetDisplayName("Forum BrokerService"); + x.SetDescription("Forum BrokerService Samples"); + + x.Service(); + + x.EnableServiceRecovery(r => { r.RestartService(1); }); + }); + } + } +} diff --git a/Forum.BrokerService/ProjectInstaller.Designer.cs b/Forum.BrokerService/ProjectInstaller.Designer.cs deleted file mode 100644 index b8649c4..0000000 --- a/Forum.BrokerService/ProjectInstaller.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace Forum.BrokerService -{ - partial class ProjectInstaller - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); - this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); - // - // serviceProcessInstaller1 - // - this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService; - this.serviceProcessInstaller1.Password = null; - this.serviceProcessInstaller1.Username = null; - // - // serviceInstaller1 - // - this.serviceInstaller1.Description = "Service to host the equeue message broker."; - this.serviceInstaller1.DisplayName = "Forum.BrokerService"; - this.serviceInstaller1.ServiceName = "Forum.BrokerService"; - // - // ProjectInstaller - // - this.Installers.AddRange(new System.Configuration.Install.Installer[] { - this.serviceProcessInstaller1, - this.serviceInstaller1}); - - } - - #endregion - - private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; - private System.ServiceProcess.ServiceInstaller serviceInstaller1; - } -} \ No newline at end of file diff --git a/Forum.BrokerService/ProjectInstaller.cs b/Forum.BrokerService/ProjectInstaller.cs deleted file mode 100644 index 36be70c..0000000 --- a/Forum.BrokerService/ProjectInstaller.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Configuration.Install; -using System.Linq; -using System.Threading.Tasks; - -namespace Forum.BrokerService -{ - [RunInstaller(true)] - public partial class ProjectInstaller : System.Configuration.Install.Installer - { - public ProjectInstaller() - { - InitializeComponent(); - } - } -} diff --git a/Forum.BrokerService/ProjectInstaller.resx b/Forum.BrokerService/ProjectInstaller.resx deleted file mode 100644 index 77b6f7b..0000000 --- a/Forum.BrokerService/ProjectInstaller.resx +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 55 - - - 208, 17 - - - False - - \ No newline at end of file diff --git a/Forum.BrokerService/Service1.Designer.cs b/Forum.BrokerService/Service1.Designer.cs deleted file mode 100644 index 4dfc604..0000000 --- a/Forum.BrokerService/Service1.Designer.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Forum.BrokerService -{ - partial class Service1 - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - components = new System.ComponentModel.Container(); - this.ServiceName = "Service1"; - } - - #endregion - } -} diff --git a/Forum.BrokerService/Service1.cs b/Forum.BrokerService/Service1.cs deleted file mode 100644 index 713ef77..0000000 --- a/Forum.BrokerService/Service1.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.ServiceProcess; - -namespace Forum.BrokerService -{ - public partial class Service1 : ServiceBase - { - public Service1() - { - InitializeComponent(); - Bootstrap.Initialize(); - } - - protected override void OnStart(string[] args) - { - Bootstrap.Start(); - } - protected override void OnStop() - { - Bootstrap.Stop(); - } - } -} diff --git a/Forum.BrokerService/install.bat b/Forum.BrokerService/install.bat new file mode 100644 index 0000000..dfab747 --- /dev/null +++ b/Forum.BrokerService/install.bat @@ -0,0 +1,2 @@ +Forum.BrokerService.exe install +Forum.BrokerService.exe start \ No newline at end of file diff --git a/Forum.BrokerService/packages.config b/Forum.BrokerService/packages.config index 8b3a995..5cf03cd 100644 --- a/Forum.BrokerService/packages.config +++ b/Forum.BrokerService/packages.config @@ -1,10 +1,11 @@ - - - - - - - - - + + + + + + + + + + \ No newline at end of file diff --git a/Forum.BrokerService/uninstall.bat b/Forum.BrokerService/uninstall.bat new file mode 100644 index 0000000..b8bae2b --- /dev/null +++ b/Forum.BrokerService/uninstall.bat @@ -0,0 +1,2 @@ +Forum.BrokerService.exe stop +Forum.BrokerService.exe uninstall \ No newline at end of file diff --git a/Forum.CommandService/Bootstrap.cs b/Forum.CommandService/Bootstrap.cs index 98af21b..6de9a2c 100644 --- a/Forum.CommandService/Bootstrap.cs +++ b/Forum.CommandService/Bootstrap.cs @@ -1,108 +1,116 @@ -using System; -using System.Reflection; -using ECommon.Autofac; -using ECommon.Components; -using ECommon.Configurations; -using ECommon.JsonNet; -using ECommon.Log4Net; -using ECommon.Logging; -using ENode.Configurations; -using ENode.Infrastructure; -using Forum.Domain.Accounts; -using Forum.Infrastructure; - -namespace Forum.CommandService -{ - public class Bootstrap - { - private static ILogger _logger; - private static Configuration _ecommonConfiguration; - private static ENodeConfiguration _enodeConfiguration; - - public static void Initialize() - { - InitializeECommon(); - try - { - InitializeENode(); - InitializeCommandService(); - } - catch (Exception ex) - { - _logger.Error("Initialize ENode failed.", ex); - throw; - } - } - public static void Start() - { - try - { - _enodeConfiguration.StartEQueue(); - } - catch (Exception ex) - { - _logger.Error("EQueue start failed.", ex); - throw; - } - } - public static void Stop() - { - try - { - _enodeConfiguration.ShutdownEQueue(); - } - catch (Exception ex) - { - _logger.Error("EQueue stop failed.", ex); - throw; - } - } - - private static void InitializeECommon() - { - _ecommonConfiguration = Configuration - .Create() - .UseAutofac() - .RegisterCommonComponents() - .UseLog4Net() - .UseJsonNet() - .RegisterUnhandledExceptionHandler(); - _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); - _logger.Info("ECommon initialized."); - } - private static void InitializeENode() - { - ConfigSettings.Initialize(); - - var assemblies = new[] - { - Assembly.Load("Forum.Infrastructure"), - Assembly.Load("Forum.Commands"), - Assembly.Load("Forum.Domain"), - Assembly.Load("Forum.Domain.Dapper"), - Assembly.Load("Forum.CommandHandlers"), - Assembly.Load("Forum.CommandService") - }; - var setting = new ConfigurationSetting - { - SqlDefaultConnectionString = ConfigSettings.ENodeConnectionString - }; - - _enodeConfiguration = _ecommonConfiguration - .CreateENode(setting) - .RegisterENodeComponents() - .RegisterBusinessComponents(assemblies) - .UseSqlServerLockService() - .UseSqlServerCommandStore() - .UseSqlServerEventStore() - .UseEQueue() - .InitializeBusinessAssemblies(assemblies); - _logger.Info("ENode initialized."); - } - private static void InitializeCommandService() - { - ObjectContainer.Resolve().AddLockKey(typeof(Account).Name); - _logger.Info("Command service initialized."); - } - } -} +using System; +using System.Reflection; +using ECommon.Autofac; +using ECommon.Components; +using ECommon.Configurations; +using ECommon.JsonNet; +using ECommon.Log4Net; +using ECommon.Logging; +using ENode.Configurations; +using ENode.Infrastructure; +using Forum.Domain.Accounts; +using Forum.Infrastructure; +using Topshelf; + +namespace Forum.CommandService +{ + public class Bootstrap : ServiceControl + { + private ILogger _logger; + private Configuration _ecommonConfiguration; + private ENodeConfiguration _enodeConfiguration; + + #region Initialize + private void Initialize() + { + InitializeECommon(); + try + { + InitializeENode(); + InitializeCommandService(); + } + catch (Exception ex) + { + _logger.Error("Initialize ENode failed.", ex); + throw; + } + } + + private void InitializeECommon() + { + _ecommonConfiguration = Configuration + .Create() + .UseAutofac() + .RegisterCommonComponents() + .UseLog4Net() + .UseJsonNet() + .RegisterUnhandledExceptionHandler(); + _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); + _logger.Info("ECommon initialized."); + } + private void InitializeENode() + { + ConfigSettings.Initialize(); + + var assemblies = new[] + { + Assembly.Load("Forum.Infrastructure"), + Assembly.Load("Forum.Commands"), + Assembly.Load("Forum.Domain"), + Assembly.Load("Forum.Domain.Dapper"), + Assembly.Load("Forum.CommandHandlers"), + Assembly.Load("Forum.CommandService") + }; + var setting = new ConfigurationSetting + { + SqlDefaultConnectionString = ConfigSettings.ENodeConnectionString + }; + + _enodeConfiguration = _ecommonConfiguration + .CreateENode(setting) + .RegisterENodeComponents() + .RegisterBusinessComponents(assemblies) + .UseSqlServerLockService() + .UseSqlServerCommandStore() + .UseSqlServerEventStore() + .UseEQueue() + .InitializeBusinessAssemblies(assemblies); + _logger.Info("ENode initialized."); + } + private void InitializeCommandService() + { + ObjectContainer.Resolve().AddLockKey(typeof(Account).Name); + _logger.Info("Command service initialized."); + } + #endregion + + public bool Start(HostControl hostControl) + { + try + { + Initialize(); + _enodeConfiguration.StartEQueue(); + return true; + } + catch (Exception ex) + { + _logger.Error("EQueue start failed.", ex); + return false; + } + } + + public bool Stop(HostControl hostControl) + { + try + { + _enodeConfiguration.ShutdownEQueue(); + return true; + } + catch (Exception ex) + { + _logger.Error("EQueue stop failed.", ex); + return false; + } + } + } +} diff --git a/Forum.CommandService/Forum.CommandService.csproj b/Forum.CommandService/Forum.CommandService.csproj index 3562445..1bca1ea 100644 --- a/Forum.CommandService/Forum.CommandService.csproj +++ b/Forum.CommandService/Forum.CommandService.csproj @@ -1,150 +1,140 @@ - - - - - Debug - AnyCPU - {6CE6AB8F-FA95-4E28-AF7C-45125E61609E} - Exe - Properties - Forum.CommandService - Forum.CommandService - v4.5 - 512 - true - ..\ - true - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - False - ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll - - - ..\packages\Dapper.1.42\lib\net45\Dapper.dll - - - ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll - True - - - ..\packages\ENode.2.7.1\lib\net45\ENode.dll - True - - - ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll - True - - - ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll - - - - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - - - ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll - True - - - - - - - - - - - - - - - - Component - - - ProjectInstaller.cs - - - - Component - - - Service1.cs - - - - - - - connectionStrings.config - PreserveNewest - - - - Always - - - Always - - - - - ProjectInstaller.cs - - - - - {985e2001-f607-44bf-a84b-31e9b7d89d0c} - Forum.CommandHandlers - - - {7787ec6c-9cf3-401c-803a-b4da2b0ca9ef} - Forum.Commands - - - {6ad347f8-d02e-4aaa-a72b-c4ad93971986} - Forum.Domain.Dapper - - - {5a4055c3-89bc-4149-8a6d-06ce985080c9} - Forum.Domain - - - {86985782-8588-48da-81ab-ffd3b855c73c} - Forum.Infrastructure - - - - - + + + + + Debug + AnyCPU + {6CE6AB8F-FA95-4E28-AF7C-45125E61609E} + Exe + Properties + Forum.CommandService + Forum.CommandService + v4.5 + 512 + true + ..\ + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + False + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + + + ..\packages\Dapper.1.42\lib\net45\Dapper.dll + + + ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll + True + + + ..\packages\ENode.2.7.1\lib\net45\ENode.dll + True + + + ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll + True + + + ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll + + + + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll + True + + + + + + + + + ..\packages\Topshelf.3.3.1\lib\net40-full\Topshelf.dll + True + + + + + + + + + + + + connectionStrings.config + PreserveNewest + + + + Always + + + Always + + + Always + + + Always + + + + + {985e2001-f607-44bf-a84b-31e9b7d89d0c} + Forum.CommandHandlers + + + {7787ec6c-9cf3-401c-803a-b4da2b0ca9ef} + Forum.Commands + + + {6ad347f8-d02e-4aaa-a72b-c4ad93971986} + Forum.Domain.Dapper + + + {5a4055c3-89bc-4149-8a6d-06ce985080c9} + Forum.Domain + + + {86985782-8588-48da-81ab-ffd3b855c73c} + Forum.Infrastructure + + + + + \ No newline at end of file diff --git a/Forum.CommandService/Program.cs b/Forum.CommandService/Program.cs index f3c958b..191f668 100644 --- a/Forum.CommandService/Program.cs +++ b/Forum.CommandService/Program.cs @@ -1,45 +1,25 @@ -using System; -using System.ServiceProcess; - -namespace Forum.CommandService -{ - static class Program - { - /// - /// 应用程序的主入口点。 - /// - static void Main() - { - if (!Environment.UserInteractive) - { - ServiceBase.Run(new Service1()); - } - else - { - Bootstrap.Initialize(); - Bootstrap.Start(); - - Console.BackgroundColor = ConsoleColor.Black; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Initialize success..."); - Console.ResetColor(); - Console.WriteLine(); - - Console.WriteLine("Press enter to exit..."); - var line = Console.ReadLine(); - while (line != "exit") - { - switch (line) - { - case "cls": - Console.Clear(); - break; - default: - return; - } - line = Console.ReadLine(); - } - } - } - } -} +using Topshelf; + +namespace Forum.CommandService +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + static int Main() + { + return (int)HostFactory.Run(x => + { + x.RunAsLocalSystem(); + x.SetServiceName("Forum.CommandService"); + x.SetDisplayName("Forum CommandService"); + x.SetDescription("Forum.CommandService Samples"); + + x.Service(); + + x.EnableServiceRecovery(r => { r.RestartService(1); }); + }); + } + } +} diff --git a/Forum.CommandService/ProjectInstaller.Designer.cs b/Forum.CommandService/ProjectInstaller.Designer.cs deleted file mode 100644 index 8149a21..0000000 --- a/Forum.CommandService/ProjectInstaller.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace Forum.CommandService -{ - partial class ProjectInstaller - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); - this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); - // - // serviceProcessInstaller1 - // - this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService; - this.serviceProcessInstaller1.Password = null; - this.serviceProcessInstaller1.Username = null; - // - // serviceInstaller1 - // - this.serviceInstaller1.Description = "Service to process forum commands."; - this.serviceInstaller1.DisplayName = "Forum.CommandService"; - this.serviceInstaller1.ServiceName = "Forum.CommandService"; - // - // ProjectInstaller - // - this.Installers.AddRange(new System.Configuration.Install.Installer[] { - this.serviceProcessInstaller1, - this.serviceInstaller1}); - - } - - #endregion - - private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; - private System.ServiceProcess.ServiceInstaller serviceInstaller1; - } -} \ No newline at end of file diff --git a/Forum.CommandService/ProjectInstaller.cs b/Forum.CommandService/ProjectInstaller.cs deleted file mode 100644 index b16e0c0..0000000 --- a/Forum.CommandService/ProjectInstaller.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Configuration.Install; -using System.Linq; -using System.Threading.Tasks; - -namespace Forum.CommandService -{ - [RunInstaller(true)] - public partial class ProjectInstaller : System.Configuration.Install.Installer - { - public ProjectInstaller() - { - InitializeComponent(); - } - } -} diff --git a/Forum.CommandService/ProjectInstaller.resx b/Forum.CommandService/ProjectInstaller.resx deleted file mode 100644 index 77b6f7b..0000000 --- a/Forum.CommandService/ProjectInstaller.resx +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 55 - - - 208, 17 - - - False - - \ No newline at end of file diff --git a/Forum.CommandService/Service1.Designer.cs b/Forum.CommandService/Service1.Designer.cs deleted file mode 100644 index 02dfd14..0000000 --- a/Forum.CommandService/Service1.Designer.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Forum.CommandService -{ - partial class Service1 - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - components = new System.ComponentModel.Container(); - this.ServiceName = "Service1"; - } - - #endregion - } -} diff --git a/Forum.CommandService/Service1.cs b/Forum.CommandService/Service1.cs deleted file mode 100644 index 4864f7b..0000000 --- a/Forum.CommandService/Service1.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.ServiceProcess; - -namespace Forum.CommandService -{ - public partial class Service1 : ServiceBase - { - public Service1() - { - InitializeComponent(); - Bootstrap.Initialize(); - } - - protected override void OnStart(string[] args) - { - Bootstrap.Start(); - } - protected override void OnStop() - { - Bootstrap.Stop(); - } - } -} diff --git a/Forum.CommandService/install.bat b/Forum.CommandService/install.bat new file mode 100644 index 0000000..9f564d5 --- /dev/null +++ b/Forum.CommandService/install.bat @@ -0,0 +1,2 @@ +Forum.CommandService.exe install +Forum.CommandService.exe start \ No newline at end of file diff --git a/Forum.CommandService/packages.config b/Forum.CommandService/packages.config index 0afb889..9140efd 100644 --- a/Forum.CommandService/packages.config +++ b/Forum.CommandService/packages.config @@ -1,11 +1,12 @@ - - - - - - - - - - + + + + + + + + + + + \ No newline at end of file diff --git a/Forum.CommandService/uninstall.bat b/Forum.CommandService/uninstall.bat new file mode 100644 index 0000000..dc3530a --- /dev/null +++ b/Forum.CommandService/uninstall.bat @@ -0,0 +1,2 @@ +Forum.CommandService.exe stop +Forum.CommandService.exe uninstall \ No newline at end of file diff --git a/Forum.EventService/Bootstrap.cs b/Forum.EventService/Bootstrap.cs index 4360588..0da13d0 100644 --- a/Forum.EventService/Bootstrap.cs +++ b/Forum.EventService/Bootstrap.cs @@ -1,99 +1,107 @@ -using System; -using System.Reflection; -using ECommon.Autofac; -using ECommon.Components; -using ECommon.Configurations; -using ECommon.JsonNet; -using ECommon.Log4Net; -using ECommon.Logging; -using ENode.Configurations; -using Forum.Infrastructure; - -namespace Forum.EventService -{ - public class Bootstrap - { - private static ILogger _logger; - private static Configuration _ecommonConfiguration; - private static ENodeConfiguration _enodeConfiguration; - - public static void Initialize() - { - InitializeECommon(); - try - { - InitializeENode(); - } - catch (Exception ex) - { - _logger.Error("Initialize ENode failed.", ex); - throw; - } - } - public static void Start() - { - try - { - _enodeConfiguration.StartEQueue(); - } - catch (Exception ex) - { - _logger.Error("EQueue start failed.", ex); - throw; - } - } - public static void Stop() - { - try - { - _enodeConfiguration.ShutdownEQueue(); - } - catch (Exception ex) - { - _logger.Error("EQueue stop failed.", ex); - throw; - } - } - - private static void InitializeECommon() - { - _ecommonConfiguration = Configuration - .Create() - .UseAutofac() - .RegisterCommonComponents() - .UseLog4Net() - .UseJsonNet() - .RegisterUnhandledExceptionHandler(); - _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); - _logger.Info("ECommon initialized."); - } - private static void InitializeENode() - { - ConfigSettings.Initialize(); - - var assemblies = new[] - { - Assembly.Load("Forum.Infrastructure"), - Assembly.Load("Forum.Commands"), - Assembly.Load("Forum.Domain"), - Assembly.Load("Forum.Denormalizers.Dapper"), - Assembly.Load("Forum.ProcessManagers"), - Assembly.Load("Forum.EventService") - }; - var setting = new ConfigurationSetting - { - SqlDefaultConnectionString = ConfigSettings.ENodeConnectionString - }; - - _enodeConfiguration = _ecommonConfiguration - .CreateENode(setting) - .RegisterENodeComponents() - .RegisterBusinessComponents(assemblies) - .UseSqlServerSequenceMessagePublishedVersionStore() - .UseSqlServerMessageHandleRecordStore() - .UseEQueue() - .InitializeBusinessAssemblies(assemblies); - _logger.Info("ENode initialized."); - } - } -} +using System; +using System.Reflection; +using ECommon.Autofac; +using ECommon.Components; +using ECommon.Configurations; +using ECommon.JsonNet; +using ECommon.Log4Net; +using ECommon.Logging; +using ENode.Configurations; +using Forum.Infrastructure; +using Topshelf; + +namespace Forum.EventService +{ + public class Bootstrap : ServiceControl + { + private ILogger _logger; + private Configuration _ecommonConfiguration; + private ENodeConfiguration _enodeConfiguration; + + #region Initialize + private void Initialize() + { + InitializeECommon(); + try + { + InitializeENode(); + } + catch (Exception ex) + { + _logger.Error("Initialize ENode failed.", ex); + throw; + } + } + + private void InitializeECommon() + { + _ecommonConfiguration = Configuration + .Create() + .UseAutofac() + .RegisterCommonComponents() + .UseLog4Net() + .UseJsonNet() + .RegisterUnhandledExceptionHandler(); + _logger = ObjectContainer.Resolve().Create(typeof(Bootstrap).FullName); + _logger.Info("ECommon initialized."); + } + private void InitializeENode() + { + ConfigSettings.Initialize(); + + var assemblies = new[] + { + Assembly.Load("Forum.Infrastructure"), + Assembly.Load("Forum.Commands"), + Assembly.Load("Forum.Domain"), + Assembly.Load("Forum.Denormalizers.Dapper"), + Assembly.Load("Forum.ProcessManagers"), + Assembly.Load("Forum.EventService") + }; + var setting = new ConfigurationSetting + { + SqlDefaultConnectionString = ConfigSettings.ENodeConnectionString + }; + + _enodeConfiguration = _ecommonConfiguration + .CreateENode(setting) + .RegisterENodeComponents() + .RegisterBusinessComponents(assemblies) + .UseSqlServerSequenceMessagePublishedVersionStore() + .UseSqlServerMessageHandleRecordStore() + .UseEQueue() + .InitializeBusinessAssemblies(assemblies); + _logger.Info("ENode initialized."); + } + #endregion Initialize + + public bool Start(HostControl hostControl) + { + try + { + Initialize(); + _enodeConfiguration.StartEQueue(); + return true; + } + catch (Exception ex) + { + _logger.Error("EQueue start failed.", ex); + return false; + } + } + + public bool Stop(HostControl hostControl) + { + try + { + _enodeConfiguration.ShutdownEQueue(); + return true; + } + catch (Exception ex) + { + _logger.Error("EQueue stop failed.", ex); + return false; + } + } + } +} diff --git a/Forum.EventService/Forum.EventService.csproj b/Forum.EventService/Forum.EventService.csproj index c48cf39..c9e5ae1 100644 --- a/Forum.EventService/Forum.EventService.csproj +++ b/Forum.EventService/Forum.EventService.csproj @@ -1,151 +1,141 @@ - - - - - Debug - AnyCPU - {B29D46C2-A4D4-492E-A154-03F7DF07BA86} - Exe - Properties - Forum.EventService - Forum.EventService - v4.5 - 512 - true - ..\ - true - - - - AnyCPU - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - AnyCPU - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - False - ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll - - - ..\packages\Dapper.1.42\lib\net45\Dapper.dll - - - ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll - True - - - ..\packages\ENode.2.7.1\lib\net45\ENode.dll - True - - - ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll - True - - - ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll - - - - ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll - - - ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll - True - - - - - - - - - - - - - - - - - Component - - - ProjectInstaller.cs - - - - Component - - - Service1.cs - - - - - - - connectionStrings.config - PreserveNewest - - - - Always - - - Always - - - - - ProjectInstaller.cs - - - - - {7787ec6c-9cf3-401c-803a-b4da2b0ca9ef} - Forum.Commands - - - {433dd072-0748-4440-ba1b-4a0d6e507d3b} - Forum.Denormalizers.Dapper - - - {5a4055c3-89bc-4149-8a6d-06ce985080c9} - Forum.Domain - - - {86985782-8588-48da-81ab-ffd3b855c73c} - Forum.Infrastructure - - - {9790a5c5-38e6-4318-97f1-a07e3b2cbd8d} - Forum.ProcessManagers - - - - - + + + + + Debug + AnyCPU + {B29D46C2-A4D4-492E-A154-03F7DF07BA86} + Exe + Properties + Forum.EventService + Forum.EventService + v4.5 + 512 + true + ..\ + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + False + ..\packages\Autofac.3.5.2\lib\net40\Autofac.dll + + + ..\packages\Dapper.1.42\lib\net45\Dapper.dll + + + ..\packages\ECommon.2.0.2\lib\net45\ECommon.dll + True + + + ..\packages\ENode.2.7.1\lib\net45\ENode.dll + True + + + ..\packages\EQueue.2.0.7\lib\net45\EQueue.dll + True + + + ..\packages\log4net.2.0.3\lib\net40-full\log4net.dll + + + + ..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll + + + ..\packages\protobuf-net.2.0.0.668\lib\net40\protobuf-net.dll + True + + + + + + + + + + ..\packages\Topshelf.3.3.1\lib\net40-full\Topshelf.dll + True + + + + + + + + + + + + connectionStrings.config + PreserveNewest + + + + Always + + + Always + + + Always + + + Always + + + + + {7787ec6c-9cf3-401c-803a-b4da2b0ca9ef} + Forum.Commands + + + {433dd072-0748-4440-ba1b-4a0d6e507d3b} + Forum.Denormalizers.Dapper + + + {5a4055c3-89bc-4149-8a6d-06ce985080c9} + Forum.Domain + + + {86985782-8588-48da-81ab-ffd3b855c73c} + Forum.Infrastructure + + + {9790a5c5-38e6-4318-97f1-a07e3b2cbd8d} + Forum.ProcessManagers + + + + + \ No newline at end of file diff --git a/Forum.EventService/Program.cs b/Forum.EventService/Program.cs index c4bc099..47f42ae 100644 --- a/Forum.EventService/Program.cs +++ b/Forum.EventService/Program.cs @@ -1,45 +1,25 @@ -using System; -using System.ServiceProcess; - -namespace Forum.EventService -{ - static class Program - { - /// - /// 应用程序的主入口点。 - /// - static void Main() - { - if (!Environment.UserInteractive) - { - ServiceBase.Run(new Service1()); - } - else - { - Bootstrap.Initialize(); - Bootstrap.Start(); - - Console.BackgroundColor = ConsoleColor.Black; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine("Initialize success..."); - Console.ResetColor(); - Console.WriteLine(); - - Console.WriteLine("Press enter to exit..."); - var line = Console.ReadLine(); - while (line != "exit") - { - switch (line) - { - case "cls": - Console.Clear(); - break; - default: - return; - } - line = Console.ReadLine(); - } - } - } - } -} +using Topshelf; + +namespace Forum.EventService +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + static int Main() + { + return (int)HostFactory.Run(x => + { + x.RunAsLocalSystem(); + x.SetServiceName("Forum.CommandService"); + x.SetDisplayName("Forum CommandService"); + x.SetDescription("Forum.CommandService Samples"); + + x.Service(); + + x.EnableServiceRecovery(r => { r.RestartService(1); }); + }); + } + } +} diff --git a/Forum.EventService/ProjectInstaller.Designer.cs b/Forum.EventService/ProjectInstaller.Designer.cs deleted file mode 100644 index 2920491..0000000 --- a/Forum.EventService/ProjectInstaller.Designer.cs +++ /dev/null @@ -1,59 +0,0 @@ -namespace Forum.EventService -{ - partial class ProjectInstaller - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller(); - this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller(); - // - // serviceProcessInstaller1 - // - this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService; - this.serviceProcessInstaller1.Password = null; - this.serviceProcessInstaller1.Username = null; - // - // serviceInstaller1 - // - this.serviceInstaller1.Description = "Service to process forum domain events."; - this.serviceInstaller1.DisplayName = "Forum.EventService"; - this.serviceInstaller1.ServiceName = "Forum.EventService"; - // - // ProjectInstaller - // - this.Installers.AddRange(new System.Configuration.Install.Installer[] { - this.serviceProcessInstaller1, - this.serviceInstaller1}); - - } - - #endregion - - private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; - private System.ServiceProcess.ServiceInstaller serviceInstaller1; - } -} \ No newline at end of file diff --git a/Forum.EventService/ProjectInstaller.cs b/Forum.EventService/ProjectInstaller.cs deleted file mode 100644 index b35cee7..0000000 --- a/Forum.EventService/ProjectInstaller.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; -using System.Configuration.Install; -using System.Linq; -using System.Threading.Tasks; - -namespace Forum.EventService -{ - [RunInstaller(true)] - public partial class ProjectInstaller : System.Configuration.Install.Installer - { - public ProjectInstaller() - { - InitializeComponent(); - } - } -} diff --git a/Forum.EventService/ProjectInstaller.resx b/Forum.EventService/ProjectInstaller.resx deleted file mode 100644 index a07029f..0000000 --- a/Forum.EventService/ProjectInstaller.resx +++ /dev/null @@ -1,129 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - 17, 17 - - - 208, 17 - - - False - - \ No newline at end of file diff --git a/Forum.EventService/Service1.Designer.cs b/Forum.EventService/Service1.Designer.cs deleted file mode 100644 index d51124f..0000000 --- a/Forum.EventService/Service1.Designer.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Forum.EventService -{ - partial class Service1 - { - /// - /// 必需的设计器变量。 - /// - private System.ComponentModel.IContainer components = null; - - /// - /// 清理所有正在使用的资源。 - /// - /// 如果应释放托管资源,为 true;否则为 false。 - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region 组件设计器生成的代码 - - /// - /// 设计器支持所需的方法 - 不要 - /// 使用代码编辑器修改此方法的内容。 - /// - private void InitializeComponent() - { - components = new System.ComponentModel.Container(); - this.ServiceName = "Service1"; - } - - #endregion - } -} diff --git a/Forum.EventService/Service1.cs b/Forum.EventService/Service1.cs deleted file mode 100644 index 76ae75e..0000000 --- a/Forum.EventService/Service1.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.ServiceProcess; - -namespace Forum.EventService -{ - public partial class Service1 : ServiceBase - { - public Service1() - { - InitializeComponent(); - Bootstrap.Initialize(); - } - - protected override void OnStart(string[] args) - { - Bootstrap.Start(); - } - protected override void OnStop() - { - Bootstrap.Stop(); - } - } -} diff --git a/Forum.EventService/install.bat b/Forum.EventService/install.bat new file mode 100644 index 0000000..09013ca --- /dev/null +++ b/Forum.EventService/install.bat @@ -0,0 +1,2 @@ +Forum.EventService.exe install +Forum.EventService.exe start \ No newline at end of file diff --git a/Forum.EventService/packages.config b/Forum.EventService/packages.config index 0afb889..9140efd 100644 --- a/Forum.EventService/packages.config +++ b/Forum.EventService/packages.config @@ -1,11 +1,12 @@ - - - - - - - - - - + + + + + + + + + + + \ No newline at end of file diff --git a/Forum.EventService/uninstall.bat b/Forum.EventService/uninstall.bat new file mode 100644 index 0000000..bb363b3 --- /dev/null +++ b/Forum.EventService/uninstall.bat @@ -0,0 +1,2 @@ +Forum.EventService.exe stop +Forum.EventService.exe uninstall \ No newline at end of file