/// <summary>
    /// Add Dapr Mongo event cache to the provided <see cref="T:IServiceCollection" />.
    /// </summary>
    /// <param name="services">The <see cref="T:IServiceCollection" /></param>
    /// <param name="stateStoreName">The name of the state store component to use.</param>
    /// <param name="configureDaprStoreOptions">Configure Dapr store options.</param>
    /// <param name="configureEventCacheOptions">Configure event cache options.</param>
    /// <returns>The original <see cref="T:IServiceCollection" />.</returns>
    public static IServiceCollection AddDaprMongoEventCache(this IServiceCollection services,
                                                            string stateStoreName, Action <DaprStoreDatabaseSettings>?configureDaprStoreOptions = null,
                                                            Action <DaprEventCacheOptions>?configureEventCacheOptions = null)
    {
        var daprEventCacheOptions = new DaprEventCacheOptions
        {
            EnableEventCache        = true,
            DaprEventCacheType      = DaprEventCacheType.Queryable,
            EnableEventCacheCleanup = true,
            DaprStateStoreOptions   = new DaprStateStoreOptions {
                StateStoreName = stateStoreName
            }
        };

        if (configureEventCacheOptions != null)
        {
            configureEventCacheOptions(daprEventCacheOptions);
        }
        services.Configure <DaprEventCacheOptions>(options =>
        {
            options.DaprEventCacheType        = daprEventCacheOptions.DaprEventCacheType;
            options.DaprStateStoreOptions     = daprEventCacheOptions.DaprStateStoreOptions;
            options.EnableEventCache          = daprEventCacheOptions.EnableEventCache;
            options.EventCacheTimeout         = daprEventCacheOptions.EventCacheTimeout;
            options.EnableEventCacheCleanup   = daprEventCacheOptions.EnableEventCacheCleanup;
            options.EventCacheCleanupInterval = daprEventCacheOptions.EventCacheCleanupInterval;
        });

        services.AddSingleton <IDaprEventCache, DaprEventCache>();
        services.AddSingleton <IEventHandlingRepository <DaprIntegrationEvent>,
                               MongoEventHandlingRepository <DaprIntegrationEvent> >();
        services.AddSingleton <IDocumentRepository <EventWrapperDto>, DocumentRepository <EventWrapperDto> >();
        return(services.AddDaprStoreDatabaseSettings(configureDaprStoreOptions));
    }
    /// <summary>
    /// Add Dapr Mongo event cache to the provided <see cref="T:IServiceCollection" />.
    /// </summary>
    /// <param name="services">The <see cref="T:IServiceCollection" /></param>
    /// <param name="configuration">The application's <see cref="IConfiguration"/>.</param>
    /// <returns>The original <see cref="T:IServiceCollection" />.</returns>
    public static IServiceCollection AddDaprMongoEventCache(this IServiceCollection services,
                                                            IConfiguration configuration)
    {
        var daprEventCacheOptions = new DaprEventCacheOptions
        {
            DaprEventCacheType      = DaprEventCacheType.Queryable,
            EnableEventCacheCleanup = true,
        };
        var daprOptionsConfigSection = configuration.GetSection(nameof(DaprEventCacheOptions));

        daprOptionsConfigSection.Bind(daprEventCacheOptions);
        if (!daprOptionsConfigSection.Exists())
        {
            throw new Exception($"Configuration section '{nameof(DaprEventCacheOptions)}' not present in app settings.");
        }

        services.Configure <DaprEventCacheOptions>(options =>
        {
            options.DaprEventCacheType        = daprEventCacheOptions.DaprEventCacheType;
            options.DaprStateStoreOptions     = daprEventCacheOptions.DaprStateStoreOptions;
            options.EnableEventCache          = daprEventCacheOptions.EnableEventCache;
            options.EventCacheTimeout         = daprEventCacheOptions.EventCacheTimeout;
            options.EnableEventCacheCleanup   = daprEventCacheOptions.EnableEventCacheCleanup;
            options.EventCacheCleanupInterval = daprEventCacheOptions.EventCacheCleanupInterval;
        });

        services.AddSingleton <IDaprEventCache, DaprEventCache>();
        services.AddSingleton <IEventHandlingRepository <DaprIntegrationEvent>,
                               MongoEventHandlingRepository <DaprIntegrationEvent> >();
        services.AddSingleton <IDocumentRepository <EventWrapperDto>, DocumentRepository <EventWrapperDto> >();
        return(services.AddMongoDbSettings <DaprStoreDatabaseSettings, EventWrapperDto>(configuration));
    }
Exemple #3
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;
            });
        }
        /// <summary>
        /// Adds DaprEventBus services to the provided <see cref="T:IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="T:IServiceCollection" /></param>
        /// <param name="pubSubName">The name of the pub sub component to use.</param>
        /// <param name="configureSchemaOptions">Configure schema registry options.</param>
        /// <param name="configureEventCacheOptions">Configure event cache options.</param>
        /// <returns>The original <see cref="T:IServiceCollection" />.</returns>
        public static IServiceCollection AddDaprEventBus(this IServiceCollection services, string pubSubName,
                                                         Action <DaprEventBusSchemaOptions> configureSchemaOptions = null,
                                                         Action <DaprEventCacheOptions> configureEventCacheOptions = null)
        {
            services.AddDaprClient();
            if (configureSchemaOptions != null)
            {
                services.AddSingleton <IEventBus, DaprEventBusWithSchemaRegistry>();
            }
            else
            {
                services.AddSingleton <IEventBus, DaprEventBus>();
            }
            services.Configure <DaprEventBusOptions>(options => options.PubSubName = pubSubName);

            var daprEventCacheOptions = new DaprEventCacheOptions();

            if (configureEventCacheOptions == null)
            {
                services.Configure <DaprEventCacheOptions>(options =>
                                                           options.EnableEventCacheCleanup = daprEventCacheOptions.EnableEventCacheCleanup);
            }
            else
            {
                configureEventCacheOptions(daprEventCacheOptions);
                services.Configure(configureEventCacheOptions);
            }

            if (!daprEventCacheOptions.EnableEventCacheCleanup)
            {
                services.AddSingleton <IEventHandlingRepository <DaprIntegrationEvent>,
                                       NullEventHandlingRepository <DaprIntegrationEvent> >();
            }

            services.AddSingleton <IDaprEventCache, DaprEventCache>();
            return(services.AddDaprEventBusSchema(configureSchemaOptions));
        }
        /// <summary>
        /// Adds DaprEventBus services to the provided <see cref="T:IServiceCollection" />.
        /// </summary>
        /// <param name="services">The <see cref="T:IServiceCollection" /></param>
        /// <param name="configuration">The application's <see cref="IConfiguration"/>.</param>
        /// <param name="useSchemaRegistry">True to use schema registry</param>
        /// <returns>The original <see cref="T:IServiceCollection" />.</returns>
        public static IServiceCollection AddDaprEventBus(this IServiceCollection services,
                                                         IConfiguration configuration, bool useSchemaRegistry = false)
        {
            services.AddDaprClient();
            var daprEventBusOptions      = new DaprEventBusOptions();
            var daprOptionsConfigSection = configuration.GetSection(nameof(DaprEventBusOptions));

            daprOptionsConfigSection.Bind(daprEventBusOptions);
            if (!daprOptionsConfigSection.Exists())
            {
                throw new Exception($"Configuration section '{nameof(DaprEventBusOptions)}' not present in app settings.");
            }
            services.Configure <DaprEventBusOptions>(daprOptionsConfigSection);

            var daprEventCacheOptions       = new DaprEventCacheOptions();
            var daprEventCacheConfigSection = configuration.GetSection(nameof(DaprEventCacheOptions));

            daprEventCacheConfigSection.Bind(daprEventCacheOptions);
            if (!daprEventCacheConfigSection.Exists())
            {
                throw new Exception($"Configuration section '{nameof(DaprEventCacheOptions)}' not present in app settings.");
            }
            services.Configure <DaprEventCacheOptions>(daprEventCacheConfigSection);

            if (daprEventCacheOptions.EnableEventCache)
            {
                services.AddSingleton <IDaprEventCache, DaprEventCache>();
            }
            if (!daprEventCacheOptions.EnableEventCacheCleanup)
            {
                services.AddSingleton <IEventHandlingRepository <DaprIntegrationEvent>,
                                       NullEventHandlingRepository <DaprIntegrationEvent> >();
            }

            Action <DaprEventBusSchemaOptions> configureSchemaOptions = null;

            if (useSchemaRegistry)
            {
                services.AddSingleton <IEventBus, DaprEventBusWithSchemaRegistry>();
                var eventBusSchemaOptions = new DaprEventBusSchemaOptions();
                var schemaConfigSection   = configuration.GetSection(nameof(DaprEventBusSchemaOptions));
                if (!schemaConfigSection.Exists())
                {
                    throw new Exception($"Configuration section '{nameof(DaprEventBusSchemaOptions)}' not present in app settings.");
                }

                schemaConfigSection.Bind(eventBusSchemaOptions);
                configureSchemaOptions = options =>
                {
                    options.UseSchemaRegistry      = eventBusSchemaOptions.UseSchemaRegistry;
                    options.SchemaRegistryType     = eventBusSchemaOptions.SchemaRegistryType;
                    options.MongoStateStoreOptions = eventBusSchemaOptions.MongoStateStoreOptions;
                    options.SchemaValidatorType    = eventBusSchemaOptions.SchemaValidatorType;
                    options.AddSchemaOnPublish     = eventBusSchemaOptions.AddSchemaOnPublish;
                };
            }
            else
            {
                services.AddSingleton <IEventBus, DaprEventBus>();
            }

            return(services.AddDaprEventBusSchema(configureSchemaOptions));
        }