コード例 #1
0
ファイル: ConfigData.cs プロジェクト: sundarnut/CorpMailman
        /// <summary>
        /// Private constructor, since this is a singleton class
        /// </summary>
        private ConfigData()
        {
            this.LogLevel = LogLevels.Invalid;

            // Construct ThreadLock instance - default to true to make first caller run!
            this.threadLock = new ThreadLock(true);

            // Construct TestDataStrings instance
            this.testData = new TestDataStrings();

            // Set default configData duration to 900000d = 900 seconds, 15 minutes
            this.Duration = 900000d;

            // Parse TimerDuration from config, if defined
            ulong confDuration;
            var timerDuration = ConfigurationManager.AppSettings[Constants.Configuration.TimerDuration];

            if (ulong.TryParse(timerDuration, out confDuration)) {
                this.Duration = confDuration * Constants.Thousand;
            } else {
                throw new ConfigurationErrorsException(string.Format(Constants.Messages.MissingOrInvalidTimerDuration, timerDuration ?? Constants.Null));
            }

            // Parse logVerbose from config, if defined
            LogLevels logLevel;
            var logVerbose = ConfigurationManager.AppSettings[Constants.Configuration.LogLevel];

            if (!string.IsNullOrEmpty(logVerbose) && (Enum.TryParse(logVerbose, out logLevel))) {
                this.LogLevel = logLevel;
            }

            // Fail if no LogLevel specified in config
            if (this.LogLevel == LogLevels.Invalid) {
                throw new ConfigurationErrorsException(string.Format(Constants.Messages.MissingLogLevel, logVerbose ?? Constants.Empty));
            }

            // Get run once setting from config
            bool runOnce;
            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.RunOnce], out runOnce)) {
                this.RunOnce = runOnce;
            }

            // Get force interval setting from config
            bool forceInterval;
            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.ForceInterval], out forceInterval)) {
                this.ForceInterval = forceInterval;
            } else {
                // Default should be true
                this.ForceInterval = true;
            }

            // Get debug-test run one-time setting from config, default is false
            bool debugTestRunOneTime;
            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.DebugTestRunOneTime], out debugTestRunOneTime)) {
                this.DebugTestRunOneTime = debugTestRunOneTime;
            }

            // Get the StartAt value from config - the service would fail if this value occurs in the past for the current day
            var startAtParameter = ConfigurationManager.AppSettings[Constants.Configuration.StartAt];

            // Only proceed if the value is provided, or is not set to "[Now]" - both seem to be valid cases and we don't explicitly enforce a StartAt = "Now" consideration for configuration
            if (!string.IsNullOrEmpty(startAtParameter)) {

                // Check if this value is not equal to "[Now]"
                if (!startAtParameter.Equals(Constants.StartNow, StringComparison.InvariantCultureIgnoreCase)) {

                    DateTime startTimeValue;

                    var now = DateTime.UtcNow;
                    var cultureInfo = CultureInfo.InvariantCulture;

                    // Try and parse the date time value from configuration as a full date-time value
                    if (DateTime.TryParseExact(startAtParameter, Constants.FullDateTimeFormat, cultureInfo, DateTimeStyles.None, out startTimeValue)) {

                        // Set the startAt parameter to specified time
                        this.StartAt = startTimeValue;
                    } else if (DateTime.TryParseExact(startAtParameter, Constants.TimeFormat, cultureInfo, DateTimeStyles.None, out startTimeValue)) {

                        this.StartAt = startTimeValue;

                        // Add another day if this date-time occurs in the past
                        if (now > startTimeValue) {
                            this.StartAt = startTimeValue.AddDays(1);
                        }
                    } else {
                        // Fail as you couldn't parse the datetime as specified in configuration!
                        throw new ConfigurationErrorsException(string.Format(Constants.Messages.StartTimeIncorrect, startAtParameter));
                    }
                }
            }

            // MySQL Connection string part!
            // Get domain from config
            var mySqlEndpoint = ConfigurationManager.AppSettings[Constants.Configuration.Database.Endpoint];
            var mySqlDatabase = ConfigurationManager.AppSettings[Constants.Configuration.Database.DB];
            var mySqlLogin = ConfigurationManager.AppSettings[Constants.Configuration.Database.Login];
            var mySqlPassword = ConfigurationManager.AppSettings[Constants.Configuration.Database.Password];

            var builder = null as StringBuilder;

            foreach (var parameter in new string[] {
                        mySqlEndpoint,
                        mySqlDatabase,
                        mySqlLogin,
                        mySqlPassword
                    }) {
                // Fail if no parameter is found for this key
                if (string.IsNullOrEmpty(parameter)) {
                    // Construct StringBuilder for the first time
                    if (builder == null) {
                        builder = new StringBuilder();
                    }

                    if (builder.Length > 0) {
                        builder.Append(Constants.CommaSpace);
                    }

                    builder.Append(parameter);
                }
            }

            if (builder != null) {

                var builderMessage = builder.ToString();
                builder.Remove(0, builder.Length);

                throw new ConfigurationErrorsException(string.Format(Constants.Messages.Database.ConfigurationParameterMissing, builderMessage));
            }

            this.ConnectionString = string.Format(Constants.DatabaseConnectionString, mySqlEndpoint, mySqlDatabase, mySqlLogin, mySqlPassword);

            // Mail Credentials
            this.MailCredentials = ConfigurationManager.AppSettings[Constants.Configuration.Email.MailCredentials];

            if (string.IsNullOrEmpty(this.MailCredentials)) {
                // Fail as you can't work without SendAsName
                throw new ConfigurationErrorsException(Constants.Messages.MailCredentialsMissing);
            }

            // Mail Credentials, this can be null if we choose to employ the current user
            this.SendAsName = ConfigurationManager.AppSettings[Constants.Configuration.Email.SendAsName];

            // Login and password for a service account
            this.SamAccountName = ConfigurationManager.AppSettings[Constants.Configuration.Email.SamAccountName];
            this.SamAccountPassword = ConfigurationManager.AppSettings[Constants.Configuration.Email.SamAccountPassword];

            // TestData - read only if you want to first time test run
            if (this.DebugTestRunOneTime) {

                this.testData.From = ConfigurationManager.AppSettings[Constants.Configuration.TestData.From];
                this.testData.To = ConfigurationManager.AppSettings[Constants.Configuration.TestData.To];
                this.testData.Cc = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Cc];
                this.testData.Bcc = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Bcc];
                this.testData.SubjectPrefix = ConfigurationManager.AppSettings[Constants.Configuration.TestData.SubjectPrefix];
                this.testData.Subject = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Subject];
                this.testData.Body = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Body];

                // Get hasAttachments run one-time setting from config, default is false
                bool testDataHasAttachments;
                if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.TestData.HasAttachments], out testDataHasAttachments)) {
                    this.testData.HasAttachments = testDataHasAttachments;
                }

                // Only if you have attachments
                if (this.testData.HasAttachments) {

                    if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.Configuration.TestData.Filename)) {
                        this.testData.Filename = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Filename];
                    }

                    if (string.IsNullOrEmpty(this.TestData.Filename)) {
                        throw new ConfigurationErrorsException(Constants.Messages.MissingFilenameSettingMessage);
                    }

                    var counter = 0u;

                    builder = new StringBuilder();

                    var nextKey = null as string;

                    do {
                        nextKey = string.Format("{0}{1}", Constants.Configuration.TestData.Buffer, counter);

                        if (ConfigurationManager.AppSettings.AllKeys.Contains(nextKey)) {
                            builder.Append(ConfigurationManager.AppSettings[nextKey]);
                            counter++;
                        }
                    } while (ConfigurationManager.AppSettings.AllKeys.Contains(nextKey));

                    if (counter > 0) {
                        this.TestData.Attachment = builder.ToString();
                        builder.Remove(0, builder.Length);
                    } else {
                        throw new ConfigurationErrorsException(Constants.Messages.MissingAttachmentDataMessage);
                    }
                }
            }
        }
コード例 #2
0
ファイル: ConfigData.cs プロジェクト: sundarnut/CorpMailman
        /// <summary>
        /// Private constructor, since this is a singleton class
        /// </summary>
        private ConfigData()
        {
            this.LogLevel = LogLevels.Invalid;

            // Construct ThreadLock instance - default to true to make first caller run!
            this.threadLock = new ThreadLock(true);

            // Construct TestDataStrings instance
            this.testData = new TestDataStrings();

            // Set default configData duration to 900000d = 900 seconds, 15 minutes
            this.Duration = 900000d;

            // Parse TimerDuration from config, if defined
            ulong confDuration;
            var   timerDuration = ConfigurationManager.AppSettings[Constants.Configuration.TimerDuration];

            if (ulong.TryParse(timerDuration, out confDuration))
            {
                this.Duration = confDuration * Constants.Thousand;
            }
            else
            {
                throw new ConfigurationErrorsException(string.Format(Constants.Messages.MissingOrInvalidTimerDuration, timerDuration ?? Constants.Null));
            }

            // Parse logVerbose from config, if defined
            LogLevels logLevel;
            var       logVerbose = ConfigurationManager.AppSettings[Constants.Configuration.LogLevel];

            if (!string.IsNullOrEmpty(logVerbose) && (Enum.TryParse(logVerbose, out logLevel)))
            {
                this.LogLevel = logLevel;
            }

            // Fail if no LogLevel specified in config
            if (this.LogLevel == LogLevels.Invalid)
            {
                throw new ConfigurationErrorsException(string.Format(Constants.Messages.MissingLogLevel, logVerbose ?? Constants.Empty));
            }

            // Get run once setting from config
            bool runOnce;

            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.RunOnce], out runOnce))
            {
                this.RunOnce = runOnce;
            }

            // Get force interval setting from config
            bool forceInterval;

            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.ForceInterval], out forceInterval))
            {
                this.ForceInterval = forceInterval;
            }
            else
            {
                // Default should be true
                this.ForceInterval = true;
            }

            // Get debug-test run one-time setting from config, default is false
            bool debugTestRunOneTime;

            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.DebugTestRunOneTime], out debugTestRunOneTime))
            {
                this.DebugTestRunOneTime = debugTestRunOneTime;
            }

            // Get the StartAt value from config - the service would fail if this value occurs in the past for the current day
            var startAtParameter = ConfigurationManager.AppSettings[Constants.Configuration.StartAt];

            // Only proceed if the value is provided, or is not set to "[Now]" - both seem to be valid cases and we don't explicitly enforce a StartAt = "Now" consideration for configuration
            if (!string.IsNullOrEmpty(startAtParameter))
            {
                // Check if this value is not equal to "[Now]"
                if (!startAtParameter.Equals(Constants.StartNow, StringComparison.InvariantCultureIgnoreCase))
                {
                    DateTime startTimeValue;

                    var now         = DateTime.UtcNow;
                    var cultureInfo = CultureInfo.InvariantCulture;

                    // Try and parse the date time value from configuration as a full date-time value
                    if (DateTime.TryParseExact(startAtParameter, Constants.FullDateTimeFormat, cultureInfo, DateTimeStyles.None, out startTimeValue))
                    {
                        // Set the startAt parameter to specified time
                        this.StartAt = startTimeValue;
                    }
                    else if (DateTime.TryParseExact(startAtParameter, Constants.TimeFormat, cultureInfo, DateTimeStyles.None, out startTimeValue))
                    {
                        this.StartAt = startTimeValue;

                        // Add another day if this date-time occurs in the past
                        if (now > startTimeValue)
                        {
                            this.StartAt = startTimeValue.AddDays(1);
                        }
                    }
                    else
                    {
                        // Fail as you couldn't parse the datetime as specified in configuration!
                        throw new ConfigurationErrorsException(string.Format(Constants.Messages.StartTimeIncorrect, startAtParameter));
                    }
                }
            }

            bool useEws;

            if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.UseEws], out useEws))
            {
                this.UseEws = useEws;
            }

            this.EwsUrl = ConfigurationManager.AppSettings[Constants.Configuration.EwsUrl];

            if (!this.UseEws)
            {
                this.SmtpServer = ConfigurationManager.AppSettings[Constants.Configuration.SmtpServer];

                if (string.IsNullOrEmpty(this.SmtpServer))
                {
                    throw new ConfigurationErrorsException(Constants.Messages.MissingSmtpServerMessage);
                }
            }

            // MySQL Connection string part!
            // Get domain from config
            var mySqlEndpoint = ConfigurationManager.AppSettings[Constants.Configuration.Database.Endpoint];
            var mySqlDatabase = ConfigurationManager.AppSettings[Constants.Configuration.Database.DB];
            var mySqlLogin    = ConfigurationManager.AppSettings[Constants.Configuration.Database.Login];
            var mySqlPassword = ConfigurationManager.AppSettings[Constants.Configuration.Database.Password];

            var builder = null as StringBuilder;

            foreach (var parameter in new string[] {
                mySqlEndpoint,
                mySqlDatabase,
                mySqlLogin,
                mySqlPassword
            })
            {
                // Fail if no parameter is found for this key
                if (string.IsNullOrEmpty(parameter))
                {
                    // Construct StringBuilder for the first time
                    if (builder == null)
                    {
                        builder = new StringBuilder();
                    }

                    if (builder.Length > 0)
                    {
                        builder.Append(Constants.CommaSpace);
                    }

                    builder.Append(parameter);
                }
            }

            if (builder != null)
            {
                var builderMessage = builder.ToString();
                builder.Remove(0, builder.Length);

                throw new ConfigurationErrorsException(string.Format(Constants.Messages.Database.ConfigurationParameterMissing, builderMessage));
            }

            this.ConnectionString = string.Format(Constants.DatabaseConnectionString, mySqlEndpoint, mySqlDatabase, mySqlLogin, mySqlPassword);

            // Mail Credentials
            this.MailCredentials = ConfigurationManager.AppSettings[Constants.Configuration.Email.MailCredentials];

            if (string.IsNullOrEmpty(this.MailCredentials))
            {
                // Fail as you can't work without SendAsName
                throw new ConfigurationErrorsException(Constants.Messages.MailCredentialsMissing);
            }

            // Mail Credentials, this can be null if we choose to employ the current user
            this.SendAsName = ConfigurationManager.AppSettings[Constants.Configuration.Email.SendAsName];

            // Login and password for a service account
            this.SamAccountName     = ConfigurationManager.AppSettings[Constants.Configuration.Email.SamAccountName];
            this.SamAccountPassword = ConfigurationManager.AppSettings[Constants.Configuration.Email.SamAccountPassword];

            // TestData - read only if you want to first time test run
            if (this.DebugTestRunOneTime)
            {
                this.testData.From          = ConfigurationManager.AppSettings[Constants.Configuration.TestData.From];
                this.testData.To            = ConfigurationManager.AppSettings[Constants.Configuration.TestData.To];
                this.testData.Cc            = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Cc];
                this.testData.Bcc           = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Bcc];
                this.testData.SubjectPrefix = ConfigurationManager.AppSettings[Constants.Configuration.TestData.SubjectPrefix];
                this.testData.Subject       = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Subject];
                this.testData.Body          = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Body];

                // Get hasAttachments run one-time setting from config, default is false
                bool testDataHasAttachments;
                if (bool.TryParse(ConfigurationManager.AppSettings[Constants.Configuration.TestData.HasAttachments], out testDataHasAttachments))
                {
                    this.testData.HasAttachments = testDataHasAttachments;
                }

                // Only if you have attachments
                if (this.testData.HasAttachments)
                {
                    if (ConfigurationManager.AppSettings.AllKeys.Contains(Constants.Configuration.TestData.Filename))
                    {
                        this.testData.Filename = ConfigurationManager.AppSettings[Constants.Configuration.TestData.Filename];
                    }

                    if (string.IsNullOrEmpty(this.TestData.Filename))
                    {
                        throw new ConfigurationErrorsException(Constants.Messages.MissingFilenameSettingMessage);
                    }

                    var counter = 0u;

                    builder = new StringBuilder();

                    var nextKey = null as string;

                    do
                    {
                        nextKey = string.Format("{0}{1}", Constants.Configuration.TestData.Buffer, counter);

                        if (ConfigurationManager.AppSettings.AllKeys.Contains(nextKey))
                        {
                            builder.Append(ConfigurationManager.AppSettings[nextKey]);
                            counter++;
                        }
                    } while (ConfigurationManager.AppSettings.AllKeys.Contains(nextKey));

                    if (counter > 0)
                    {
                        this.TestData.Attachment = builder.ToString();
                        builder.Remove(0, builder.Length);
                    }
                    else
                    {
                        throw new ConfigurationErrorsException(Constants.Messages.MissingAttachmentDataMessage);
                    }
                }
            }
        }