protected async Task StartPump(Func <MessageContext, Task> onMessage, Func <ErrorContext, Task <ErrorHandleResult> > onError, TransportTransactionMode transactionMode, Action <string, Exception> onCriticalError = null)
        {
            InputQueueName = GetTestName() + transactionMode;
            ErrorQueueName = $"{InputQueueName}.error";

            var endpointConfiguration = new EndpointConfiguration(InputQueueName);

            endpointConfiguration.SendFailedMessagesTo(ErrorQueueName);

            transportSettings = endpointConfiguration.GetSettings();


            var queueBindings = transportSettings.Get <QueueBindings>();

            queueBindings.BindReceiving(InputQueueName);
            queueBindings.BindSending(ErrorQueueName);

            Configurer = CreateConfigurer();

            var configuration = Configurer.Configure(transportSettings, transactionMode);

            TransportInfrastructure = configuration.TransportInfrastructure;

            IgnoreUnsupportedTransactionModes(transactionMode);
            IgnoreUnsupportedDeliveryConstraints();

            ReceiveInfrastructure = TransportInfrastructure.ConfigureReceiveInfrastructure();

            var queueCreator = ReceiveInfrastructure.QueueCreatorFactory();
            var userName     = GetUserName();
            await queueCreator.CreateQueueIfNecessary(queueBindings, userName);

            var result = await ReceiveInfrastructure.PreStartupCheck();

            if (result.Succeeded == false)
            {
                throw new Exception($"Pre start-up check failed: {result.ErrorMessage}");
            }

            await TransportInfrastructure.Start();

            SendInfrastructure = TransportInfrastructure.ConfigureSendInfrastructure();
            lazyDispatcher     = new Lazy <IDispatchMessages>(() => SendInfrastructure.DispatcherFactory());

            MessagePump = ReceiveInfrastructure.MessagePumpFactory();
            await MessagePump.Init(
                context =>
            {
                if (context.Headers.ContainsKey(TestIdHeaderName) && context.Headers[TestIdHeaderName] == testId)
                {
                    return(onMessage(context));
                }

                return(Task.FromResult(0));
            },
                context =>
            {
                if (context.Message.Headers.ContainsKey(TestIdHeaderName) && context.Message.Headers[TestIdHeaderName] == testId)
                {
                    return(onError(context));
                }

                return(Task.FromResult(ErrorHandleResult.Handled));
            },
                new FakeCriticalError(onCriticalError),
                new PushSettings(InputQueueName, ErrorQueueName, configuration.PurgeInputQueueOnStartup, transactionMode));

            result = await SendInfrastructure.PreStartupCheck();

            if (result.Succeeded == false)
            {
                throw new Exception($"Pre start-up check failed: {result.ErrorMessage}");
            }

            MessagePump.Start(configuration.PushRuntimeSettings);
        }
예제 #2
0
        protected async Task StartPump(Func <MessageContext, Task> onMessage, Func <ErrorContext, Task <ErrorHandleResult> > onError, TransportTransactionMode transactionMode, Action <string, Exception> onCriticalError = null)
        {
            InputQueueName = GetTestName() + transactionMode;
            ErrorQueueName = $"{InputQueueName}.error";

            transportSettings.Set("NServiceBus.Routing.EndpointName", InputQueueName);
            transportSettings.Set(new StartupDiagnosticEntries());

            var queueBindings = new QueueBindings();

            queueBindings.BindReceiving(InputQueueName);
            queueBindings.BindSending(ErrorQueueName);
            transportSettings.Set(ErrorQueueSettings.SettingsKey, ErrorQueueName);
            transportSettings.Set(queueBindings);

            transportSettings.Set(new EndpointInstances());

            Configurer = CreateConfigurer();

            var configuration = Configurer.Configure(transportSettings, transactionMode);

            TransportInfrastructure = configuration.TransportInfrastructure;

            IgnoreUnsupportedTransactionModes(transactionMode);
            IgnoreUnsupportedDeliveryConstraints();

            ReceiveInfrastructure = TransportInfrastructure.ConfigureReceiveInfrastructure();

            var queueCreator = ReceiveInfrastructure.QueueCreatorFactory();
            var userName     = GetUserName();
            await queueCreator.CreateQueueIfNecessary(queueBindings, userName);

            await TransportInfrastructure.Start();

            SendInfrastructure = TransportInfrastructure.ConfigureSendInfrastructure();
            lazyDispatcher     = new Lazy <IDispatchMessages>(() => SendInfrastructure.DispatcherFactory());

            MessagePump = ReceiveInfrastructure.MessagePumpFactory();
            await MessagePump.Init(
                context =>
            {
                if (context.Headers.ContainsKey(TestIdHeaderName) && context.Headers[TestIdHeaderName] == testId)
                {
                    return(onMessage(context));
                }

                return(Task.FromResult(0));
            },
                context =>
            {
                if (context.Message.Headers.ContainsKey(TestIdHeaderName) && context.Message.Headers[TestIdHeaderName] == testId)
                {
                    return(onError(context));
                }

                return(Task.FromResult(ErrorHandleResult.Handled));
            },
                new FakeCriticalError(onCriticalError),
                new PushSettings(InputQueueName, ErrorQueueName, configuration.PurgeInputQueueOnStartup, transactionMode));

            MessagePump.Start(configuration.PushRuntimeSettings);
        }