Ejemplo n.º 1
0
    static async Task AsyncMain()
    {
        BusConfiguration busConfiguration = new BusConfiguration();

        busConfiguration.EndpointName("Samples.ComplexSagaFindingLogic");
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new StartOrder
            {
                OrderId = "123"
            });

            await busContext.SendLocal(new StartOrder
            {
                OrderId = "456"
            });

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 2
0
    public static async Task Run(IBusContext bus)
    {
        Console.WriteLine("Press 's' to send a valid message");
        Console.WriteLine("Press 'e' to send a failed message");
        Console.WriteLine("Press any key to exit");

        while (true)
        {
            ConsoleKeyInfo key = Console.ReadKey();
            Console.WriteLine();
            Console.WriteLine();
            switch (key.Key)
            {
            case ConsoleKey.S:
                #region SendingSmall
                await bus.SendLocal(new CreateProductCommand
                {
                    ProductId   = "XJ128",
                    ProductName = "Milk",
                    ListPrice   = 4,
                    // 7MB. MSMQ should throw an exception, but it will not since the buffer will be compressed
                    // before it reaches MSMQ.
                    Image = new byte[1024 * 1024 * 7]
                });

                #endregion
                break;

            case ConsoleKey.E:
                try
                {
                    #region SendingLarge
                    await bus.SendLocal(new CreateProductCommand
                    {
                        ProductId   = "XJ128",
                        ProductName = "Really long product name",
                        ListPrice   = 15,
                        // 7MB. MSMQ should throw an exception, but it will not since the buffer will be compressed
                        // before it reaches MSMQ.
                        Image = new byte[1024 * 1024 * 7]
                    });

                    #endregion
                }
                catch
                {
                    //so the console keeps on running
                }
                break;

            default:
            {
                return;
            }
            }
        }
    }
Ejemplo n.º 3
0
    static async Task AsyncMain()
    {
        #region ContainerConfiguration

        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Castle");

        WindsorContainer container = new WindsorContainer();
        container.Register(Component.For <MyService>().Instance(new MyService()));

        busConfiguration.UseContainer <WindsorBuilder>(c => c.ExistingContainer(container));

        #endregion

        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");
        busConfiguration.EnableInstallers();

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 4
0
    static async Task AsyncMain()
    {
        #region self-hosting

        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.SelfHosting");
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            Console.WriteLine("\r\nBus created and configured; press any key to stop program\r\n");
            await busContext.SendLocal(new MyMessage());

            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }

        #endregion
    }
Ejemplo n.º 5
0
    static async Task AsyncMain()
    {
        #region ConfigureLogging
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Logging.Default");
        //Note that no config is required in version 5 and higher since logging is enabled by default
        #endregion
        busConfiguration.SendFailedMessagesTo("error");
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 6
0
    static async Task AsyncMain()
    {
        #region ContainerConfiguration
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Spring");

        GenericApplicationContext applicationContext = new GenericApplicationContext();
        applicationContext.ObjectFactory.RegisterSingleton("MyService", new MyService());
        busConfiguration.UseContainer <SpringBuilder>(c => c.ExistingApplicationContext(applicationContext));
        #endregion
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.UsePersistence <InMemoryPersistence>();

        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 7
0
    static async Task AsyncMain()
    {
        #region logging
        DefaultFactory defaultFactory = LogManager.Use <DefaultFactory>();
        defaultFactory.Level(LogLevel.Fatal);
        #endregion
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Logging.Default");
        busConfiguration.SendFailedMessagesTo("error");
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 8
0
    static async Task SendMessage(IBusContext busContext)
    {
        Message message = new Message();
        await busContext.SendLocal(message);

        Console.WriteLine();
        Console.WriteLine("Message sent");
    }
Ejemplo n.º 9
0
    async Task Invoke(IDictionary <string, object> environment)
    {
        string messageBody = await GetMessageBody(environment).ConfigureAwait(false);

        IDictionary <string, string[]> requestHeaders = (IDictionary <string, string[]>)environment["owin.RequestHeaders"];
        string typeName    = requestHeaders["MessageType"].Single();
        Type   objectType  = Type.GetType(typeName);
        object deserialize = Deserialize(messageBody, objectType);
        await bus.SendLocal(deserialize).ConfigureAwait(false);
    }
Ejemplo n.º 10
0
        public async Task RequestImmediateDispatchUsingScope()
        {
            IBusContext busContext = null;

            #region RequestImmediateDispatchUsingScope
            using (new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            {
                await busContext.SendLocal(new MyMessage());
            }
            #endregion
        }
Ejemplo n.º 11
0
    static async Task AsyncMain()
    {
        #region config
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Serialization.Xml");
        // this is optional since Xml is the default serializer
        busConfiguration.UseSerialization <XmlSerializer>();
        // register the mutator so the the message on the wire is written
        busConfiguration.RegisterComponents(components =>
        {
            components.ConfigureComponent <MessageBodyWriter>(DependencyLifecycle.InstancePerCall);
        });
        #endregion
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.EnableInstallers();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            #region message
            CreateOrder message = new CreateOrder
            {
                OrderId    = 9,
                Date       = DateTime.Now,
                CustomerId = 12,
                OrderItems = new List <OrderItem>
                {
                    new OrderItem
                    {
                        ItemId   = 6,
                        Quantity = 2
                    },
                    new OrderItem
                    {
                        ItemId   = 5,
                        Quantity = 4
                    },
                }
            };
            await busContext.SendLocal(message);

            #endregion
            Console.WriteLine("Order Sent");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 12
0
    static async Task AsyncMain()
    {
        #region ConfigureLog4Net
        PatternLayout layout = new PatternLayout
        {
            ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n"
        };
        layout.ActivateOptions();
        ConsoleAppender consoleAppender = new ConsoleAppender
        {
            Threshold = Level.Info,
            Layout    = layout
        };
        // Note that ActivateOptions is required in NSB 5 and above
        consoleAppender.ActivateOptions();
        BasicConfigurator.Configure(consoleAppender);
        #endregion

        #region UseConfig

        LogManager.Use <Log4NetFactory>();

        // Then continue with your bus configuration
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Logging.Log4NetCustom");

        #endregion

        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 13
0
    static async Task AsyncRun()
    {
        BusConfiguration busConfiguration = new BusConfiguration();

        busConfiguration.EndpointName("Samples.Headers");

        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        busConfiguration.RegisterComponents(components =>
        {
            components.ConfigureComponent <MutateIncomingMessages>(DependencyLifecycle.InstancePerCall);
            components.ConfigureComponent <MutateIncomingTransportMessages>(DependencyLifecycle.InstancePerCall);
            components.ConfigureComponent <MutateOutgoingMessages>(DependencyLifecycle.InstancePerCall);
            components.ConfigureComponent <MutateOutgoingTransportMessages>(DependencyLifecycle.InstancePerCall);
        });

        #region global-all-outgoing

        busConfiguration.AddHeaderToAllOutgoingMessages("AllOutgoing", "ValueAllOutgoing");

        #endregion
        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();


            #region sending

            MyMessage myMessage = new MyMessage();
            await busContext.SendLocal(myMessage);

            #endregion

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 14
0
    static async Task AsyncMain()
    {
        #region ConfigureNLog

        LoggingConfiguration config = new LoggingConfiguration();

        ColoredConsoleTarget consoleTarget = new ColoredConsoleTarget
        {
            Layout = "${level}|${logger}|${message}${onexception:${newline}${exception:format=tostring}}"
        };
        config.AddTarget("console", consoleTarget);
        config.LoggingRules.Add(new LoggingRule("*", LogLevel.Info, consoleTarget));

        LogManager.Configuration = config;
        #endregion
        #region UseConfig

        NServiceBus.Logging.LogManager.Use <NLogFactory>();

        // Then continue with your BusConfiguration
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Logging.NLogCustom");

        #endregion

        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 15
0
    static async Task AsyncMain()
    {
        BusConfiguration busConfiguration = new BusConfiguration();

        busConfiguration.EndpointName("Samples.PerfCounters");
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        #region enable-counters
        busConfiguration.EnableCriticalTimePerformanceCounter();
        busConfiguration.EnableSLAPerformanceCounter(TimeSpan.FromSeconds(100));
        #endregion

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();

            Console.WriteLine("Press enter to send 10 messages with random sleep");
            Console.WriteLine("Press any key to exit");

            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                Console.WriteLine();

                if (key.Key != ConsoleKey.Enter)
                {
                    break;
                }
                for (int i = 0; i < 10; i++)
                {
                    await busContext.SendLocal(new MyMessage());
                }
            }
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 16
0
    static async Task AsyncMain()
    {
        LogManager.Use <DefaultFactory>()
        .Level(LogLevel.Warn);

        #region DisableSLR
        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.ErrorHandling.WithoutSLR");
        busConfiguration.DisableFeature <SecondLevelRetries>();
        #endregion
        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.EnableInstallers();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            Console.WriteLine("Press enter to send a message that will throw an exception.");
            Console.WriteLine("Press any key to exit");

            while (true)
            {
                ConsoleKeyInfo key = Console.ReadKey();
                if (key.Key != ConsoleKey.Enter)
                {
                    return;
                }
                MyMessage m = new MyMessage
                {
                    Id = Guid.NewGuid()
                };
                await busContext.SendLocal(m);
            }
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 17
0
    static async Task AsyncMain()
    {
        #region ConfigureLogging

        Common.Logging.LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter
        {
            Level = LogLevel.Info
        };

        NServiceBus.Logging.LogManager.Use <CommonLoggingFactory>();

        BusConfiguration busConfiguration = new BusConfiguration();
        busConfiguration.EndpointName("Samples.Logging.CommonLogging");

        #endregion

        busConfiguration.UseSerialization <JsonSerializer>();
        busConfiguration.EnableInstallers();
        busConfiguration.UsePersistence <InMemoryPersistence>();
        busConfiguration.SendFailedMessagesTo("error");

        IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

        try
        {
            IBusContext busContext = endpoint.CreateBusContext();
            await busContext.SendLocal(new MyMessage());

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
        finally
        {
            await endpoint.Stop();
        }
    }
Ejemplo n.º 18
0
    static async Task AsyncMain()
    {
        using (new RavenHost())
        {
            BusConfiguration busConfiguration = new BusConfiguration();
            busConfiguration.EndpointName("Samples.UoWWithChildContainers");
            busConfiguration.SendFailedMessagesTo("error");

            #region ContainerConfiguration

            DocumentStore documentStore = new DocumentStore
            {
                Url             = "http://localhost:32076",
                DefaultDatabase = "NServiceBus"
            };
            documentStore.Initialize();

            Container container = new Container(x =>
            {
                x.For <IDocumentStore>().Use(documentStore);
                x.For <IDocumentSession>().Use(c => c.GetInstance <IDocumentStore>().OpenSession());
            });

            busConfiguration.UseContainer <StructureMapBuilder>(c => c.ExistingContainer(container));

            #endregion

            #region PipelineRegistration

            busConfiguration.Pipeline.Register <RavenUnitOfWork.Registration>();

            #endregion

            busConfiguration.UsePersistence <InMemoryPersistence>();
            busConfiguration.EnableInstallers();

            int orderNumber = 1;

            IEndpointInstance endpoint = await Endpoint.Start(busConfiguration);

            try
            {
                IBusContext busContext = endpoint.CreateBusContext();
                Console.WriteLine("Press enter to send a message");
                Console.WriteLine("Press any key to exit");
                Console.WriteLine("After storing a few orders you can open a browser and view them at http://localhost:32076");

                while (true)
                {
                    ConsoleKeyInfo key = Console.ReadKey();
                    Console.WriteLine();

                    if (key.Key != ConsoleKey.Enter)
                    {
                        return;
                    }
                    await busContext.SendLocal(new PlaceOrder
                    {
                        OrderNumber = $"Order-{orderNumber}",
                        OrderValue  = 100
                    });

                    orderNumber++;
                }
            }
            finally
            {
                await endpoint.Stop();
            }
        }
    }
Ejemplo n.º 19
0
 public async Task Start(IBusContext context)
 {
     await context.SendLocal(new MyMessage());
 }