private static IServiceCollection AddDaprStoreDatabaseSettings(this IServiceCollection services,
                                                                   Action <DaprStoreDatabaseSettings>?configureDaprStoreOptions = null)
    {
        var databaseSettings = new DaprStoreDatabaseSettings();

        if (configureDaprStoreOptions != null)
        {
            configureDaprStoreOptions(databaseSettings);
            services.Configure(configureDaprStoreOptions);
        }
        return(services.AddSingleton(_ =>
        {
            var client = new MongoClient(databaseSettings.ConnectionString);
            var database = client.GetDatabase(databaseSettings.DatabaseName);
            return database.GetCollection <EventWrapperDto>(databaseSettings.CollectionName);
        }));
    }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Subscriber", Version = "v1"
                });
            });

            // Add weather repository
            services.AddSingleton <WeatherForecastRepository>();

            // Add handlers
            services.AddSingleton <WeatherForecastEventHandler>();

            // Configuration
            var eventBusOptions = new DaprEventBusOptions();

            Configuration.GetSection(nameof(DaprEventBusOptions)).Bind(eventBusOptions);
            var eventCacheOptions = new DaprEventCacheOptions();

            Configuration.GetSection(nameof(DaprEventCacheOptions)).Bind(eventCacheOptions);
            var daprStoreDatabaseSettings = new DaprStoreDatabaseSettings();

            Configuration.GetSection(nameof(DaprStoreDatabaseSettings)).Bind(daprStoreDatabaseSettings);

            // Add Dapr service bus
            services.AddDaprEventBus(eventBusOptions.PubSubName);

            // Add Dapr Mongo event cache
            services.AddDaprMongoEventCache(eventCacheOptions.DaprStateStoreOptions
                                            .StateStoreName, options =>
            {
                options.ConnectionString = daprStoreDatabaseSettings.ConnectionString;
                options.DatabaseName     = daprStoreDatabaseSettings.DatabaseName;
                options.CollectionName   = daprStoreDatabaseSettings.CollectionName;
            });
        }