private static IServiceCollection AddDaprEventBusSchema(this IServiceCollection services, Action <DaprEventBusSchemaOptions> configureSchemaOptions = null) { if (configureSchemaOptions == null) { return(services); } var schemaOptions = new DaprEventBusSchemaOptions { UseSchemaRegistry = false, MongoStateStoreOptions = new MongoStateStoreOptions() }; configureSchemaOptions(schemaOptions); services.Configure(configureSchemaOptions); if (schemaOptions.SchemaValidatorType == SchemaValidatorType.Json) { services.AddSingleton <ISchemaGenerator, JsonSchemaGenerator>(); services.AddSingleton <ISchemaValidator, JsonSchemaValidator>(); } switch (schemaOptions.SchemaRegistryType) { case SchemaRegistryType.Mongo: services.AddMongoSchemaRegistry(options => { options.ConnectionString = schemaOptions.MongoStateStoreOptions.ConnectionString; options.DatabaseName = schemaOptions.MongoStateStoreOptions.DatabaseName; options.CollectionName = schemaOptions.MongoStateStoreOptions.CollectionName; }); break; } return(services); }
private static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddHostedService <Worker>(); services.AddSingleton <WeatherFactory>(); // Configuration var eventBusOptions = new DaprEventBusOptions(); hostContext.Configuration.GetSection(nameof(DaprEventBusOptions)).Bind(eventBusOptions); var eventBusSchemaOptions = new DaprEventBusSchemaOptions(); hostContext.Configuration.GetSection(nameof(DaprEventBusSchemaOptions)).Bind(eventBusSchemaOptions); // Add Dapr service bus and enable schema registry with schemas added on publish. services.AddDaprEventBus(eventBusOptions.PubSubName, options => { options.UseSchemaRegistry = eventBusSchemaOptions.UseSchemaRegistry; options.SchemaRegistryType = eventBusSchemaOptions.SchemaRegistryType; options.MongoStateStoreOptions = eventBusSchemaOptions.MongoStateStoreOptions; options.SchemaValidatorType = eventBusSchemaOptions.SchemaValidatorType; options.AddSchemaOnPublish = eventBusSchemaOptions.AddSchemaOnPublish; }); });
/// <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)); }