Last active
August 1, 2021 10:22
-
-
Save sfmskywalker/d466148d58281198b25307f8f5a3e42b to your computer and use it in GitHub Desktop.
Building Workflow Driven .NET Applications With Elsa 2 - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Logging": { | |
"LogLevel": { | |
"Default": "Warning", | |
"Microsoft": "Warning", | |
"Microsoft.Hosting.Lifetime": "Information" | |
} | |
}, | |
"AllowedHosts": "*", | |
"ConnectionStrings": { | |
"Sqlite": "Data Source=elsa.sqlite.db;Cache=Shared;" | |
}, | |
"Hangfire": { | |
"SchedulePollingInterval": "00:00:10", | |
"WorkerCount": 1 | |
}, | |
"Elsa": { | |
"Server": { | |
"BaseUrl": "http://localhost:5000", | |
"BasePath": "/workflows" | |
}, | |
"Smtp": { | |
"Host": "localhost", | |
"Port": 2525, | |
"DefaultSender": "[email protected]" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DocumentManagement.Workflows.Extensions; | |
using Elsa.Activities.Email.Options; | |
using Elsa.Activities.Http.Options; | |
using Elsa.Persistence.EntityFramework.Sqlite; | |
using Elsa.Server.Hangfire.Extensions; | |
using Hangfire; | |
using Hangfire.SQLite; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
using NodaTime; | |
using NodaTime.Serialization.JsonNet; | |
namespace DocumentManagement.Web | |
{ | |
public class Startup | |
{ | |
public Startup(IConfiguration configuration, IWebHostEnvironment environment) | |
{ | |
Configuration = configuration; | |
Environment = environment; | |
} | |
private IConfiguration Configuration { get; } | |
private IWebHostEnvironment Environment { get; } | |
public void ConfigureServices(IServiceCollection services) | |
{ | |
var dbConnectionString = Configuration.GetConnectionString("Sqlite"); | |
// Razor Pages (for UI). | |
services.AddRazorPages(); | |
// Hangfire (for background tasks). | |
AddHangfire(services, dbConnectionString); | |
// Elsa (workflows engine). | |
AddWorkflowServices(services, dbConnectionString); | |
// Allow arbitrary client browser apps to access the API for demo purposes only. | |
// In a production environment, make sure to allow only origins you trust. | |
services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithExposedHeaders("Content-Disposition"))); | |
} | |
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
app.UseExceptionHandler("/Error"); | |
app.UseHsts(); | |
} | |
app | |
.UseStaticFiles() | |
.UseCors() | |
.UseRouting() | |
.UseHttpActivities() // Install middleware for triggering HTTP Endpoint activities. | |
.UseEndpoints(endpoints => | |
{ | |
endpoints.MapRazorPages(); | |
endpoints.MapControllers(); // Elsa API Endpoints are implemented as ASP.NET API controllers. | |
}); | |
} | |
private void AddHangfire(IServiceCollection services, string dbConnectionString) | |
{ | |
services | |
.AddHangfire(config => config | |
// Use same SQLite database as Elsa for storing jobs. | |
.UseSQLiteStorage(dbConnectionString) | |
.UseSimpleAssemblyNameTypeSerializer() | |
// Elsa uses NodaTime primitives, so Hangfire needs to be able to serialize them. | |
.UseRecommendedSerializerSettings(settings => settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb))) | |
.AddHangfireServer((sp, options) => | |
{ | |
// Bind settings from configuration. | |
Configuration.GetSection("Hangfire").Bind(options); | |
// Configure queues for Elsa workflow dispatchers. | |
options.ConfigureForElsaDispatchers(sp); | |
}); | |
} | |
private void AddWorkflowServices(IServiceCollection services, string dbConnectionString) | |
{ | |
services.AddWorkflowServices(dbContext => dbContext.UseSqlite(dbConnectionString)); | |
// Configure SMTP. | |
services.Configure<SmtpOptions>(options => Configuration.GetSection("Elsa:Smtp").Bind(options)); | |
// Configure HTTP activities. | |
services.Configure<HttpActivityOptions>(options => Configuration.GetSection("Elsa:Server").Bind(options)); | |
// Elsa API (to allow Elsa Dashboard to connect for checking workflow instances). | |
services.AddElsaApiEndpoints(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Elsa.Persistence.EntityFramework.Core.Extensions; | |
using Elsa.Server.Hangfire.Extensions; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.DependencyInjection; | |
namespace DocumentManagement.Workflows.Extensions | |
{ | |
public static class ServiceCollectionExtensions | |
{ | |
public static IServiceCollection AddWorkflowServices(this IServiceCollection services, Action<DbContextOptionsBuilder> configureDb) | |
{ | |
return services.AddElsa(configureDb); | |
} | |
private static IServiceCollection AddElsa(this IServiceCollection services, Action<DbContextOptionsBuilder> configureDb) | |
{ | |
services | |
.AddElsa(elsa => elsa | |
// Use EF Core's SQLite provider to store workflow instances and bookmarks. | |
.UseEntityFrameworkPersistence(configureDb) | |
// Ue Console activities for testing & demo purposes. | |
.AddConsoleActivities() | |
// Use Hangfire to dispatch workflows from. | |
.UseHangfireDispatchers() | |
// Configure Email activities. | |
.AddEmailActivities() | |
// Configure HTTP activities. | |
.AddHttpActivities() | |
); | |
return services; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment