2020-07-14 13:24:07 +00:00
|
|
|
using System;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Elsa.Caching;
|
2020-07-14 13:24:07 +00:00
|
|
|
using Elsa.DistributedLock;
|
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-01-15 20:26:44 +00:00
|
|
|
using Rebus.Persistence.InMem;
|
|
|
|
|
using Rebus.Routing.TypeBased;
|
|
|
|
|
using Rebus.Transport.InMem;
|
2020-10-09 20:26:19 +00:00
|
|
|
using YesSql;
|
2020-10-10 19:10:09 +00:00
|
|
|
using YesSql.Provider.Sqlite.InMemory;
|
2020-01-15 20:26:44 +00:00
|
|
|
|
|
|
|
|
namespace Elsa
|
|
|
|
|
{
|
|
|
|
|
public class ElsaOptions
|
|
|
|
|
{
|
|
|
|
|
public ElsaOptions(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
Services = services;
|
|
|
|
|
|
2020-10-10 19:10:09 +00:00
|
|
|
ConfigurePersistence = (sp, config) => config.UseInMemorySqlite();
|
2020-01-15 20:26:44 +00:00
|
|
|
DistributedLockProviderFactory = sp => new DefaultLockProvider();
|
|
|
|
|
SignalFactory = sp => new Signal();
|
|
|
|
|
ServiceBusConfigurer = ConfigureInMemoryServiceBus;
|
2020-10-17 18:19:44 +00:00
|
|
|
JsonSerializerConfigurer = (sp, serializer) => {};
|
|
|
|
|
|
|
|
|
|
CreateJsonSerializer = sp =>
|
|
|
|
|
{
|
|
|
|
|
var serializer = DefaultJsonSerializer.CreateDefaultJsonSerializer();
|
|
|
|
|
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-01-15 20:26:44 +00:00
|
|
|
internal Func<IServiceProvider, IDistributedLockProvider> DistributedLockProviderFactory { get; private set; }
|
|
|
|
|
internal Func<IServiceProvider, ISignal> SignalFactory { get; private set; }
|
|
|
|
|
internal Func<RebusConfigurer, IServiceProvider, RebusConfigurer> ServiceBusConfigurer { 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-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-01-15 20:26:44 +00:00
|
|
|
public ElsaOptions ConfigureServiceBus(Func<RebusConfigurer, IServiceProvider, RebusConfigurer> configure)
|
|
|
|
|
{
|
|
|
|
|
ServiceBusConfigurer = configure;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-07-14 13:24:07 +00:00
|
|
|
|
2020-10-17 18:19:44 +00:00
|
|
|
public ElsaOptions ConfigureServiceBus(Func<RebusConfigurer, RebusConfigurer> configure) => ConfigureServiceBus((bus, _) => configure(bus));
|
|
|
|
|
|
|
|
|
|
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-10 19:10:09 +00:00
|
|
|
private static RebusConfigurer ConfigureInMemoryServiceBus(RebusConfigurer rebus,
|
|
|
|
|
IServiceProvider serviceProvider)
|
2020-01-15 20:26:44 +00:00
|
|
|
{
|
|
|
|
|
return rebus
|
2020-04-19 08:32:10 +00:00
|
|
|
.Logging(logging => logging.ColoredConsole())
|
2020-01-15 20:26:44 +00:00
|
|
|
.Subscriptions(s => s.StoreInMemory(new InMemorySubscriberStore()))
|
2020-04-19 13:39:28 +00:00
|
|
|
.DataBus(s => s.StoreInMemory(new InMemDataStore()))
|
2020-01-15 20:26:44 +00:00
|
|
|
.Routing(r => r.TypeBased())
|
|
|
|
|
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "Messages"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|