From d50081746810f2f5dd92f15e3456a0d838daa338 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Wed, 29 Jan 2025 19:03:59 +0100 Subject: [PATCH] Simplify DbContext creation with ActivatorUtilities. Replaced Activator.CreateInstance with ActivatorUtilities.CreateInstance to streamline the DbContext instantiation. This improves dependency injection support and ensures better compatibility with service provider configurations. --- src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj | 1 + src/apps/Elsa.Server.Web/Program.cs | 14 ++++++++++---- .../Abstractions/DesignTimeDbContextFactoryBase.cs | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj index bb2614836e..badb50298c 100644 --- a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj +++ b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj @@ -14,6 +14,7 @@ + diff --git a/src/apps/Elsa.Server.Web/Program.cs b/src/apps/Elsa.Server.Web/Program.cs index 1f1c393d4b..199ff6cce7 100644 --- a/src/apps/Elsa.Server.Web/Program.cs +++ b/src/apps/Elsa.Server.Web/Program.cs @@ -167,7 +167,7 @@ { jobStorage = new MemoryStorage(); } - + elsa.UseHangfire(hangfire => hangfire.UseJobStorage(jobStorage)); } @@ -371,7 +371,7 @@ // Make sure to configure the path to the python DLL. E.g. /opt/homebrew/Cellar/python@3.11/3.11.6_1/Frameworks/Python.framework/Versions/3.11/bin/python3.11 // alternatively, you can set the PYTHONNET_PYDLL environment variable. configuration.GetSection("Scripting:Python").Bind(options); - + options.AddScript(sb => { sb.AppendLine("def greet():"); @@ -428,7 +428,13 @@ if (useQuartz) { - elsa.UseQuartz(quartz => { quartz.UseSqlite(sqliteConnectionString); }); + elsa.UseQuartz(quartz => + { + if (sqlDatabaseProvider == SqlDatabaseProvider.Sqlite) + quartz.UseSqlite(sqliteConnectionString); + else if (sqlDatabaseProvider == SqlDatabaseProvider.PostgreSql) + quartz.UsePostgreSql(postgresConnectionString); + }); } if (useSignalR) @@ -560,7 +566,7 @@ .UseSecretsScripting() ; } - + elsa.UseRetention(r => { r.SweepInterval = TimeSpan.FromHours(5); diff --git a/src/modules/Elsa.EntityFrameworkCore.Common/Abstractions/DesignTimeDbContextFactoryBase.cs b/src/modules/Elsa.EntityFrameworkCore.Common/Abstractions/DesignTimeDbContextFactoryBase.cs index fe1951e6e1..df23d655fb 100644 --- a/src/modules/Elsa.EntityFrameworkCore.Common/Abstractions/DesignTimeDbContextFactoryBase.cs +++ b/src/modules/Elsa.EntityFrameworkCore.Common/Abstractions/DesignTimeDbContextFactoryBase.cs @@ -27,7 +27,7 @@ public TDbContext CreateDbContext(string[] args) ConfigureBuilder(builder, connectionString); - return (TDbContext)Activator.CreateInstance(typeof(TDbContext), builder.Options, serviceProvider)!; + return (TDbContext)ActivatorUtilities.CreateInstance(serviceProvider, typeof(TDbContext), builder.Options); } ///