コード例 #1
0
        static void SendAutoDistributeDemo()
        {
            // demonstrate distributing messages evenly across queues
            // each call to Send chooses the next queue to send the message to

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineWriteQueue("MiniBus.messages1")
                       .DefineWriteQueue("MiniBus.messages2")
                       .DefineWriteQueue("MiniBus.messages3")
                       .CreateLocalQueuesAutomatically()
                       .AutoDistributeOnSend()
                       .JsonSerialization()
                       .CreateBus();

            var p = new Person {
                Name = "Bob", Age = 20
            };

            bus.Send(p); // MiniBus.messages1
            bus.Send(p); // MiniBus.messages2
            bus.Send(p); // MiniBus.messages3
            bus.Send(p); // MiniBus.messages1

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
コード例 #2
0
        static void SendDemo()
        {
            // demonstrate sending message to defined write queue

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineWriteQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .CreateBus();


            // transaction scope isn't required unless modifying another transactional resource at the same time
            using (var scope = new TransactionScope())
            {
                for (int i = 1; i <= 10; i++)
                {
                    var p = new Person {
                        Name = "Bob", Age = i
                    };

                    // insert/update a database to see atomic commit

                    bus.Send(p);
                }
                scope.Complete();
            }

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
コード例 #3
0
        static void ReceiveDemo()
        {
            // demonstrate receiving - Receive processes whatever messages happen to be on the queue at the time of the call

            Console.WriteLine("\nPress a key to read message/s");
            Console.ReadLine();

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineReadQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .NumberOfRetries(3)
                       .CreateBus();

            // pass true to have a message fail and moved to the error queue
            bus.RegisterHandler(new PersonHandler(false));

            bus.Receive <Person>();

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
コード例 #4
0
        static void ReturnErrorMessagesDemo()
        {
            // demonstrate moving messages from error queue to read queue

            IBus bus = new BusBuilder()
                       .WithLogging(new FileLogger())
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineReadQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .CreateBus();

            bus.ReturnErrorMessages();

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
コード例 #5
0
        static void ReceiveDemoAsync()
        {
            // demonstrate receiving asynchrnously - ReceiveAsync processes messages as and when they arrive.
            // Open another instance to send messages while the other receives.

            IBus bus = new BusBuilder()
                       .WithLogging(new ConsoleLogger())
                       .InstallMsmqIfNeeded()
                       .DefineErrorQueue("MiniBus.errors")
                       .DefineReadQueue("MiniBus.messages1")
                       .CreateLocalQueuesAutomatically()
                       .EnlistInAmbientTransactions()
                       .JsonSerialization()
                       .NumberOfRetries(3)
                       .CreateBus();

            bus.RegisterHandler(new PersonHandler(false));
            bus.ReceiveAsync <Person>();

            Console.WriteLine("\nPress a key to exit");
            Console.ReadLine();
            bus.Dispose();
        }
コード例 #6
0
        public async Task TheStartupTimeShouldBeAcceptable()
        {
            const int numMessageTypes = 50;

            var assemblyBuilder = EmitMessageContractsAndHandlersAssembly(numMessageTypes);

            var logger       = TestHarnessLoggerFactory.Create(Guid.NewGuid(), GetType().FullName);
            var typeProvider = new AssemblyScanningTypeProvider(assemblyBuilder);

            var firstBus = new BusBuilder().Configure()
                           .WithNames("MyTestSuite", Environment.MachineName)
                           .WithDefaults(typeProvider)
                           .WithTransport(new WindowsServiceBusTransportConfiguration()
                                          .WithConnectionString(DefaultSettingsReader.Get <AzureServiceBusConnectionString>()))
                           .WithLogger(logger)
                           .WithDebugOptions(
                dc =>
                dc.RemoveAllExistingNamespaceElementsOnStartup(
                    "I understand this will delete EVERYTHING in my namespace. I promise to only use this for test suites."))
                           .Build();

            try
            {
                try
                {
                    await firstBus.Start(MessagePumpTypes.All);
                }
                catch (AggregateException exc)
                {
                    throw exc.Flatten();
                }
            }
            finally
            {
                firstBus.Dispose();
            }

            var subsequentBus = new BusBuilder().Configure()
                                .WithNames("MyTestSuite", Environment.MachineName)
                                .WithTransport(new WindowsServiceBusTransportConfiguration()
                                               .WithConnectionString(DefaultSettingsReader.Get <AzureServiceBusConnectionString>()))
                                .WithDefaults(typeProvider)
                                .WithLogger(logger)
                                .Build();

            try
            {
                try
                {
                    await subsequentBus.Start(MessagePumpTypes.All);
                }
                catch (AggregateException exc)
                {
                    throw exc.Flatten();
                }
            }
            finally
            {
                subsequentBus.Dispose();
            }
        }
コード例 #7
0
ファイル: WhenCreatingABigBus.cs プロジェクト: teyc/Nimbus_v2
        public async Task TheStartupTimeShouldBeAcceptable()
        {
            const int numMessageTypes = 100;

            var assemblyBuilder = EmitMessageContractsAndHandlersAssembly(numMessageTypes);

            var logger       = TestHarnessLoggerFactory.Create();
            var typeProvider = new AssemblyScanningTypeProvider(new[] { assemblyBuilder });

            var firstBus = new BusBuilder().Configure()
                           .WithNames("MyTestSuite", Environment.MachineName)
                           .WithConnectionString(CommonResources.ServiceBusConnectionString)
                           .WithTypesFrom(typeProvider)
                           .WithDefaultTimeout(TimeSpan.FromSeconds(10))
                           .WithServerConnectionCount(100)
                           .WithLogger(logger)
                           .Build();

            try
            {
                using (new AssertingStopwatch("First bus startup", TimeSpan.FromMinutes(1)))
                {
                    {
                        try
                        {
                            await firstBus.Start(MessagePumpTypes.All);

                            WriteBlankLines();
                        }
                        catch (AggregateException exc)
                        {
                            throw exc.Flatten();
                        }
                    }
                }
            }
            finally
            {
                WriteBlankLines();
                firstBus.Dispose();
            }

            WriteBlankLines();

            var subsequentBus = new BusBuilder().Configure()
                                .WithNames("MyTestSuite", Environment.MachineName)
                                .WithConnectionString(CommonResources.ServiceBusConnectionString)
                                .WithTypesFrom(typeProvider)
                                .WithDefaultTimeout(TimeSpan.FromSeconds(10))
                                .WithLogger(logger)
                                .Build();

            try
            {
                using (new AssertingStopwatch("Subsequent bus startup", TimeSpan.FromSeconds(20)))
                {
                    try
                    {
                        await subsequentBus.Start(MessagePumpTypes.All);

                        WriteBlankLines();
                    }
                    catch (AggregateException exc)
                    {
                        throw exc.Flatten();
                    }
                }
            }
            finally
            {
                WriteBlankLines();
                subsequentBus.Dispose();
            }
        }