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-12-12 13:07:17 +00:00
|
|
|
using Elsa.Persistence;
|
|
|
|
|
using Elsa.Persistence.InMemory;
|
2020-10-17 18:19:44 +00:00
|
|
|
using Elsa.Serialization;
|
2020-11-20 22:24:48 +00:00
|
|
|
using Elsa.Services;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-10-17 18:19:44 +00:00
|
|
|
using Newtonsoft.Json;
|
2020-04-19 13:39:28 +00:00
|
|
|
using Rebus.DataBus.InMem;
|
2020-11-22 10:49:28 +00:00
|
|
|
using Rebus.Logging;
|
2020-01-15 20:26:44 +00:00
|
|
|
using Rebus.Persistence.InMem;
|
|
|
|
|
using Rebus.Routing.TypeBased;
|
|
|
|
|
using Rebus.Transport.InMem;
|
2020-10-23 12:20:52 +00:00
|
|
|
using Storage.Net;
|
|
|
|
|
using Storage.Net.Blobs;
|
2020-01-15 20:26:44 +00:00
|
|
|
|
|
|
|
|
namespace Elsa
|
|
|
|
|
{
|
|
|
|
|
public class ElsaOptions
|
|
|
|
|
{
|
|
|
|
|
public ElsaOptions(IServiceCollection services)
|
|
|
|
|
{
|
|
|
|
|
Services = services;
|
|
|
|
|
|
2020-12-14 16:15:39 +00:00
|
|
|
WorkflowDefinitionStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowDefinitionStore>(sp);
|
|
|
|
|
WorkflowInstanceStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowInstanceStore>(sp);
|
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-11-20 22:24:48 +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
|
|
|
|
2020-11-20 22:24:48 +00:00
|
|
|
services.AddSingleton<InMemNetwork>();
|
|
|
|
|
services.AddSingleton<InMemorySubscriberStore>();
|
|
|
|
|
services.AddSingleton<InMemDataStore>();
|
2020-10-20 21:17:08 +00:00
|
|
|
|
2020-11-20 22:24:48 +00:00
|
|
|
ConfigureServiceBusEndpoint = ConfigureInMemoryServiceBusEndpoint;
|
|
|
|
|
|
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-23 12:20:52 +00:00
|
|
|
internal Func<IServiceProvider, IBlobStorage> StorageFactory { get; set; }
|
2020-12-12 13:07:17 +00:00
|
|
|
internal Func<IServiceProvider, IWorkflowDefinitionStore> WorkflowDefinitionStoreFactory { get; set; }
|
|
|
|
|
internal Func<IServiceProvider, IWorkflowInstanceStore> WorkflowInstanceStoreFactory { 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 22:24:48 +00:00
|
|
|
internal Action<ServiceBusEndpointConfigurationContext> ConfigureServiceBusEndpoint { 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-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-12-12 13:07:17 +00:00
|
|
|
|
|
|
|
|
public ElsaOptions UseWorkflowDefinitionStore(Func<IServiceProvider, IWorkflowDefinitionStore> factory)
|
|
|
|
|
{
|
|
|
|
|
WorkflowDefinitionStoreFactory = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ElsaOptions UseWorkflowInstanceStore(Func<IServiceProvider, IWorkflowInstanceStore> factory)
|
|
|
|
|
{
|
|
|
|
|
WorkflowInstanceStoreFactory = factory;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
2020-11-20 22:24:48 +00:00
|
|
|
|
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-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-11-22 12:05:34 +00:00
|
|
|
public ElsaOptions UseServiceBus(Action<ServiceBusEndpointConfigurationContext> setup)
|
2020-11-20 22:24:48 +00:00
|
|
|
{
|
|
|
|
|
ConfigureServiceBusEndpoint = setup;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void ConfigureInMemoryServiceBusEndpoint(ServiceBusEndpointConfigurationContext context)
|
|
|
|
|
{
|
|
|
|
|
var serviceProvider = context.ServiceProvider;
|
|
|
|
|
var transport = serviceProvider.GetService<InMemNetwork>();
|
|
|
|
|
var store = serviceProvider.GetRequiredService<InMemorySubscriberStore>();
|
|
|
|
|
var queueName = context.QueueName;
|
|
|
|
|
|
|
|
|
|
context.Configurer
|
2020-11-22 10:49:28 +00:00
|
|
|
.Logging(l => l.ColoredConsole(LogLevel.Info))
|
2020-11-20 22:24:48 +00:00
|
|
|
.Subscriptions(s => s.StoreInMemory(store))
|
|
|
|
|
.Transport(t => t.UseInMemoryTransport(transport, queueName))
|
2020-11-22 10:49:28 +00:00
|
|
|
.Routing(r => r.TypeBased().Map(context.MessageTypeMap));
|
2020-01-15 20:26:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|