在我的应用程序(C# api dotnet 5 修改为作为 Windows 服务运行)中,我使用 HangFire。从 Visual Studio 以调试模式运行我的应用程序时,一切正常,并且当我调用迁移时,正在创建我的数据库,包括 HangFire 表。
当我打包它并尝试将其作为服务安装时,会创建数据库,但没有 HangFire 表。我已经尝试了我能想到的一切,但没有运气。
有没有人知道从 Windows 服务内部迁移 HangFire 的要求?
从主要
var host = Host.CreateDefaultBuilder().UseWindowsService()
.ConfigureWebHostDefaults(config =>
{
config.UseKestrel(options =>
options.Listen(IPAddress.Any, int.Parse(Configuration["port"]),
listenOptions =>
{
listenOptions.UseHttps("generated.pfx", "xxxxx");
})).UseContentRoot(pathToContentRoot);
config.UseIISIntegration();
config.UseStartup<Startup>();
})
.ConfigureAppConfiguration((buildercontext, config) =>
{
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
})
.Configure(new TMConfiguration
{
EnableLogging = true,
EnableTracing = true,
UseAutoMapper = false,
UseHealthCheck = true,
EnableSwagger = false,
ProductName = "TM Backend Server"
})
.Build();
从 ConfigureServices(启动)
// running migrate and applying all migrations
context.Database.Migrate();
//adding support for Hangfire in the Db
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString("DbConnection"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
PrepareSchemaIfNecessary = true,
EnableHeavyMigrations = true,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
})
);
services.AddHangfireServer(action =>
{
action.ServerName = $"{Environment.MachineName}:default";
action.Queues = new[] { "default" };
action.WorkerCount = Environment.ProcessorCount * 5;
});
services.AddHangfireServer(action =>
{
action.ServerName = $"{Environment.MachineName}:serial";
action.Queues = new[] { "serial" };
action.WorkerCount = 1;
});