コード例 #1
0
ファイル: Program.cs プロジェクト: josh28/risen
        static void Main(string[] args)
        {
            ConsumerRegistry.Configure();

            var consumer            = ObjectFactory.GetInstance <IConsumer>();
            var messagesToWriteToDb = consumer.ConsumeLogMessages();
            var formatter           = new XmlMessageFormatter(new[] { typeof(LogMessage) });

            using (var connection = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]))
            {
                connection.Open();

                foreach (var messageToWrite in messagesToWriteToDb)
                {
                    messageToWrite.Formatter = formatter;
                    var message = (LogMessage)messageToWrite.Body;

                    using (var command = new SqlCommand("CreateLogMessage", connection)
                    {
                        CommandType = CommandType.StoredProcedure
                    })
                    {
                        command.Parameters.AddWithValue("@Category", message.Category);
                        command.Parameters.AddWithValue("@Severity", message.Severity);
                        command.Parameters.AddWithValue("@Description", message.Description);
                        command.Parameters.AddWithValue("@CreatedDate", message.CreatedDate);
                        command.Parameters.AddWithValue("@CreatedBy", message.CreatedBy);

                        command.ExecuteNonQuery();
                    }
                }
            }
        }
コード例 #2
0
        public void Can_create_descriptors()
        {
            var validConsumers = new List <Lazy <IConsumer, EventConsumerMetadata> >
            {
                CreateConsumerRegistration(new TestConsumer1()),
                CreateConsumerRegistration(new TestConsumer2()),
                CreateConsumerRegistration(new TestConsumer3())
            };

            var registry = new ConsumerRegistry(validConsumers);

            var descriptors = registry.GetConsumers(new AppStartedEvent());

            descriptors.Count().ShouldEqual(2, "AppStartedEvent count");

            descriptors = registry.GetConsumers(new AppInitScheduledTasksEvent());
            descriptors.Count().ShouldEqual(2, "AppInitScheduledTasksEvent count");

            descriptors = registry.GetConsumers(new CustomerRegisteredEvent());
            descriptors.Count().ShouldEqual(1);
            var descriptor = descriptors.First();

            descriptor.ContainerType.ShouldEqual(typeof(TestConsumer2));
            descriptor.FireForget.ShouldEqual(true);
            descriptor.IsAsync.ShouldEqual(false);
            descriptor.WithEnvelope.ShouldEqual(false);

            descriptors = registry.GetConsumers(new OrderPaidEvent(null));
            descriptors.Count().ShouldEqual(1);
            descriptor = descriptors.First();
            descriptor.ContainerType.ShouldEqual(typeof(TestConsumer3));
            descriptor.FireForget.ShouldEqual(true);
            descriptor.IsAsync.ShouldEqual(true);

            descriptors = registry.GetConsumers(new RowExportingEvent());
            descriptors.Count().ShouldEqual(1);
            descriptor = descriptors.First();
            descriptor.ContainerType.ShouldEqual(typeof(TestConsumer3));
            descriptor.FireForget.ShouldEqual(true);
            descriptor.IsAsync.ShouldEqual(false);
            descriptor.WithEnvelope.ShouldEqual(true);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: THoVer/khnum
        static async Task Main(string[] args)
        {
            var collection = new ServiceCollection();

            var registry = new ConsumerRegistry(collection);

            registry.Subscribe <SendTimeRequest, SendTimeRequestConsumer>("consumer-id-x");
            registry.Subscribe <SendTimeRequest, SendTimeRequestConsumer>("consumer-id-y");

            collection.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());

            collection.AddSingleton <ISerializer, Serializer>();
            collection.AddSingleton <IConsumerRegistry>(registry);
            collection.AddSingleton <IBus, PostgreSqlBus>();
            collection.AddSingleton(new PostgreSqlBusOptions
            {
                ConnectionString = "Server=127.0.0.1;Database=test;User Id=admin;Password=admin"
            });
            collection.AddSingleton <IPublisher, PostgreSqlPublisher>();

            var services = collection.BuildServiceProvider(true);

            //await StartConsumer(services).ConfigureAwait(false);

            while (true)
            {
                Console.WriteLine("Press ESC to exit or any other key to publish");
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Escape)
                {
                    break;
                }

                var publisher = services.GetRequiredService <IPublisher>();
                await publisher.PublishAsync(new SendTimeRequest { Time = DateTime.Now }).ConfigureAwait(false);
            }
        }