コード例 #1
0
        public void exception_is_thrown()
        {
            RebusTransactionScopeUnitOfWork.Initialize(unitOfWorkFactory: null);
            var fakeMessageContext = new FakeMessageContext();

            var ex = Should.Throw <InvalidOperationException>(() => RebusTransactionScopeUnitOfWork.Create(fakeMessageContext));

            ex.Message.ShouldBe(
                "RebusTransactionScopeUnitOfWork has not been initialized! Please call RebusTransactionScopeUnitOfWork.Initialize(...) before using it.");
        }
コード例 #2
0
        private static IBus _ConfigureAndStartRebus(IHandlerActivator handlerActivator)
        {
            var rebusConfigurer = Configure.With(handlerActivator);

            var rebusInputQueue = _configuration["Rebus:InputQueueName"];
            var rebusTransport  = _configuration["Rebus:Transport"];

            switch (rebusTransport)
            {
#if NETFRAMEWORK
            case "MSMQ":
                rebusConfigurer
                .Transport(x => x.UseMsmq(rebusInputQueue))
                .Subscriptions(x => x.UseJsonFile($"{Path.GetTempPath()}\\emailmaker_msmq_subscriptions.json"))
                ;
                break;
#endif
            case "RabbitMQ":
                rebusConfigurer.Transport(x => x.UseRabbitMq(_configuration["Rebus:RabbitMQ:ConnectionString"], rebusInputQueue));
                break;

            default:
                throw new Exception($"Unknown rebus transport: {rebusTransport}");
            }

            var rebusUnitOfWorkMode = _configuration["Rebus:UnitOfWorkMode"];
            switch (rebusUnitOfWorkMode)
            {
            case "TransactionScopeUnitOfWork":
                RebusTransactionScopeUnitOfWork.Initialize(
                    unitOfWorkFactory: IoC.Resolve <IUnitOfWorkFactory>(),
                    isolationLevel: System.Transactions.IsolationLevel.ReadCommitted,
                    transactionScopeEnlistmentAction: null
                    );
                rebusConfigurer
                .Options(o =>
                {
                    o.EnableUnitOfWork(
                        RebusTransactionScopeUnitOfWork.Create,
                        RebusTransactionScopeUnitOfWork.Commit,
                        RebusTransactionScopeUnitOfWork.Rollback,
                        RebusTransactionScopeUnitOfWork.Cleanup
                        );
                })
                ;
                break;

            case "UnitOfWork":
                RebusUnitOfWork.Initialize(
                    unitOfWorkFactory: IoC.Resolve <IUnitOfWorkFactory>(),
                    isolationLevel: System.Data.IsolationLevel.ReadCommitted
                    );
                rebusConfigurer
                .Options(o =>
                {
                    o.EnableUnitOfWork(
                        RebusUnitOfWork.Create,
                        RebusUnitOfWork.Commit,
                        RebusUnitOfWork.Rollback,
                        RebusUnitOfWork.Cleanup
                        );
                })
                ; break;

            default:
                throw new Exception($"Unknown rebus unit of work mode: {rebusUnitOfWorkMode}");
            }

            var bus = rebusConfigurer.Start();
            bus.Subscribe <EmailEnqueuedToBeSentEventMessage>().Wait();
            return(bus);
        }