// 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; }); }
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)); }