Ejemplo n.º 1
0
        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }



            context
            .AddConverter <Message, string>(new MessageToStringConverter())
            .AddConverter <Message, byte[]>(new MessageToByteArrayConverter())
            .AddOpenConverter <Message, OpenType.Poco>(typeof(MessageToPocoConverter <>));

            // register our trigger binding provider
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = new ServiceBusTriggerAttributeBindingProvider(_nameResolver, _options, _messagingProvider, _configuration, _loggerFactory, _converterManager);

            context.AddBindingRule <ServiceBusTriggerAttribute>()
            .BindToTrigger(triggerBindingProvider);

            // register our binding provider
            ServiceBusAttributeBindingProvider bindingProvider = new ServiceBusAttributeBindingProvider(_nameResolver, _options, _configuration, _messagingProvider);

            context.AddBindingRule <ServiceBusAttribute>().Bind(bindingProvider);
        }
Ejemplo n.º 2
0
        public ServiceBusTriggerAttributeBindingProviderTests()
        {
            _configuration = new ConfigurationBuilder()
                             .AddEnvironmentVariables()
                             .AddTestSettings()
                             .Build();

            // Add all test configuration to the environment as WebJobs requires a few of them to be in the environment
            foreach (var kv in _configuration.AsEnumerable())
            {
                Environment.SetEnvironmentVariable(kv.Key, kv.Value);
            }

            Mock <INameResolver> mockResolver = new Mock <INameResolver>(MockBehavior.Strict);

            _connectionString = _configuration.GetConnectionStringOrSetting(ServiceBus.Constants.DefaultConnectionStringName);

            _options = new ServiceBusOptions()
            {
                ConnectionString = _connectionString
            };
            _mockMessagingProvider = new Mock <MessagingProvider>(MockBehavior.Strict, new OptionsWrapper <ServiceBusOptions>(_options));

            Mock <IConverterManager> convertManager = new Mock <IConverterManager>(MockBehavior.Default);

            _provider = new ServiceBusTriggerAttributeBindingProvider(mockResolver.Object, _options, _mockMessagingProvider.Object, _configuration, NullLoggerFactory.Instance, convertManager.Object);

            _loggerFactory  = new LoggerFactory();
            _loggerProvider = new TestLoggerProvider();
            _loggerFactory.AddProvider(_loggerProvider);
        }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // Set the default exception handler for background exceptions
            // coming from MessageReceivers.
            Options.ExceptionHandler = (e) =>
            {
                LogExceptionReceivedEvent(e, _loggerFactory);
                return(Task.CompletedTask);
            };

            context
            .AddConverter(new MessageToStringConverter())
            .AddConverter(new MessageToByteArrayConverter())
            .AddOpenConverter <ServiceBusReceivedMessage, OpenType.Poco>(typeof(MessageToPocoConverter <>), _options.JsonSerializerSettings);

            // register our trigger binding provider
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = new ServiceBusTriggerAttributeBindingProvider(_nameResolver, _options, _messagingProvider, _loggerFactory, _converterManager, _clientFactory);

            context.AddBindingRule <ServiceBusTriggerAttribute>()
            .BindToTrigger(triggerBindingProvider);

            // register our binding provider
            ServiceBusAttributeBindingProvider bindingProvider = new ServiceBusAttributeBindingProvider(_nameResolver, _messagingProvider, _clientFactory);

            context.AddBindingRule <ServiceBusAttribute>().Bind(bindingProvider);
        }
Ejemplo n.º 4
0
        public async Task Initialize_PerformsExpectedRegistrations()
        {
            JobHostConfiguration config = new JobHostConfiguration();

            config.AddService <INameResolver>(new RandomNameResolver());

            TestLoggerProvider loggerProvider = new TestLoggerProvider();
            ILoggerFactory     loggerFactory  = new LoggerFactory();

            loggerFactory.AddProvider(loggerProvider);
            config.LoggerFactory = loggerFactory;

            ServiceBusConfiguration   serviceBusConfig          = new ServiceBusConfiguration();
            ServiceBusExtensionConfig serviceBusExtensionConfig = new ServiceBusExtensionConfig(serviceBusConfig);

            IExtensionRegistry extensions = config.GetService <IExtensionRegistry>();

            ITriggerBindingProvider[] triggerBindingProviders = extensions.GetExtensions <ITriggerBindingProvider>().ToArray();
            Assert.Empty(triggerBindingProviders);
            IBindingProvider[] bindingProviders = extensions.GetExtensions <IBindingProvider>().ToArray();
            Assert.Empty(bindingProviders);

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
            };

            serviceBusExtensionConfig.Initialize(context);

            // ensure the ServiceBusTriggerAttributeBindingProvider was registered
            triggerBindingProviders = extensions.GetExtensions <ITriggerBindingProvider>().ToArray();
            Assert.Single(triggerBindingProviders);
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = (ServiceBusTriggerAttributeBindingProvider)triggerBindingProviders[0];

            Assert.NotNull(triggerBindingProvider);

            // ensure the ServiceBusAttributeBindingProvider was registered
            bindingProviders = extensions.GetExtensions <IBindingProvider>().ToArray();
            Assert.Single(bindingProviders);
            ServiceBusAttributeBindingProvider bindingProvider = (ServiceBusAttributeBindingProvider)bindingProviders[0];

            Assert.NotNull(bindingProvider);

            // ensure the default MessageOptions exception handler is wired up
            var messageOptions = serviceBusConfig.MessageOptions;
            var ex             = new ServiceBusException(false);
            var args           = new ExceptionReceivedEventArgs(ex, "TestAction", "TestEndpoint", "TestEntityPath", "TestClientId");

            Assert.NotNull(serviceBusConfig.ExceptionHandler);

            // invoke the handler and make sure it logs
            await serviceBusConfig.MessageOptions.ExceptionReceivedHandler(args);

            string expectedMessage = "MessageReceiver error (Action=TestAction, ClientId=TestClientId, EntityPath=TestEntityPath, Endpoint=TestEndpoint)";
            var    logMessage      = loggerProvider.GetAllLogMessages().Single();

            Assert.Equal(LogLevel.Error, logMessage.Level);
            Assert.Equal(expectedMessage, logMessage.FormattedMessage);
            Assert.Same(ex, logMessage.Exception);
        }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Set the default exception handler for background exceptions
            // coming from MessageReceivers.
            Config.ExceptionHandler = (e) =>
            {
                LogExceptionReceivedEvent(e, context.Config.LoggerFactory);
            };

            // get the services we need to construct our binding providers
            INameResolver      nameResolver = context.Config.GetService <INameResolver>();
            IExtensionRegistry extensions   = context.Config.GetService <IExtensionRegistry>();

            // register our trigger binding provider
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = new ServiceBusTriggerAttributeBindingProvider(nameResolver, _serviceBusConfig);

            extensions.RegisterExtension <ITriggerBindingProvider>(triggerBindingProvider);

            // register our binding provider
            ServiceBusAttributeBindingProvider bindingProvider = new ServiceBusAttributeBindingProvider(nameResolver, _serviceBusConfig);

            extensions.RegisterExtension <IBindingProvider>(bindingProvider);
        }
        public ServiceBusTriggerAttributeBindingProviderTests()
        {
            Mock <INameResolver> mockResolver = new Mock <INameResolver>(MockBehavior.Strict);

            ServiceBusConfiguration config = new ServiceBusConfiguration();

            _mockMessagingProvider = new Mock <MessagingProvider>(MockBehavior.Strict, config);

            config.MessagingProvider = _mockMessagingProvider.Object;
            _provider = new ServiceBusTriggerAttributeBindingProvider(mockResolver.Object, config);
        }
Ejemplo n.º 7
0
        public ServiceBusTriggerAttributeBindingProviderTests()
        {
            _configuration = new ConfigurationBuilder()
                             .AddEnvironmentVariables()
                             .Build();
            Mock <INameResolver> mockResolver = new Mock <INameResolver>(MockBehavior.Strict);

            ServiceBusOptions config = new ServiceBusOptions();

            _mockMessagingProvider = new Mock <MessagingProvider>(MockBehavior.Strict, new OptionsWrapper <ServiceBusOptions>(config));

            _provider = new ServiceBusTriggerAttributeBindingProvider(mockResolver.Object, config, _mockMessagingProvider.Object, _configuration);
        }
        public ServiceBusTriggerAttributeBindingProviderTests()
        {
            var configuration = ConfigurationUtilities.CreateConfiguration(new KeyValuePair <string, string>(Constants.DefaultConnectionStringName, "defaultConnection"));

            Mock <INameResolver> mockResolver = new Mock <INameResolver>(MockBehavior.Strict);

            ServiceBusOptions options = new ServiceBusOptions();

            Mock <IConverterManager> convertManager = new Mock <IConverterManager>(MockBehavior.Default);
            var provider = new MessagingProvider(new OptionsWrapper <ServiceBusOptions>(options));
            var factory  = new ServiceBusClientFactory(configuration, new Mock <AzureComponentFactory>().Object, provider);

            _provider = new ServiceBusTriggerAttributeBindingProvider(mockResolver.Object, options, provider, NullLoggerFactory.Instance, convertManager.Object, factory);
        }
Ejemplo n.º 9
0
        public void LogExceptionReceivedEvent_NonMessagingException_LoggedAsError()
        {
            var ex = new MissingMethodException("What method??");
            ExceptionReceivedEventArgs e = new ExceptionReceivedEventArgs(ex, "TestAction", "TestEndpoint", "TestEntity", "TestClient");

            ServiceBusTriggerAttributeBindingProvider.LogExceptionReceivedEvent(e, "TestFunction", _loggerFactory);

            var expectedMessage = $"Message processing error (Action=TestAction, ClientId=TestClient, EntityPath=TestEntity, Endpoint=TestEndpoint)";
            var logMessage      = _loggerProvider.GetAllLogMessages().Single();

            Assert.Equal(LogLevel.Error, logMessage.Level);
            Assert.Same(ex, logMessage.Exception);
            Assert.Equal(expectedMessage, logMessage.FormattedMessage);
            Assert.True(logMessage.Category.Contains("TestFunction"));
        }
Ejemplo n.º 10
0
        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // get the services we need to construct our binding providers
            INameResolver      nameResolver = context.Config.GetService <INameResolver>();
            IExtensionRegistry extensions   = context.Config.GetService <IExtensionRegistry>();

            // register our trigger binding provider
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = new ServiceBusTriggerAttributeBindingProvider(nameResolver, _serviceBusConfig);

            extensions.RegisterExtension <ITriggerBindingProvider>(triggerBindingProvider);
        }
Ejemplo n.º 11
0
        public void Initialize_PerformsExpectedRegistrations()
        {
            JobHostConfiguration config = new JobHostConfiguration();

            config.AddService <INameResolver>(new RandomNameResolver());

            ServiceBusConfiguration   serviceBusConfig          = new ServiceBusConfiguration();
            ServiceBusExtensionConfig serviceBusExtensionConfig = new ServiceBusExtensionConfig(serviceBusConfig);

            IExtensionRegistry extensions = config.GetService <IExtensionRegistry>();

            ITriggerBindingProvider[] triggerBindingProviders = extensions.GetExtensions <ITriggerBindingProvider>().ToArray();
            Assert.Equal(0, triggerBindingProviders.Length);
            IBindingProvider[] bindingProviders = extensions.GetExtensions <IBindingProvider>().ToArray();
            Assert.Equal(0, bindingProviders.Length);

            ExtensionConfigContext context = new ExtensionConfigContext
            {
                Config = config,
                Trace  = new TestTraceWriter(TraceLevel.Verbose)
            };

            serviceBusExtensionConfig.Initialize(context);

            // ensure the ServiceBusTriggerAttributeBindingProvider was registered
            triggerBindingProviders = extensions.GetExtensions <ITriggerBindingProvider>().ToArray();
            Assert.Equal(1, triggerBindingProviders.Length);
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = (ServiceBusTriggerAttributeBindingProvider)triggerBindingProviders[0];

            Assert.NotNull(triggerBindingProvider);

            // ensure the ServiceBusAttributeBindingProvider was registered
            bindingProviders = extensions.GetExtensions <IBindingProvider>().ToArray();
            Assert.Equal(1, bindingProviders.Length);
            ServiceBusAttributeBindingProvider bindingProvider = (ServiceBusAttributeBindingProvider)bindingProviders[0];

            Assert.NotNull(bindingProvider);
        }
Ejemplo n.º 12
0
        /// <inheritdoc />
        public void Initialize(ExtensionConfigContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            // Set the default exception handler for background exceptions
            // coming from MessageReceivers.
            Options.ExceptionHandler = (e) =>
            {
                LogExceptionReceivedEvent(e, _loggerFactory);
            };

            // register our trigger binding provider
            ServiceBusTriggerAttributeBindingProvider triggerBindingProvider = new ServiceBusTriggerAttributeBindingProvider(_nameResolver, _options, _messagingProvider, _configuration);

            context.AddBindingRule <ServiceBusTriggerAttribute>().BindToTrigger(triggerBindingProvider);

            // register our binding provider
            ServiceBusAttributeBindingProvider bindingProvider = new ServiceBusAttributeBindingProvider(_nameResolver, _options, _configuration, _messagingProvider);

            context.AddBindingRule <ServiceBusAttribute>().Bind(bindingProvider);
        }