elsa-core/src/core/Elsa.Core/ElsaConfiguration.cs

145 lines
5.8 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using System.Reflection;
using AutoMapper;
using Elsa.Caching;
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;
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;
using Rebus.Persistence.InMem;
using Rebus.Routing.TypeBased;
using Rebus.Transport.InMem;
using Storage.Net;
using Storage.Net.Blobs;
namespace Elsa
{
public class ElsaConfiguration
{
public ElsaConfiguration(IServiceCollection services)
{
Services = services;
WorkflowDefinitionStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowDefinitionStore>(sp);
WorkflowInstanceStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowInstanceStore>(sp);
WorkflowExecutionLogStoreFactory = sp => ActivatorUtilities.CreateInstance<InMemoryWorkflowExecutionLogStore>(sp);
StorageFactory = sp => Storage.Net.StorageFactory.Blobs.InMemory();
DistributedLockProviderFactory = sp => new DefaultLockProvider();
SignalFactory = sp => new Signal();
2020-11-20 22:24:48 +00:00
JsonSerializerConfigurer = (sp, serializer) => { };
AddAutoMapper = () =>
{
// The profiles are added to AddWorkflowsCore so that they are not forgotten in case the AddAutoMapper function(option) is overridden.
services.AddAutoMapper(Enumerable.Empty<Assembly>(), serviceLifetime: ServiceLifetime.Singleton);
};
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-11-20 22:24:48 +00:00
ConfigureServiceBusEndpoint = ConfigureInMemoryServiceBusEndpoint;
2020-10-17 18:19:44 +00:00
CreateJsonSerializer = sp =>
{
var serializer = DefaultContentSerializer.CreateDefaultJsonSerializer();
2020-10-17 18:19:44 +00:00
JsonSerializerConfigurer(sp, serializer);
return serializer;
};
}
public IServiceCollection Services { get; }
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; }
internal Func<IServiceProvider, IWorkflowExecutionLogStore> WorkflowExecutionLogStoreFactory { get; set; }
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; }
internal Action AddAutoMapper { get; private set; }
2020-11-20 22:24:48 +00:00
internal Action<ServiceBusEndpointConfigurationContext> ConfigureServiceBusEndpoint { get; private set; }
public ElsaConfiguration UseDistributedLockProvider(Func<IServiceProvider, IDistributedLockProvider> factory)
{
DistributedLockProviderFactory = factory;
return this;
}
public ElsaConfiguration UseSignal(Func<IServiceProvider, ISignal> factory)
{
SignalFactory = factory;
return this;
}
public ElsaConfiguration UseStorage(Func<IBlobStorage> factory) => UseStorage(_ => factory());
2020-10-23 12:38:26 +00:00
public ElsaConfiguration UseStorage(Func<IServiceProvider, IBlobStorage> factory)
{
StorageFactory = factory;
return this;
}
2020-12-12 13:07:17 +00:00
public ElsaConfiguration UseWorkflowDefinitionStore(Func<IServiceProvider, IWorkflowDefinitionStore> factory)
2020-12-12 13:07:17 +00:00
{
WorkflowDefinitionStoreFactory = factory;
return this;
}
public ElsaConfiguration UseWorkflowInstanceStore(Func<IServiceProvider, IWorkflowInstanceStore> factory)
2020-12-12 13:07:17 +00:00
{
WorkflowInstanceStoreFactory = factory;
return this;
}
public ElsaConfiguration UseWorkflowExecutionLogStore(Func<IServiceProvider, IWorkflowExecutionLogStore> factory)
{
WorkflowExecutionLogStoreFactory = factory;
return this;
}
2020-11-20 22:24:48 +00:00
public ElsaConfiguration UseAutoMapper(Action addAutoMapper)
{
AddAutoMapper = addAutoMapper;
return this;
}
2020-10-10 19:10:09 +00:00
public ElsaConfiguration UseJsonSerializer(Func<IServiceProvider, JsonSerializer> factory)
2020-10-17 18:19:44 +00:00
{
CreateJsonSerializer = factory;
return this;
}
public ElsaConfiguration ConfigureJsonSerializer(Action<IServiceProvider, JsonSerializer> configure)
2020-10-17 18:19:44 +00:00
{
JsonSerializerConfigurer = configure;
return this;
}
public ElsaConfiguration 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));
}
}
}