public T Value()
        {
            T result;

            try
            {
                result = _Element.Value();

                // ReSharper disable once ConvertIfStatementToNullCoalescingExpression
                if (result == null)
                {
                    result = _GetDefaultValue();
                }
            }
            catch (ArgumentException)
            {
                result = _GetDefaultValue();
            }
            catch (InvalidCastException)
            {
                result = _GetDefaultValue();
            }

            assume(result != null);
            return(result);
        }
Пример #2
0
        public async Task SendEmailMessageAsync(ISmtpEmailMessage emailMessage)
        {
            var configuration = _Configuration.Value();

            if (configuration == null || !configuration.IsValid())
            {
                throw new InvalidOperationException("Smtp configuration not present or valid");
            }

            assume(configuration.Server?.Address != null);
            assume(configuration.Authentication != null);

            MailMessage mm = emailMessage.CreateFrameworkMailMessage();

            using (_Logger.LogScope(
                       LogLevel.Debug,
                       $"Sending email with subject '{mm.Subject}' from '{mm.From}' to '{string.Join(", ", mm.To)}' via '{configuration.Server.Address}:{configuration.Server.Port}'")
                   )
            {
                using (var client = new System.Net.Mail.SmtpClient(configuration.Server.Address, configuration.Server.Port))
                {
                    client.Credentials = new NetworkCredential(
                        configuration.Authentication.Username, configuration.Authentication.Password);

                    var tcs = new TaskCompletionSource <bool>();

                    void onClientOnSendCompleted(object sender, AsyncCompletedEventArgs args)
                    {
                        assume(args != null);
                        if (args.Error != null)
                        {
                            tcs.SetException(args.Error);
                        }
                        else if (args.Cancelled)
                        {
                            tcs.SetCanceled();
                        }
                        else
                        {
                            tcs.SetResult(true);
                        }
                    }

                    client.SendCompleted += onClientOnSendCompleted;
                    client.SendAsync(mm, null);
                    await tcs.Task.NotNull();
                }
            }
        }
Пример #3
0
        public string Value()
        {
            var encryptedValue = _ConfigurationElement.Value();

            if (string.IsNullOrWhiteSpace(encryptedValue))
            {
                return(encryptedValue);
            }

            try
            {
                return(_DataProtection.Unprotect(encryptedValue, _PasswordName));
            }
            catch (DataProtectionException)
            {
                return(encryptedValue);
            }
        }