public void ApplyConfigurationSection_CreatesExpectedOptions()
        {
            var options = new SendGridOptions();
            var dict    = new Dictionary <string, string>();
            var builder = new ConfigurationBuilder();

            builder.AddInMemoryCollection(dict);
            var config = builder.Build();

            SendGridHelpers.ApplyConfiguration(config, options);
            Assert.Null(options.FromAddress);
            Assert.Null(options.ToAddress);

            dict = new Dictionary <string, string>
            {
                { "to", "Testing1 <*****@*****.**>" },
                { "from", "Testing2 <*****@*****.**>" },
            };
            builder = new ConfigurationBuilder();
            builder.AddInMemoryCollection(dict);
            config = builder.Build();

            SendGridHelpers.ApplyConfiguration(config, options);

            Assert.Equal("*****@*****.**", options.ToAddress.Email);
            Assert.Equal("Testing1", options.ToAddress.Name);
            Assert.Equal("*****@*****.**", options.FromAddress.Email);
            Assert.Equal("Testing2", options.FromAddress.Name);
        }
        /// <summary>
        /// Adds the SendGrid extension to the provided <see cref="IWebJobsBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IWebJobsBuilder"/> to configure.</param>
        public static IWebJobsBuilder AddSendGrid(this IWebJobsBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddExtension <SendGridExtensionConfigProvider>()
            .ConfigureOptions <SendGridOptions>((section, options) =>
            {
                SendGridHelpers.ApplyConfiguration(section, options);
            });

            builder.Services.AddSingleton <ISendGridClientFactory, DefaultSendGridClientFactory>();

            return(builder);
        }
        /// <summary>
        /// Adds the SendGrid extension to the provided <see cref="IWebJobsBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IWebJobsBuilder"/> to configure.</param>
        public static IWebJobsBuilder AddSendGrid(this IWebJobsBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            builder.AddExtension <SendGridExtensionConfigProvider>()
            .ConfigureOptions <SendGridOptions>((rootConfig, extensionPath, options) =>
            {
                // Set the default, which can be overridden.
                options.ApiKey = rootConfig[SendGridExtensionConfigProvider.AzureWebJobsSendGridApiKeyName];

                IConfigurationSection section = rootConfig.GetSection(extensionPath);
                SendGridHelpers.ApplyConfiguration(section, options);
            });

            builder.Services.AddSingleton <ISendGridClientFactory, DefaultSendGridClientFactory>();

            return(builder);
        }