protected override void OnStart(string[] args) { // TODO: add startup stuff for running as service if (!MyLogger.checkLogFile()) { Environment.Exit(1); //not sure if this works } MyLogger.writeSingleLine("Program started in service mode"); MyConfig conf = new MyConfig(@".\TicketsFromEmail365.cfg"); if (conf.Error != "") { MyLogger.writeSingleLine(conf.Error); MyLogger.writeSingleLine("Terminating"); Environment.Exit(1); //not sure if this works } MyLogger.writeSingleLine(@"Succesfully Read Config File: .\TicketsFromEmail365.cfg"); if (!MyTicketWorker.TestDatabaseConnection(conf)) { MyLogger.writeSingleLine("Error testing database connection"); MyLogger.writeSingleLine("Terminating"); Environment.Exit(1); //not sure if this works } MyLogger.writeSingleLine(@"Succesfully tested database connection."); MyEwsWorker worker = new MyEwsWorker(conf); //Not sure if something is needed here to keep service running. Or will the worker object keep the service alive? if (worker.Error == "") { //Do stuff to keep the service running??? } }
public static bool TestDatabaseConnection(MyConfig currentConfig) { //This should take in a config and test the connection to the database string connectionString = BuildConnectionString(currentConfig); using (SqlConnection connection = new SqlConnection(connectionString)) { try { connection.Open(); return true; } catch (SqlException) { return false; } } }
public MyTicketWorker(EmailMessage message, MyConfig config) { _message = message; _config = config; SubjectHasTicketNumber(); }
private static string BuildConnectionString(MyConfig config) { //This should safely build the appropriate SQL connection string using the config file SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder(); builder["Data Source"] = config.DbServer + "," + config.DbPort; builder["integrated Security"] = false; builder.UserID = config.UserDb; builder["Password"] = config.PasswordDb; builder["Initial Catalog"] = config.DbName; return builder.ConnectionString; }