Skip to content

Commit

Permalink
Simplify DbContext creation with ActivatorUtilities.
Browse files Browse the repository at this point in the history
Replaced Activator.CreateInstance with ActivatorUtilities.CreateInstance to streamline the DbContext instantiation. This improves dependency injection support and ensures better compatibility with service provider configurations.
  • Loading branch information
sfmskywalker committed Jan 29, 2025
1 parent e995cbf commit d500817
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\modules\Elsa.MassTransit.AzureServiceBus\Elsa.MassTransit.AzureServiceBus.csproj"/>
<ProjectReference Include="..\..\modules\Elsa.OpenTelemetry\Elsa.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Quartz.EntityFrameworkCore.MySql\Elsa.Quartz.EntityFrameworkCore.MySql.csproj" Condition=" '$(TargetFramework)' != 'net9.0' " />
<ProjectReference Include="..\..\modules\Elsa.Quartz.EntityFrameworkCore.PostgreSql\Elsa.Quartz.EntityFrameworkCore.PostgreSql.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Retention\Elsa.Retention.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Secrets.Api\Elsa.Secrets.Api.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Secrets.Persistence.EntityFrameworkCore.PostgreSql\Elsa.Secrets.Persistence.EntityFrameworkCore.PostgreSql.csproj" />
Expand Down
14 changes: 10 additions & 4 deletions src/apps/Elsa.Server.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
{
jobStorage = new MemoryStorage();
}

elsa.UseHangfire(hangfire => hangfire.UseJobStorage(jobStorage));
}

Expand Down Expand Up @@ -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():");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -560,7 +566,7 @@
.UseSecretsScripting()
;
}

elsa.UseRetention(r =>
{
r.SweepInterval = TimeSpan.FromHours(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down

0 comments on commit d500817

Please sign in to comment.