using System; using Elsa.Activities.Webhooks; using Elsa.Persistence.YesSql; using Elsa.Webhooks.Persistence.YesSql.Stores; using Microsoft.Extensions.DependencyInjection; using YesSql; using YesSql.Indexes; using Elsa.Runtime; using Elsa.Webhooks.Persistence.YesSql.Mapping; using Elsa.Persistence.YesSql.Services; using Elsa.Persistence.YesSql.Data; using Elsa.Webhooks.Persistence.YesSql.Indexes; namespace Elsa.Webhooks.Persistence.YesSql.Extensions { public static class ServiceCollectionExtensions { public static WebhookOptionsBuilder UseYesSqlPersistence(this WebhookOptionsBuilder elsa, Action configure) { elsa.Services .AddScoped() .AddSingleton(sp => CreateStore(sp, configure)) .AddSingleton() .AddScoped(CreateSession) .AddScoped() .AddStartupTask() .AddStartupTask() .AddDataMigration() .AddAutoMapperProfile() .AddIndexProvider(); return elsa .UseWebhookDefinitionStore(sp => sp.GetRequiredService()); } private static IStore CreateStore( IServiceProvider serviceProvider, Action configure) { var configuration = new Configuration { ContentSerializer = new CustomJsonContentSerializer() }; configure(serviceProvider, configuration); // TODO: The following line is a temporary workaround until the bug in YesSql is fixed: https://github.com/sebastienros/yessql/pull/280 var store = StoreFactory.CreateAndInitializeAsync(configuration).GetAwaiter().GetResult(); //var store = StoreFactory.Create(configuration); var indexes = serviceProvider.GetServices(); store.RegisterIndexes(indexes); return store; } private static ISession CreateSession(IServiceProvider serviceProvider) { var provider = serviceProvider.GetRequiredService(); return provider.CreateSession(); } } }