In my application (C# api dotnet 5 modified to run as windows service) I use HangFire. When running my application in debug mode from Visual Studio, all works fine, and my database is being created including HangFire tables when I invoke migrations.
When I pack it up and try and install it as a service, the database gets created, but without the HangFire tables. I have tried everything I can think of, but without luck.
Anybody out there that know the requirements to make HangFire migrate from inside a windows service?
From Main
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();
From ConfigureServices (startup)
// 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;
});
I finally got around to sharing my findings. It was all due to privileges on the SQL Server side. A lot of work to figure out how to setup a login and assign the needed privileges and not too much :-). If anybody runs into something similar, comment here, and I will be happy to share my findings.