Using the background task tools in the .Netcore Webapi project HANGFIRE

Install Hangfire

   Install Hangfire.Core, Hangfire.SqlServer, Hangfire.AspNetCore through nuget in the webapi project. The latest version so far is 1.7. 6.

share picture

Use MSSQL database

   can create a new database, or use an existing database.

CREATE DATABASE < span style="color: #ff0000;">[HangfireTest]

GO

Set appsettings.json

{

"ConnectionStrings": {
"Hangfire": "Server=.;Database=mssqllocaldb;Integrated Security=SSPI;"
},
"Logging": {
"LogLevel": {
"Default": "Warning",
"Hangfire": "Information"
}
},
"AllowedHosts": "*"
}

Register hangfire service

   Quote Hangfire and in startup.cs Hangfire.SqlServer, and then register the hangfire service.

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)
{
// Sign up for Hangfire service
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString(
"HangfireConnection< span style="color: #800000;">"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout
= TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout
= TimeSpan.FromMinutes(5),
QueuePollInterval
= TimeSpan.Zero,
UseRecommendedIsolationLevel
= true,
UsePageLocksOnDequeue
= true,
DisableGlobalLocks
= true
}));

services.AddHangfireServer();

services.AddMvc();
}

   modify configure method

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHangfireDashboard();
backgroundJobs.Enqueue(()
=> Console.WriteLine("hello from hangfire"));

app.UseHttpsRedirection();
app.UseMvc();
}

Startup project

   You can see that several tables are automatically created in the database.

share picture

   add / after the project address Hangfire enters the hangfire task panel, this panel can be said to be exactly the same as the CAP panel??

Share pictures

share picture

CREATE DATABASE [HangfireTest]

GO

{

"ConnectionStrings": {
"Hangfire": "Server=.;Database=mssqllocaldb;Integrated Security=SSPI;"
},
"Logging": {
"LogLevel": {
"Default": "Warning",
"Hangfire": "Information"
}
},
"AllowedHosts": "*"
}

// This method gets called by the runtime. Use this method to add services to the container.

public void ConfigureServices(IServiceCollection services)
{
// Sign up for Hangfire service
services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(Configuration.GetConnectionString(
"HangfireConnection< span style="color: #800000;">"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout
= TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout
= TimeSpan.FromMinutes(5),
QueuePollInterval
= TimeSpan.Zero,
UseRecommendedIsolationLevel
= true,
UsePageLocksOnDequeue
= true,
DisableGlobalLocks
= true
}));

services.AddHangfireServer();

services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHangfireDashboard();
backgroundJobs.Enqueue(()
=> Console.WriteLine("hello from hangfire"));

app.UseHttpsRedirection();
app.UseMvc();
}

Leave a Comment

Your email address will not be published.