Example #1
0
        /// <summary>
        /// Add an <see cref="ElasticSearchMessageQueue{TMessage}"/> to the specified <see cref="IServiceCollection"/>
        /// </summary>
        /// <typeparam name="TMessage"></typeparam>
        /// <param name="services"></param>
        /// <param name="configureOptions"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public static IServiceCollection AddElasticSearchMessageQueue <TMessage>(this IServiceCollection services, Action <IServiceProvider, ElasticSearchMessageQueueOptions <TMessage> > configureOptions)
        {
            if (services is null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (configureOptions is null)
            {
                throw new ArgumentNullException(nameof(configureOptions));
            }

            return(services
                   .AddMessageQueue <ElasticSearchMessageQueue <TMessage>, TMessage>(services =>
            {
                var options = new ElasticSearchMessageQueueOptions <TMessage>();
                configureOptions(services, options);

                var logger = services.GetRequiredService <ILogger <ElasticSearchMessageQueue <TMessage> > >();
                return new ElasticSearchMessageQueue <TMessage>(logger, Options.Options.Create(options));
            }));
        }
Example #2
0
        public static async Task Main(string[] _)
        {
            //elasticsearch setup example
            var elasticSearchOptions =
                new ElasticSearchMessageQueueOptions <MyMessage>()
                .UseConnectionUri(new Uri("YOUR URI HERE"),
                                  (options) =>
            {
                options.ThrowExceptions();
            }
                                  );
            var elasticSearchQueue = new ElasticSearchMessageQueue <MyMessage>(new Logger <ElasticSearchMessageQueue <MyMessage> >(), Options.Create(elasticSearchOptions));

            //sqlite setup example
            var sqliteOptions = new SqliteMessageQueueOptions <MyMessage>()
            {
                ConnectionString = $"Data Source = {Path.Combine(AppContext.BaseDirectory, "Queue.db")}"
            };
            var sqliteQueue = new SqliteMessageQueue <MyMessage>(new Logger <SqliteMessageQueue <MyMessage> >(), Options.Create(sqliteOptions));

            //mqtt setup example
            var mqttOptions =
                new MqttMessageQueueOptions <MyMessage>()
                .UseManagedMqttClientOptionsBuilder(builder =>
            {
                builder.WithClientOptions(options =>
                                          options
                                          .WithTcpServer("HOST HERE")
                                          .WithCredentials("USERNAME", "PASSWORD")
                                          .Build()
                                          );
            });


            //setup for disk queue forwarding to azure queue
            var diskOptions = new DiskMessageQueueOptions <MyMessage>()
            {
                MessageStore = new DirectoryInfo("/my-messages")
            };
            var diskQueue = new DiskMessageQueue <MyMessage>(new Logger <DiskMessageQueue <MyMessage> >(), Options.Create(diskOptions));

            var azureTopicOptions =
                new AzureTopicMessageQueueOptions <MyMessage>()
                .UseConnectionStringBuilder(
                    endpoint: "YOUR ENDPOINT HERE",
                    entityPath: "YOUR ENTITYPATH HERE",
                    sharedAccessKeyName: "YOUR SHARED ACCESS KEY NAME HERE",
                    sharedAccessKey: "YOUR SHARED ACCESS KEY HERE"
                    );
            var azureTopic = new AzureTopicMessageQueue <MyMessage>(new Logger <AzureTopicMessageQueue <MyMessage> >(), Options.Create(azureTopicOptions));

            var forwarderLogger  = new Logger <ForwarderMessageQueue <MyMessage> >();
            var forwarderOptions = new ForwarderMessageQueueOptions()
            {
                SourceSubscriptionName = "YOUR SUBSCRIPTION NAME HERE",
                ForwardingErrorHandler = (ex) =>
                {
                    forwarderLogger.LogError(ex, string.Empty);
                    return(Task.FromResult(CompletionResult.Abandon));
                }
            };
            var forwarder = new ForwarderMessageQueue <MyMessage>(forwarderLogger, Options.Create(forwarderOptions), diskQueue, azureTopic);


            //create the message
            var msg = new MyMessage()
            {
                GUID = Guid.NewGuid(),
                TEST = "TEST"
            };

            //with attributes
            var attributes = new MessageAttributes()
            {
                Label       = "YOUR LABEL HERE",
                ContentType = "application/json"
            };

            //and post to whichever queue you'd like. this one posts to the forwarder queue which posts to the disk and then forwards to azure
            await forwarder.PostMessageAsync(msg, attributes, CancellationToken.None);

            Console.Write("press any key to exit");
            Console.ReadKey();
        }