2020-07-14 13:24:07 +00:00
|
|
|
using System;
|
2020-11-03 08:46:38 +00:00
|
|
|
using System.Data;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Elsa.Caching;
|
2020-07-14 13:24:07 +00:00
|
|
|
using Elsa.DistributedLock;
|
2020-10-20 21:17:08 +00:00
|
|
|
using Elsa.Extensions;
|
2020-11-20 16:08:05 +00:00
|
|
|
using Elsa.Messages;
|
2020-10-17 18:19:44 +00:00
|
|
|
using Elsa.Serialization;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-10-17 18:19:44 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Rebus.Config;
|
2020-04-19 13:39:28 +00:00
|
|
|
using Rebus.DataBus.InMem;
|
2020-10-18 13:16:28 +00:00
|
|
|
using Rebus.Logging;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Rebus.Persistence.InMem;
|
|
|
|
|
using Rebus.Routing.TypeBased;
|
2020-11-20 12:29:08 +00:00
|
|
|
using Rebus.ServiceProvider;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Rebus.Transport.InMem;
|
2020-10-09 20:26:19 +00:00
|
|
|
using YesSql;
|
2020-10-23 12:20:52 +00:00
|
|
|
using Storage.Net;
|
|
|
|
|
using Storage.Net.Blobs;
|
2020-11-03 08:46:38 +00:00
|
|
|
using YesSql.Provider.Sqlite;
|
2020-01-15 20:26:44 +00:00
|
|
|
|
|
|
|
|
namespace Elsa
|
|
|
|
|
{
|
|
|
|
|
public class ElsaOptions
|
|
|
|
|
{
|
|
|
|
|
public ElsaOptions(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
Services = services;
|
|
|
|
|
|
2020-11-03 08:46:38 +00:00
|
|
|
ConfigurePersistence = (sp, config) => config.UseSqLite("Data Source=elsa.db;Cache=Shared", IsolationLevel.ReadUncommitted);
|
2020-10-23 12:20:52 +00:00
|
|
|
StorageFactory = sp => Storage.Net.StorageFactory.Blobs.InMemory();
|
2020-01-15 20:26:44 +00:00
|
|
|
DistributedLockProviderFactory = sp => new DefaultLockProvider();
|
|
|
|
|
SignalFactory = sp => new Signal();
|
2020-10-17 18:19:44 +00:00
|
|
|
JsonSerializerConfigurer = (sp, serializer) => {};
|
|
|
|
|
|
2020-10-20 21:17:08 +00:00
|
|
|
AddAutoMapper = () =>
|
|
|
|
|
{
|
|
|
|
|
services.AddAutoMapper(ServiceLifetime.Singleton);
|
|
|
|
|
services.AddSingleton(sp => sp.CreateAutoMapperConfiguration());
|
|
|
|
|
};
|
2020-11-20 12:29:08 +00:00
|
|
|
|
|
|
|
|
AddServiceBus = () =>
|
|
|
|
|
{
|
|
|
|
|
services.AddSingleton<InMemNetwork>();
|
|
|
|
|
services.AddSingleton<InMemorySubscriberStore>();
|
|
|
|
|
services.AddSingleton<InMemDataStore>();
|
|
|
|
|
services.AddRebus(ConfigureInMemoryServiceBus);
|
|
|
|
|
};
|
2020-10-20 21:17:08 +00:00
|
|
|
|
2020-10-17 18:19:44 +00:00
|
|
|
CreateJsonSerializer = sp =>
|
|
|
|
|
{
|
2020-10-20 21:17:08 +00:00
|
|
|
var serializer = DefaultContentSerializer.CreateDefaultJsonSerializer();
|
2020-10-17 18:19:44 +00:00
|
|
|
JsonSerializerConfigurer(sp, serializer);
|
|
|
|
|
return serializer;
|
|
|
|
|
};
|
2020-01-15 20:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IServiceCollection Services { get; }
|
2020-10-10 19:10:09 +00:00
|
|
|
internal Action<IServiceProvider, IConfiguration> ConfigurePersistence { get; set; }
|
2020-10-23 12:20:52 +00:00
|
|
|
internal Func<IServiceProvider, IBlobStorage> StorageFactory { get; set; }
|
2020-01-15 20:26:44 +00:00
|
|
|
internal Func<IServiceProvider, IDistributedLockProvider> DistributedLockProviderFactory { get; private set; }
|
|
|
|
|
internal Func<IServiceProvider, ISignal> SignalFactory { get; private set; }
|
2020-10-17 18:19:44 +00:00
|
|
|
internal Func<IServiceProvider, JsonSerializer> CreateJsonSerializer { get; private set; }
|
|
|
|
|
internal Action<IServiceProvider, JsonSerializer> JsonSerializerConfigurer { get; private set; }
|
2020-10-20 21:17:08 +00:00
|
|
|
internal Action AddAutoMapper { get; private set; }
|
2020-11-20 12:29:08 +00:00
|
|
|
internal Action AddServiceBus { get; private set; }
|
2020-01-15 20:26:44 +00:00
|
|
|
|
|
|
|
|
public ElsaOptions UseDistributedLockProvider(Func<IServiceProvider, IDistributedLockProvider> factory)
|
|
|
|
|
{
|
|
|
|
|
DistributedLockProviderFactory = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ElsaOptions UseSignal(Func<IServiceProvider, ISignal> factory)
|
|
|
|
|
{
|
|
|
|
|
SignalFactory = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 18:19:44 +00:00
|
|
|
public ElsaOptions UsePersistence(Action<IConfiguration> configure) => UsePersistence((sp, config) => configure(config));
|
2020-10-10 19:10:09 +00:00
|
|
|
|
|
|
|
|
public ElsaOptions UsePersistence(Action<IServiceProvider, IConfiguration> configure)
|
|
|
|
|
{
|
|
|
|
|
ConfigurePersistence = configure;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-23 12:20:52 +00:00
|
|
|
|
2020-10-23 12:38:26 +00:00
|
|
|
public ElsaOptions UseStorage(Func<IBlobStorage> factory) => UseStorage(_ => factory());
|
|
|
|
|
|
2020-10-23 12:20:52 +00:00
|
|
|
public ElsaOptions UseStorage(Func<IServiceProvider, IBlobStorage> factory)
|
|
|
|
|
{
|
|
|
|
|
StorageFactory = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-20 21:17:08 +00:00
|
|
|
|
|
|
|
|
public ElsaOptions UseAutoMapper(Action addAutoMapper)
|
|
|
|
|
{
|
|
|
|
|
AddAutoMapper = addAutoMapper;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-10-10 19:10:09 +00:00
|
|
|
|
2020-11-20 12:29:08 +00:00
|
|
|
public ElsaOptions UseServiceBus(Action addServiceBus)
|
2020-01-15 20:26:44 +00:00
|
|
|
{
|
2020-11-20 12:29:08 +00:00
|
|
|
AddServiceBus = addServiceBus;
|
2020-01-15 20:26:44 +00:00
|
|
|
return this;
|
|
|
|
|
}
|
2020-07-14 13:24:07 +00:00
|
|
|
|
2020-10-17 18:19:44 +00:00
|
|
|
public ElsaOptions UseJsonSerializer(Func<IServiceProvider, JsonSerializer> factory)
|
|
|
|
|
{
|
|
|
|
|
CreateJsonSerializer = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ElsaOptions ConfigureJsonSerializer(Action<IServiceProvider, JsonSerializer> configure)
|
|
|
|
|
{
|
|
|
|
|
JsonSerializerConfigurer = configure;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-01-15 20:26:44 +00:00
|
|
|
|
2020-10-23 12:20:52 +00:00
|
|
|
private static RebusConfigurer ConfigureInMemoryServiceBus(RebusConfigurer rebus, IServiceProvider serviceProvider)
|
2020-01-15 20:26:44 +00:00
|
|
|
{
|
2020-11-20 12:29:08 +00:00
|
|
|
var subscriberStore = serviceProvider.GetRequiredService<InMemorySubscriberStore>();
|
|
|
|
|
var dataStore = serviceProvider.GetRequiredService<InMemDataStore>();
|
|
|
|
|
var network = serviceProvider.GetRequiredService<InMemNetwork>();
|
|
|
|
|
|
2020-01-15 20:26:44 +00:00
|
|
|
return rebus
|
2020-11-20 12:29:08 +00:00
|
|
|
.Logging(logging => logging.ColoredConsole(LogLevel.Debug))
|
|
|
|
|
.Subscriptions(s => s.StoreInMemory(subscriberStore))
|
|
|
|
|
.DataBus(s => s.StoreInMemory(dataStore))
|
2020-11-20 16:08:05 +00:00
|
|
|
//.Routing(r => r.TypeBased().Map<RunWorkflow>("runworkflow"))
|
|
|
|
|
.Transport(t => t.UseInMemoryTransport(network, "elsa:publisher"));
|
2020-01-15 20:26:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|