Ejemplo n.º 1
0
        public async Task RunNew()
        {
            var cache = new MessageHandlerRegistry(new Conventions());

            cache.RegisterHandler(typeof(StubMessageHandler));
            cache.RegisterHandler(typeof(StubTimeoutHandler));
            var stubMessage    = new StubMessage();
            var stubTimeout    = new StubTimeoutState();
            var messageHandler = cache.GetCachedHandlerForMessage <StubMessage>();
            var timeoutHandler = cache.GetCachedHandlerForMessage <StubTimeoutState>();

            await messageHandler.Invoke(stubMessage, null);

            await timeoutHandler.Invoke(stubTimeout, null);

            var startNew = Stopwatch.StartNew();

            for (var i = 0; i < 100000; i++)
            {
                await messageHandler.Invoke(stubMessage, null);

                await timeoutHandler.Invoke(stubTimeout, null);
            }
            startNew.Stop();
            Trace.WriteLine(startNew.ElapsedMilliseconds);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Scans the given types for types that are message handlers
        /// then uses the Configurer to configure them into the container as single call components,
        /// finally passing them to the bus as its MessageHandlerTypes.
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        ConfigUnicastBus ConfigureMessageHandlersIn(IEnumerable <Type> types)
        {
            var handlerRegistry = new MessageHandlerRegistry();
            var handlers        = new List <Type>();

            foreach (var t in types.Where(IsMessageHandler))
            {
                Configurer.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
                handlers.Add(t);
            }

            Configurer.RegisterSingleton <IMessageHandlerRegistry>(handlerRegistry);

            var availableDispatcherFactories = TypesToScan
                                               .Where(
                factory =>
                !factory.IsInterface && typeof(IMessageDispatcherFactory).IsAssignableFrom(factory))
                                               .ToList();

            var dispatcherMappings = GetDispatcherFactories(handlers, availableDispatcherFactories);

            //configure the message dispatcher for each handler
            busConfig.ConfigureProperty(b => b.MessageDispatcherMappings, dispatcherMappings);
            Configurer.ConfigureProperty <InvokeHandlersBehavior>(b => b.MessageDispatcherMappings, dispatcherMappings);

            availableDispatcherFactories.ToList().ForEach(factory => Configurer.ConfigureComponent(factory, DependencyLifecycle.InstancePerUnitOfWork));

            return(this);
        }
Ejemplo n.º 3
0
        public async Task ShouldIndicateWhetherAHandlerIsATimeoutHandler()
        {
            var registry = new MessageHandlerRegistry();

            registry.RegisterHandler(typeof(SagaWithTimeoutOfMessage));

            var handlers = registry.GetHandlersFor(typeof(MyMessage));

            Assert.AreEqual(2, handlers.Count);

            var timeoutHandler = handlers.SingleOrDefault(h => h.IsTimeoutHandler);

            Assert.NotNull(timeoutHandler, "Timeout handler should be marked as such");

            var timeoutInstance = new SagaWithTimeoutOfMessage();

            timeoutHandler.Instance = timeoutInstance;
            await timeoutHandler.Invoke(new MyMessage(), new TestableInvokeHandlerContext());

            Assert.True(timeoutInstance.TimeoutCalled);
            Assert.False(timeoutInstance.HandlerCalled);

            var regularHandler = handlers.SingleOrDefault(h => !h.IsTimeoutHandler);

            Assert.NotNull(regularHandler, "Regular handler should be marked as timeout handler");

            var regularInstance = new SagaWithTimeoutOfMessage();

            regularHandler.Instance = regularInstance;
            await regularHandler.Invoke(new MyMessage(), new TestableInvokeHandlerContext());

            Assert.False(regularInstance.TimeoutCalled);
            Assert.True(regularInstance.HandlerCalled);
        }
Ejemplo n.º 4
0
        protected void RegisterMessageHandlerType <T>() where T : new()
        {
// ReSharper disable HeapView.SlowDelegateCreation
            FuncBuilder.Register <T>(() => new T());
// ReSharper restore HeapView.SlowDelegateCreation

            handlerRegistry.RegisterHandler(typeof(T));
        }
Ejemplo n.º 5
0
        protected void RegisterMessageHandlerType <T>() where T : new()
        {
            FuncBuilder.Register <T>(() => new T());

            handlerRegistry.RegisterHandler(typeof(T));

            if (unicastBus.MessageDispatcherMappings == null)
            {
                unicastBus.MessageDispatcherMappings = new Dictionary <Type, Type>();
            }
        }
Ejemplo n.º 6
0
        public async Task Should_invoke_handle_method()
        {
            var cache = new MessageHandlerRegistry(new Conventions());

            cache.RegisterHandler(typeof(StubHandler));

            var handler = cache.GetCachedHandlerForMessage <StubMessage>();
            await handler.Invoke(new StubMessage(), null);

            Assert.IsTrue(((StubHandler)handler.Instance).HandleCalled);
        }
Ejemplo n.º 7
0
        public async Task Should_have_passed_through_correct_context()
        {
            var cache = new MessageHandlerRegistry(new Conventions());

            cache.RegisterHandler(typeof(StubHandler));

            var handler        = cache.GetCachedHandlerForMessage <StubTimeoutState>();
            var handlerContext = new TestableMessageHandlerContext();
            await handler.Invoke(new StubTimeoutState(), handlerContext);

            Assert.AreSame(handlerContext, ((StubHandler)handler.Instance).HandlerContext);
        }
Ejemplo n.º 8
0
        public async Task Should_invoke_timeout_method()
        {
            var cache = new MessageHandlerRegistry(new Conventions());

            cache.RegisterHandler(typeof(StubHandler));

            var handler        = cache.GetCachedHandlerForMessage <StubTimeoutState>();
            var handlerContext = new TestableMessageHandlerContext();
            await handler.Invoke(new StubTimeoutState(), handlerContext);

            Assert.IsTrue(((StubHandler)handler.Instance).TimeoutCalled);
        }
Ejemplo n.º 9
0
        public async Task Should_have_passed_through_correct_message()
        {
            var cache = new MessageHandlerRegistry(new Conventions());

            cache.RegisterHandler(typeof(StubHandler));

            var handler     = cache.GetCachedHandlerForMessage <StubMessage>();
            var stubMessage = new StubMessage();
            await handler.Invoke(stubMessage, null);

            Assert.AreEqual(stubMessage, ((StubHandler)handler.Instance).HandledMessage);
        }
        static void ConfigureMessageHandlersIn(FeatureConfigurationContext context, IEnumerable <Type> types)
        {
            var handlerRegistry = new MessageHandlerRegistry();

            foreach (var t in types.Where(IsMessageHandler))
            {
                context.Container.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
            }

            context.Container.RegisterSingleton(handlerRegistry);
        }
 public EndpointWithRegularHandler()
 {
     EndpointSetup <DefaultServer>(c =>
     {
         var registry = new MessageHandlerRegistry();
         registry.RegisterHandler(typeof(ManuallyRegisteredHandler));
         c.GetSettings().Set(registry);
         // the handler isn't registered for DI automatically
         c.RegisterComponents(components => components
                              .AddTransient <ManuallyRegisteredHandler>());
     })
     .ExcludeType <ManuallyRegisteredHandler>();
 }
        public async Task Should_have_passed_through_correct_state()
        {
            var cache = new MessageHandlerRegistry();

            cache.RegisterHandler(typeof(StubHandler));

            var stubState      = new StubTimeoutState();
            var handler        = cache.GetCachedHandlerForMessage <StubTimeoutState>();
            var handlerContext = new TestableMessageHandlerContext();
            await handler.Invoke(stubState, handlerContext);

            Assert.AreEqual(stubState, ((StubHandler)handler.Instance).HandledState);
        }
 public EndpointWithRegularHandler()
 {
     EndpointSetup <DefaultServer>(c =>
     {
         var registry = new MessageHandlerRegistry();
         registry.RegisterHandler(typeof(ManuallyRegisteredHandler));
         c.GetSettings().Set(registry);
         // the handler isn't registered for DI automatically
         c.RegisterComponents(components => components
                              .ConfigureComponent <ManuallyRegisteredHandler>(DependencyLifecycle.InstancePerCall));
     })
     .ExcludeType <ManuallyRegisteredHandler>();
 }
Ejemplo n.º 14
0
        static void RegisterMessageHandlers(MessageHandlerRegistry handlerRegistry, List <Type> orderedTypes, IServiceCollection container, ICollection <Type> availableTypes)
        {
            var types = new List <Type>(availableTypes);

            foreach (var t in orderedTypes)
            {
                types.Remove(t);
            }

            types.InsertRange(0, orderedTypes);

            foreach (var t in types.Where(IsMessageHandler))
            {
                container.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
            }

            container.AddSingleton(handlerRegistry);
        }
 public EndpointWithRegularHandler()
 {
     EndpointSetup <DefaultServer>(c =>
     {
         var registry = new MessageHandlerRegistry();
         registry.RegisterHandler(typeof(ManuallyRegisteredHandler));
         c.GetSettings().Set(registry);
         // the handler isn't registered for DI automatically
         c.RegisterComponents(components => components
                              .AddTransient <ManuallyRegisteredHandler>());
         c.OnEndpointSubscribed <Context>((t, ctx) =>
         {
             if (t.MessageType == typeof(SomeEvent).AssemblyQualifiedName)
             {
                 ctx.EventSubscribed = true;
             }
         });
     }, metadata => metadata.RegisterPublisherFor <SomeEvent>(AcceptanceTesting.Customization.Conventions.EndpointNamingConvention(typeof(EndpointWithRegularHandler))))
     .ExcludeType <ManuallyRegisteredHandler>();
 }
        static void ConfigureMessageHandlersIn(FeatureConfigurationContext context, IEnumerable<Type> types)
        {
            var handlerRegistry = new MessageHandlerRegistry(context.Settings.Get<Conventions>());

            foreach (var t in types.Where(IsMessageHandler))
            {
                context.Container.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
            }

            List<Action<IConfigureComponents>> propertiesToInject;
            if (context.Settings.TryGet("NServiceBus.HandlerProperties", out propertiesToInject))
            {
                foreach (var action in propertiesToInject)
                {
                    action(context.Container);
                }
            }

            context.Container.RegisterSingleton(handlerRegistry);
        }
Ejemplo n.º 17
0
        static void ConfigureMessageHandlersIn(FeatureConfigurationContext context, IEnumerable <Type> types)
        {
            var handlerRegistry = new MessageHandlerRegistry(context.Settings.Get <Conventions>());

            foreach (var t in types.Where(IsMessageHandler))
            {
                context.Container.ConfigureComponent(t, DependencyLifecycle.InstancePerUnitOfWork);
                handlerRegistry.RegisterHandler(t);
            }

            List <Action <IConfigureComponents> > propertiesToInject;

            if (context.Settings.TryGet("NServiceBus.HandlerProperties", out propertiesToInject))
            {
                foreach (var action in propertiesToInject)
                {
                    action(context.Container);
                }
            }

            context.Container.RegisterSingleton(handlerRegistry);
        }
Ejemplo n.º 18
0
        public void ShouldThrowIfUserTriesToBypassTheHandlerContext(Type handlerType)
        {
            var registry = new MessageHandlerRegistry(new Conventions());

            Assert.Throws <Exception>(() => registry.RegisterHandler(handlerType));
        }