// 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 = "EventDriven.SchemaRegistry.Api", Version = "v1" }); }); // Configuration var stateStoreOptions = new MongoStateStoreOptions(); Configuration.GetSection(nameof(MongoStateStoreOptions)).Bind(stateStoreOptions); // Dapr Schema Registry services.AddMongoSchemaRegistry(options => { options.ConnectionString = stateStoreOptions.ConnectionString; options.DatabaseName = stateStoreOptions.DatabaseName; options.SchemasCollectionName = stateStoreOptions.SchemasCollectionName; }); }
/// <summary> /// Adds MongoSchemaRegistry services to the provided <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />. /// </summary> /// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /></param> /// <param name="configureStateStoreOptions">Configure state store settings</param> /// <returns>The original <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" />.</returns> public static IServiceCollection AddMongoSchemaRegistry(this IServiceCollection services, Action <MongoStateStoreOptions> configureStateStoreOptions) { // Configure options var schemaOptions = new MongoStateStoreOptions(); configureStateStoreOptions.Invoke(schemaOptions); services.Configure(configureStateStoreOptions); // Register IMongoCollection<MongoSchema> services.AddSingleton(_ => { var context = new MongoSchemaRegistryDbContext(new MongoClient(schemaOptions.ConnectionString), schemaOptions.DatabaseName, schemaOptions.CollectionName); return(context.MongoSchemas); }); // Register services services.AddSingleton <ISchemaRegistry, MongoSchemaRegistry>(); services.AddSingleton <IDocumentRepository <MongoSchema>, DocumentRepository <MongoSchema> >(); return(services); }