public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ParameterInfo            parameter = context.Parameter;
            EventHubTriggerAttribute attribute = parameter.GetCustomAttribute <EventHubTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            string resolvedEventHubName = _nameResolver.ResolveWholeString(attribute.EventHubName);

            string consumerGroup         = attribute.ConsumerGroup ?? EventHubConsumerClient.DefaultConsumerGroupName;
            string resolvedConsumerGroup = _nameResolver.ResolveWholeString(consumerGroup);

            if (!string.IsNullOrWhiteSpace(attribute.Connection))
            {
                var connection       = _nameResolver.ResolveWholeString(attribute.Connection);
                var connectionString = _config.GetConnectionStringOrSetting(connection);
                _options.Value.AddReceiver(resolvedEventHubName, connectionString);
            }

            var eventHostListener = _options.Value.GetEventProcessorHost(resolvedEventHubName, resolvedConsumerGroup);
            var checkpointStoreConnectionString = _options.Value.GetCheckpointStoreConnectionString(_config, resolvedEventHubName);

            Func <ListenerFactoryContext, bool, Task <IListener> > createListener =
                (factoryContext, singleDispatch) =>
            {
                var checkpointStore = new BlobsCheckpointStore(
                    new BlobContainerClient(checkpointStoreConnectionString, _options.Value.LeaseContainerName),
                    _options.Value.EventProcessorOptions.RetryOptions.ToRetryPolicy(),
                    factoryContext.Descriptor.Id,
                    _logger);

                IListener listener = new EventHubListener(
                    factoryContext.Descriptor.Id,
                    factoryContext.Executor,
                    eventHostListener,
                    singleDispatch,
                    () => _options.Value.GetEventHubConsumerClient(resolvedEventHubName, consumerGroup),
                    checkpointStore,
                    _options.Value,
                    _logger);
                return(Task.FromResult(listener));
            };

#pragma warning disable 618
            ITriggerBinding binding = BindingFactory.GetTriggerBinding(new EventHubTriggerBindingStrategy(), parameter, _converterManager, createListener);
#pragma warning restore 618
            return(Task.FromResult(binding));
        }
Example #2
0
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            ParameterInfo            parameter = context.Parameter;
            EventHubTriggerAttribute attribute = parameter.GetCustomAttribute <EventHubTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            string resolvedEventHubName = _nameResolver.ResolveWholeString(attribute.EventHubName);

            string consumerGroup         = attribute.ConsumerGroup ?? PartitionReceiver.DefaultConsumerGroupName;
            string resolvedConsumerGroup = _nameResolver.ResolveWholeString(consumerGroup);

            string connectionString = null;

            if (!string.IsNullOrWhiteSpace(attribute.Connection))
            {
                attribute.Connection = _nameResolver.ResolveWholeString(attribute.Connection);
                connectionString     = _config.GetConnectionStringOrSetting(attribute.Connection);
                _options.Value.AddReceiver(resolvedEventHubName, connectionString);
            }

            var eventHostListener = _options.Value.GetEventProcessorHost(_config, resolvedEventHubName, resolvedConsumerGroup);

            string storageConnectionString = _config.GetWebJobsConnectionString(ConnectionStringNames.Storage);

            Func <ListenerFactoryContext, bool, Task <IListener> > createListener =
                (factoryContext, singleDispatch) =>
            {
                IListener listener = new EventHubListener(
                    factoryContext.Descriptor.Id,
                    resolvedEventHubName,
                    resolvedConsumerGroup,
                    connectionString,
                    storageConnectionString,
                    factoryContext.Executor,
                    eventHostListener,
                    singleDispatch,
                    _options.Value,
                    _logger);
                return(Task.FromResult(listener));
            };

            ITriggerBinding binding = BindingFactory.GetTriggerBinding(new EventHubTriggerBindingStrategy(), parameter, _converterManager, createListener);

            return(Task.FromResult <ITriggerBinding>(binding));
        }
        public static void EventHubTriggerAttribute_ShouldHaveV1vsV2Differences()
        {
            var attribute = new EventHubTriggerAttribute("eventHub");

            var jObject = attribute.ToJObject();

#if NET46
            jObject.Should().HaveElement("path");
            jObject["path"].Should().Be("eventHub");
#else
            jObject.Should().HaveElement("eventHubName");
            jObject["eventHubName"].Should().Be("eventHub");
#endif
        }
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ParameterInfo            parameter = context.Parameter;
            EventHubTriggerAttribute attribute = parameter.GetCustomAttribute <EventHubTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            Func <ListenerFactoryContext, bool, Task <IListener> > createListener =
                (factoryContext, singleDispatch) =>
            {
                var options = _options.Value;
                if (singleDispatch && !options.IsSingleDispatchEnabled)
                {
                    throw new NotSupportedException("Binding to individual events is not supported. Please use batch processing by binding to an array instead.");
                }

                var checkpointStore = new BlobsCheckpointStore(
                    _clientFactory.GetCheckpointStoreClient(),
                    options.EventProcessorOptions.RetryOptions.ToRetryPolicy(),
                    factoryContext.Descriptor.Id,
                    _logger);

                IListener listener = new EventHubListener(
                    factoryContext.Descriptor.Id,
                    factoryContext.Executor,
                    _clientFactory.GetEventProcessorHost(attribute.EventHubName, attribute.Connection, attribute.ConsumerGroup),
                    singleDispatch,
                    _clientFactory.GetEventHubConsumerClient(attribute.EventHubName, attribute.Connection, attribute.ConsumerGroup),
                    checkpointStore,
                    options,
                    _logger);
                return(Task.FromResult(listener));
            };

#pragma warning disable 618
            ITriggerBinding binding = BindingFactory.GetTriggerBinding(new EventHubTriggerBindingStrategy(), parameter, _converterManager, createListener);
#pragma warning restore 618
            return(Task.FromResult(binding));
        }
        public Task <ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ParameterInfo            parameter = context.Parameter;
            EventHubTriggerAttribute attribute = parameter.GetCustomAttribute <EventHubTriggerAttribute>(inherit: false);

            if (attribute == null)
            {
                return(Task.FromResult <ITriggerBinding>(null));
            }

            Func <ListenerFactoryContext, bool, Task <IListener> > createListener =
                (factoryContext, singleDispatch) =>
            {
                var options         = _options.Value;
                var checkpointStore = new BlobCheckpointStoreInternal(
                    _clientFactory.GetCheckpointStoreClient(),
                    factoryContext.Descriptor.Id,
                    _loggerFactory.CreateLogger <BlobCheckpointStoreInternal>());

                IListener listener = new EventHubListener(
                    factoryContext.Descriptor.Id,
                    factoryContext.Executor,
                    _clientFactory.GetEventProcessorHost(attribute.EventHubName, attribute.Connection, attribute.ConsumerGroup, singleDispatch),
                    singleDispatch,
                    _clientFactory.GetEventHubConsumerClient(attribute.EventHubName, attribute.Connection, attribute.ConsumerGroup),
                    checkpointStore,
                    options,
                    _loggerFactory);
                return(Task.FromResult(listener));
            };

#pragma warning disable 618
            ITriggerBinding binding = BindingFactory.GetTriggerBinding(new EventHubTriggerBindingStrategy(), parameter, _converterManager, createListener);
#pragma warning restore 618
            ITriggerBinding eventHubTriggerBindingWrapper = new EventHubTriggerBindingWrapper(binding);
            return(Task.FromResult(eventHubTriggerBindingWrapper));
        }
Example #6
0
            public override Collection <Attribute> GetAttributes()
            {
                Collection <Attribute> attributes = new Collection <Attribute>();

                string eventHubName = Context.GetMetadataValue <string>("path");

                if (!string.IsNullOrEmpty(eventHubName))
                {
                    eventHubName = _nameResolver.ResolveWholeString(eventHubName);
                }

                string connectionString = Context.GetMetadataValue <string>("connection");

                if (!string.IsNullOrEmpty(connectionString))
                {
                    connectionString = _nameResolver.Resolve(connectionString);
                }

                if (Context.IsTrigger)
                {
                    var    attribute     = new EventHubTriggerAttribute(eventHubName);
                    string consumerGroup = Context.GetMetadataValue <string>("consumerGroup");
                    if (consumerGroup != null)
                    {
                        consumerGroup           = _nameResolver.ResolveWholeString(consumerGroup);
                        attribute.ConsumerGroup = consumerGroup;
                    }
                    attributes.Add(attribute);
                    _eventHubConfiguration.AddReceiver(eventHubName, connectionString);
                }
                else
                {
                    attributes.Add(new EventHubAttribute(eventHubName));

                    _eventHubConfiguration.AddSender(eventHubName, connectionString);
                }

                return(attributes);
            }