public SendGridBinding(ParameterInfo parameter, SendGridAttribute attribute, SendGridConfiguration config, BindingProviderContext context)
            {
                _parameter = parameter;
                _attribute = attribute;
                _config = config;

                _sendGrid = new Web(_config.ApiKey);

                if (!string.IsNullOrEmpty(_attribute.To))
                {
                    _toFieldBinding = new BindablePath(_attribute.To);
                    _toFieldBinding.ValidateContractCompatibility(context.BindingDataContract);
                }

                if (!string.IsNullOrEmpty(_attribute.Subject))
                {
                    _subjectFieldBinding = new BindablePath(_attribute.Subject);
                    _subjectFieldBinding.ValidateContractCompatibility(context.BindingDataContract);
                }

                if (!string.IsNullOrEmpty(_attribute.Text))
                {
                    _textFieldBinding = new BindablePath(_attribute.Text);
                    _textFieldBinding.ValidateContractCompatibility(context.BindingDataContract);
                }
            }
        /// <summary>
        /// Enables use of the SendGrid extensions
        /// </summary>
        /// <param name="config">The <see cref="JobHostConfiguration"/> to configure.</param>
        /// <param name="sendGridConfig">The <see cref="SendGridConfiguration"/> to use.</param>
        public static void UseSendGrid(this JobHostConfiguration config, SendGridConfiguration sendGridConfig = null)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (sendGridConfig == null)
            {
                sendGridConfig = new SendGridConfiguration();
            }

            config.RegisterExtensionConfigProvider(new SendGridExtensionConfig(sendGridConfig));
        }
        public ErrorNotifier(SendGridConfiguration sendGridConfig)
        {
            if (sendGridConfig == null)
            {
                throw new ArgumentNullException("sendGridConfig");
            }
            _sendGridConfig = sendGridConfig;
            _sendGrid = new Web(sendGridConfig.ApiKey);

            // pull our IFTTT notification URL from app settings (since it contains a secret key)
            _webNotificationUri = ConfigurationManager.AppSettings.Get(NotificationUriSettingName);
            if (string.IsNullOrEmpty(_webNotificationUri))
            {
                _webNotificationUri = Environment.GetEnvironmentVariable(NotificationUriSettingName);
            }
        }
        /// <summary>
        /// Set up monitoring + notifications for WebJob errors. This shows how to set things up
        /// manually on startup. You can also use <see cref="ErrorTriggerAttribute"/> to designate
        /// error handler functions.
        /// </summary>
        private static void ConfigureTraceMonitor(JobHostConfiguration config, SendGridConfiguration sendGridConfiguration)
        {
            var notifier = new ErrorNotifier(sendGridConfiguration);

            var traceMonitor = new TraceMonitor()
                .Filter(new SlidingWindowTraceFilter(TimeSpan.FromMinutes(5), 3))
                .Filter(p =>
                {
                    FunctionInvocationException functionException = p.Exception as FunctionInvocationException;
                    return p.Level == TraceLevel.Error && functionException != null &&
                           functionException.MethodName == "ExtensionsSample.FileSamples.ImportFile";
                }, "ImportFile Job Failed")
                .Subscribe(notifier.WebNotify, notifier.EmailNotify)
                .Throttle(TimeSpan.FromMinutes(30));

            config.Tracing.Tracers.Add(traceMonitor);
        }
        public static void Main(string[] args)
        {
            JobHostConfiguration config = new JobHostConfiguration();

            config.Tracing.ConsoleLevel = TraceLevel.Verbose;

            // Set to a short polling interval to facilitate local
            // debugging. You wouldn't want to run prod this way.
            config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(2);

            FilesConfiguration filesConfig = new FilesConfiguration();
            if (string.IsNullOrEmpty(filesConfig.RootPath))
            {
                // when running locally, set this to a valid directory
                filesConfig.RootPath = @"c:\temp\files";
            }
            EnsureSampleDirectoriesExist(filesConfig.RootPath);
            config.UseFiles(filesConfig);

            config.UseTimers();
            config.UseSample();
            config.UseCore();
            var sendGridConfiguration = new SendGridConfiguration()
            {
                ToAddress = "*****@*****.**",
                FromAddress = new MailAddress("*****@*****.**", "WebJobs Extensions Samples")
            };
            config.UseSendGrid(sendGridConfiguration);

            ConfigureTraceMonitor(config, sendGridConfiguration);

            WebHooksConfiguration webHooksConfig = new WebHooksConfiguration();
            webHooksConfig.UseReceiver<GitHubWebHookReceiver>();
            config.UseWebHooks(webHooksConfig);

            JobHost host = new JobHost(config);

            host.Call(typeof(MiscellaneousSamples).GetMethod("ExecutionContext"));
            host.Call(typeof(FileSamples).GetMethod("ReadWrite"));
            host.Call(typeof(SampleSamples).GetMethod("Sample_BindToStream"));
            host.Call(typeof(SampleSamples).GetMethod("Sample_BindToString"));
            host.Call(typeof(TableSamples).GetMethod("CustomBinding"));

            host.RunAndBlock();
        }
        public void CreateDefaultMessage_CreatesExpectedMessage()
        {
            ParameterInfo parameter = GetType().GetMethod("TestMethod", BindingFlags.Static | BindingFlags.NonPublic).GetParameters().First();
            SendGridAttribute attribute = new SendGridAttribute
            {
                To = "{Param1}",
                Subject = "Test {Param2}",
                Text = "Test {Param3}"
            };
            SendGridConfiguration config = new SendGridConfiguration
            {
                ApiKey = "12345",
                FromAddress = new MailAddress("*****@*****.**", "Test2"),
                ToAddress = "*****@*****.**"
            };
            Dictionary<string, Type> contract = new Dictionary<string, Type>();
            contract.Add("Param1", typeof(string));
            contract.Add("Param2", typeof(string));
            contract.Add("Param3", typeof(string));
            BindingProviderContext context = new BindingProviderContext(parameter, contract, CancellationToken.None);

            SendGridBinding binding = new SendGridBinding(parameter, attribute, config, context);
            Dictionary<string, object> bindingData = new Dictionary<string, object>();
            bindingData.Add("Param1", "*****@*****.**");
            bindingData.Add("Param2", "Value2");
            bindingData.Add("Param3", "Value3");
            SendGridMessage message = binding.CreateDefaultMessage(bindingData);

            Assert.Same(config.FromAddress, message.From);
            Assert.Equal("*****@*****.**", message.To.Single().Address);
            Assert.Equal("Test Value2", message.Subject);
            Assert.Equal("Test Value3", message.Text);

            // If no To value specified, verify it is defaulted from config
            attribute = new SendGridAttribute
            {
                Subject = "Test {Param2}",
                Text = "Test {Param3}"
            };
            binding = new SendGridBinding(parameter, attribute, config, context);
            message = binding.CreateDefaultMessage(bindingData);
            Assert.Equal("*****@*****.**", message.To.Single().Address);
        }
 public SendGridAttributeBindingProvider(SendGridConfiguration config)
 {
     _config = config;
 }
 public SendGridExtensionConfig(SendGridConfiguration sendGridConfig)
 {
     _sendGridConfig = sendGridConfig;
 }