private static void StartupEndpoint() { try { Logger.LogInfo("Attempting to execute database upgrade."); var connectionStringSettings = ConfigurationManager.ConnectionStrings["gFileSystemEntities"]; var connectionStringBuilder = new SqlConnectionStringBuilder(connectionStringSettings.ConnectionString) { InitialCatalog = "Master" }; var installer = new DatabaseInstaller(); if (installer.NeedsUpdate(connectionStringSettings.ConnectionString)) { var setup = new InstallSetup { AcceptVersionWarningsChangedScripts = true, AcceptVersionWarningsNewScripts = true, ConnectionString = connectionStringSettings.ConnectionString, InstallStatus = InstallStatusConstants.Upgrade, MasterConnectionString = connectionStringBuilder.ToString() }; installer.Install(setup); } } catch (Exception ex) { Logger.LogError(ex, "Failed to execute database upgrade."); throw new Exception("Failed to execute database upgrade."); } Logger.LogInfo("Services Started Begin"); try { #region Primary Endpoint var service = new SystemCore(); var primaryAddress = new Uri("net.tcp://localhost:" + ConfigHelper.Port + "/__gfile"); var primaryHost = new ServiceHost(service, primaryAddress); //Initialize the service //var netTcpBinding = new CompressedNetTcpBinding(); var netTcpBinding = new NetTcpBinding { MaxBufferSize = 10 * 1024 * 1024, MaxReceivedMessageSize = 10 * 1024 * 1024, MaxBufferPoolSize = 10 * 1024 * 1024 }; netTcpBinding.ReaderQuotas.MaxStringContentLength = 10 * 1024 * 1024; netTcpBinding.ReaderQuotas.MaxBytesPerRead = 10 * 1024 * 1024; netTcpBinding.ReaderQuotas.MaxArrayLength = 10 * 1024 * 1024; netTcpBinding.ReaderQuotas.MaxDepth = 10 * 1024 * 1024; netTcpBinding.ReaderQuotas.MaxNameTableCharCount = 10 * 1024 * 1024; netTcpBinding.Security.Mode = SecurityMode.None; primaryHost.AddServiceEndpoint(typeof(ISystemCore), netTcpBinding, string.Empty); primaryHost.Open(); //Create Core Listener var primaryEndpoint = new EndpointAddress(primaryHost.BaseAddresses.First().AbsoluteUri); var primaryClient = new ChannelFactory <ISystemCore>(netTcpBinding, primaryEndpoint); _core = primaryClient.CreateChannel(); #endregion Logger.LogInfo("Service Running on Port " + ConfigHelper.Port); Logger.LogInfo("Services Started End"); } catch (Exception ex) { Logger.LogError(ex); throw; } }
private void StartupEndpoint() { var config = new SetupConfig(); try { LoggerCQ.LogInfo("Attempting to upgrade database."); var connectionStringSettings = ConfigurationManager.ConnectionStrings["DatastoreEntities"]; var connectionStringBuilder = new SqlConnectionStringBuilder(connectionStringSettings.ConnectionString) { InitialCatalog = "Master" }; //Make sure there are no other nHydrate installations on this database if (DbMaintenanceHelper.ContainsOtherInstalls(connectionStringSettings.ConnectionString)) { LoggerCQ.LogError($"The database contains another installation. This is an error condition. Database={connectionStringBuilder.InitialCatalog}"); throw new Exception($"The database contains another installation. This is an error condition. Database={connectionStringBuilder.InitialCatalog}"); } //Even a blank database gets updated below so save if DB is blank when started var isBlank = DbMaintenanceHelper.IsBlank(connectionStringSettings.ConnectionString); var installer = new DatabaseInstaller(); if (installer.NeedsUpdate(connectionStringSettings.ConnectionString)) { var setup = new InstallSetup { AcceptVersionWarningsChangedScripts = true, AcceptVersionWarningsNewScripts = true, ConnectionString = connectionStringSettings.ConnectionString, InstallStatus = InstallStatusConstants.Upgrade, MasterConnectionString = connectionStringBuilder.ToString(), SuppressUI = true, }; installer.Install(setup); } //If new database then add file split data files to reduce file locking if (isBlank) { try { DbMaintenanceHelper.SplitDbFiles(connectionStringSettings.ConnectionString); LoggerCQ.LogInfo("New database has split data files."); } catch { LoggerCQ.LogWarning("New database could not split data files."); } try { var configFile = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "setup.config"); if (File.Exists(configFile)) { var barr = File.ReadAllBytes(configFile); config = ServerUtilities.DeserializeObject <SetupConfig>(barr); } } catch (Exception ex) { throw new Exception($"Setup configuration file is not valid."); } if (config != null) { if (!string.IsNullOrEmpty(config.ListDataPath) && !Directory.Exists(config.ListDataPath)) { throw new Exception("The setup configuration file value 'ListDataPath' is not valid"); } if (!string.IsNullOrEmpty(config.IndexPath) && !Directory.Exists(config.IndexPath)) { throw new Exception("The setup configuration file value 'IndexPath' is not valid"); } //Create a file group for List tables config.ListDataPath = DbMaintenanceHelper.CreateFileGroup(connectionStringSettings.ConnectionString, config.ListDataPath, SetupConfig.YFileGroup); //Create a file group for Indexes config.IndexPath = DbMaintenanceHelper.CreateFileGroup(connectionStringSettings.ConnectionString, config.IndexPath, SetupConfig.IndexFileGroup); } } } catch (Exception ex) { LoggerCQ.LogError(ex, "Failed on database upgrade."); throw new Exception("Failed on database upgrade."); } LoggerCQ.LogInfo("Service started begin"); try { #region Primary Endpoint var service = new Gravitybox.Datastore.Server.Core.SystemCore(ConfigurationManager.ConnectionStrings["DatastoreEntities"].ConnectionString, _enableHouseKeeping); if (config != null) { ConfigHelper.SetupConfig = config; } #region Determine if configured port is free var isPortFree = false; do { try { //Determine if can connect to port using (var p1 = new System.Net.Sockets.TcpClient("localhost", ConfigHelper.Port)) { } //If did connect successfully then there is already something on this port isPortFree = false; LoggerCQ.LogInfo($"Port {ConfigHelper.Port} is in use..."); System.Threading.Thread.Sleep(3000); //wait... } catch (Exception ex) { //If there is an error connecting then nothing is listening on that port so FREE isPortFree = true; } } while (!isPortFree); #endregion var primaryAddress = new Uri($"net.tcp://localhost:{ConfigHelper.Port}/__datastore_core"); var primaryHost = new ServiceHost(service, primaryAddress); //Initialize the service var netTcpBinding = new NetTcpBinding(); netTcpBinding.MaxConnections = ThrottleMax; netTcpBinding.Security.Mode = SecurityMode.None; primaryHost.AddServiceEndpoint(typeof(Gravitybox.Datastore.Common.ISystemCore), netTcpBinding, string.Empty); //Add more threads var stb = new ServiceThrottlingBehavior { MaxConcurrentSessions = ThrottleMax, MaxConcurrentCalls = ThrottleMax, MaxConcurrentInstances = ThrottleMax, }; primaryHost.Description.Behaviors.Add(stb); primaryHost.Open(); //Create Core Listener var primaryEndpoint = new EndpointAddress(primaryHost.BaseAddresses.First().AbsoluteUri); var primaryClient = new ChannelFactory <Gravitybox.Datastore.Common.ISystemCore>(netTcpBinding, primaryEndpoint); _core = primaryClient.CreateChannel(); (_core as IContextChannel).OperationTimeout = new TimeSpan(0, 0, 120); //Timeout=2m #endregion LoadEngine(service); service.Manager.ResetMaster(); LoggerCQ.LogInfo("Service started complete"); ConfigHelper.StartUp(); } catch (Exception ex) { LoggerCQ.LogError(ex); throw; } }
private static void Main(string[] args) { NLog.Targets.Target.Register <Logging.ExceptionalErrorStoreTarget>("ErrorStore"); LoggerCQ.LogInfo("Initializing Service..."); #if DEBUG LoggerCQ.LogInfo("(Debug Build)"); #endif //Try to connect to database and if successfull the assume service will start try { var installer = new DatabaseInstaller(); var connectionStringSettings = ConfigurationManager.ConnectionStrings["DatastoreEntities"]; //Just wait a few seconds to determine if the database is there var cb = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionStringSettings.ConnectionString); cb.ConnectTimeout = 12; var b = installer.NeedsUpdate(cb.ToString()); } catch (Exception ex) { LoggerCQ.LogError(ex, "Failed to connect to database."); throw new Exception("Failed to connect to database."); } LoggerCQ.LogInfo("Database connection verified."); if (args.Any(x => x == "-console" || x == "/console")) { try { var enableHouseKeeping = true; if (args.Any(x => x == "-nohousekeeping" || x == "/nohousekeeping")) { enableHouseKeeping = false; } var service = new PersistentService(enableHouseKeeping); service.Start(); Console.WriteLine("Press <ENTER> to stop..."); Console.ReadLine(); service.Cleanup(); service.Stop(); } catch (Exception ex) { LoggerCQ.LogError(ex, "Failed to start service from console."); throw; } } else { try { var servicesToRun = new ServiceBase[] { new PersistentService() }; ServiceBase.Run(servicesToRun); } catch (Exception ex) { LoggerCQ.LogError(ex, "Failed to start service."); } } }