feat Hnagfire初步搭建

This commit is contained in:
cheche
2023-12-29 16:41:24 +08:00
parent f5c8f659e4
commit 66eacb685a
6 changed files with 58 additions and 1 deletions

View File

@@ -15,5 +15,7 @@ public class Startup : DefaultStartup
public Startup(IConfiguration configuration) : base(configuration)
{
IdentityModelEventSource.ShowPII = true;
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
@@ -17,6 +17,10 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.6" />
<PackageReference Include="Hangfire.Dashboard.BasicAuthorization" Version="1.0.2" />
<PackageReference Include="Hangfire.MySql.Core_MySql.Data" Version="2.1.10" />
<PackageReference Include="Hangfire.MySqlStorage" Version="2.0.2" />
<PackageReference Include="Kurisu.AspNetCore" Version="0.4.7" />
</ItemGroup>

View File

@@ -0,0 +1,12 @@
using Hangfire;
namespace Dlhis.ScheduledTask
{
public class MyBackgroundJobService
{
public void EnqueueMyJob()
{
BackgroundJob.Enqueue(() => Console.WriteLine("This is a background job."));
}
}
}

View File

@@ -1,6 +1,7 @@
using Kurisu.AspNetCore.Startup;
using Kurisu.Startup;
class Program
{
static void Main(string[] args)
@@ -15,4 +16,5 @@ public class Startup : DefaultStartup
{
}
}

View File

@@ -0,0 +1,34 @@
using Hangfire;
using Hangfire.MySql;
using Kurisu.AspNetCore.Startup;
namespace Dlhis.ScheduledTask
{
public class SqlSugarPack1 : BaseAppPack
{
public override bool IsBeforeUseRouting { get; }
public override void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseStorage(
new MySqlStorage(
Configuration.GetConnectionString("HangfireConnection"),
new MySqlStorageOptions
{
TablesPrefix = "Hangfire" // Adjust other options as needed
}
)
));
services.AddHangfireServer();
}
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHangfireDashboard(); //使用hangfire面板
app.UseHangfireServer(); //启动hangfire服务
}
}
}

View File

@@ -5,6 +5,9 @@
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"HangfireConnection": "server=192.168.199.124;port=3306;userid=root;password=dlhis123;database=Hangfire;charset=utf8mb4;Allow User Variables=true;"
},
"AllowedHosts": "*",
"Serilog": {
"MinimumLevel": {