hangfire - 将多个hangfire项目与单个postgresql数据库一起使用,如何写代码
要使用单个PostgreSQL数据库与多个Hangfire项目一起使用,你需要在每个Hangfire项目中定义不同的连接字符串。 您可以使用PostgreSQL的Schema机制来组织表,以便将所有表都存储在一个数据库中,而不是将其分布到多个数据库。
此外,请注意,如果你想使用Hangfire Dashboard UI来管理所有作业和工作者(而不是单独的Dashboard实例),你可能还需要使用shared filters或强制使用特定的过滤器/视图名称。 这些都可以通过DashboardOptions对象上的Filters属性来设置。
代码如下:
```csharp
// Setup Hangfire with a single Postgres database for multiple projects.
GlobalConfiguration.Configuration // Global Hangfire configuration object.
.UseStorage(new PostgreSqlStorage("ConnectionString1")) // First project's connection string.
.UseStorage(new PostgreSqlStorage("ConnectionString2")); // Second project's connection string.
// Setup the dashboard to use the same connection strings and force shared filters or views names across all projects.
appBuilder.UseHangfireDashboard("/hangfiredash", new DashboardOptions {
Authorization = new[] { new MyAuthorizationFilter() }, // Custom authorization filter to control access to the dashboard UI.
AppPath = null, // Set this property to `null` so that the Dashboard will be at "/hangfiredash" instead of "/hangfiredash/jobs".
Filters = new[] { new MyFilter() } // Use custom filters or view names for each project so that they are shared across all projects in one database instance.
}); ```
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。