AllReady.NotificationsWebJob.EnvironmentHelper.TryGetEnvironmentVariable C# (CSharp) Method

TryGetEnvironmentVariable() public static method

public static TryGetEnvironmentVariable ( string variable ) : string
variable string
return string
        public static string TryGetEnvironmentVariable(string variable)
        {
            var candidate = Environment.GetEnvironmentVariable(variable, EnvironmentVariableTarget.User);
            if (!string.IsNullOrEmpty(candidate))
            {
                return candidate;
            }

            candidate = Environment.GetEnvironmentVariable(variable);
            if (string.IsNullOrWhiteSpace(candidate))
            {
                throw new Exception($"Can't find `{variable}` environment variable.");
            }

            return candidate;
        }
    }

Usage Example

Exemplo n.º 1
0
        public static async Task ProcessEmailQueueMessage([QueueTrigger("email-pending-deliveries")] string message, TextWriter log)
        {
            var emailMessage = JsonConvert.DeserializeObject <QueuedEmailMessage>(message);

            // Create the email object first, then add the properties.
            var from  = GuardAgainstInvalidEmailAddress(EnvironmentHelper.TryGetEnvironmentVariable("Authentication:SendGrid:FromEmail"));
            var email = new SendGridMessage();

            email.AddTo(emailMessage.Recipient);
            email.From    = new MailAddress(from, "AllReady");
            email.Subject = emailMessage.Subject;
            email.Html    = emailMessage.HtmlMessage;
            email.Text    = emailMessage.Message;

            // Create credentials, specifying your user name and password.
            var username    = EnvironmentHelper.TryGetEnvironmentVariable("Authentication:SendGrid:UserName");
            var password    = EnvironmentHelper.TryGetEnvironmentVariable("Authentication:SendGrid:Password");
            var credentials = new NetworkCredential(username, password);

            // Create an Web transport for sending email, using credentials...
            var transportWeb = new Web(credentials);
            await transportWeb.DeliverAsync(email);

            await log.WriteLineAsync($"Sent email with subject `{email.Subject}` to `{emailMessage.Recipient}`");
        }
All Usage Examples Of AllReady.NotificationsWebJob.EnvironmentHelper::TryGetEnvironmentVariable
EnvironmentHelper