You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.3 KiB
166 lines
4.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Data.SqlClient;
|
|
using Hangfire;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Hangfire.Common;
|
|
using Hangfire.SqlServer;
|
|
using System.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
using static WpfApp4.App;
|
|
using Hangfire.Server;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TaskManager.Controllers;
|
|
using Microsoft.VisualBasic;
|
|
|
|
|
|
namespace WpfApp4
|
|
{
|
|
public class WebStartup
|
|
{
|
|
public IConfiguration Configuration { get; }
|
|
|
|
public WebStartup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddControllers();
|
|
|
|
services.AddDbContext<JobDbContext>(options =>
|
|
options.UseSqlServer("Server=127.0.0.1;Database=HANGFIRE;User ID=sa;Password=1;TrustServerCertificate=True"));
|
|
// 添加 Hangfire 服务
|
|
services.AddHangfire(configuration => configuration
|
|
.UseSqlServerStorage(
|
|
"Server=127.0.0.1;Database=Hangfire;User ID=sa;Password=1;TrustServerCertificate=True")
|
|
);
|
|
|
|
// 添加 Hangfire 服务器
|
|
services.AddHangfireServer();
|
|
services.AddControllersWithViews();
|
|
RegisterRecurringJobs();
|
|
|
|
|
|
}
|
|
|
|
|
|
private void RegisterRecurringJobs()
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
RecurringJob.AddOrUpdate<TokenServiceController>(
|
|
"reader",
|
|
x => x.ExecuteAsync(),
|
|
Cron.Minutely
|
|
//,
|
|
// JobFilterAttribute.AddFilter(new JobCompletionListener())
|
|
);
|
|
|
|
RecurringJob.AddOrUpdate<MyJob>(
|
|
"writer",
|
|
x => x.ExecuteWriterAsync(),
|
|
Cron.Minutely);
|
|
}
|
|
|
|
|
|
|
|
public class MyJob
|
|
{
|
|
//private readonly ILogger<MyJob> _logger;
|
|
// private readonly JobDbContext _dbContext;
|
|
public MyJob(
|
|
//ILogger<MyJob> _logger
|
|
//, JobDbContext dbContext
|
|
)
|
|
{
|
|
//this._logger = _logger;
|
|
// _dbContext = dbContext;
|
|
}
|
|
public void Execute()
|
|
{
|
|
|
|
// 这里是你的作业逻辑
|
|
Console.WriteLine("定时任务执行中: " + DateTime.Now);
|
|
|
|
// 如果需要访问 UI 元素,需要使用 Dispatcher
|
|
//Application.Current.Dispatcher.Invoke(() =>
|
|
//{
|
|
// // 更新 UI 的代码
|
|
|
|
//});
|
|
}
|
|
|
|
public void ExecuteReaderAsync()
|
|
{
|
|
//_logger.LogInformation("开始读取!");
|
|
|
|
//Application.Current.Dispatcher.Invoke(() =>
|
|
//{
|
|
// App.AddLog(new logModel() { Name = "读取", Info = "读取成功!" });
|
|
// // 更新 UI 的代码
|
|
|
|
//});
|
|
|
|
//string jobId = context.BackgroundJob.Id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
public void ExecuteWriterAsync()
|
|
{
|
|
//_logger.LogInformation("开始读取!");
|
|
//Application.Current.Dispatcher.Invoke(() =>
|
|
//{
|
|
// App.AddLog(new logModel() { Name = "写入", Info = "写入成功!" });// 更新 UI 的代码
|
|
|
|
//});
|
|
///string jobId = context.BackgroundJob.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
public class logModel
|
|
{
|
|
public string Name { get; set; }
|
|
public string Info { get; set; }
|
|
}
|
|
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
{
|
|
if (env.IsDevelopment())
|
|
{
|
|
app.UseDeveloperExceptionPage();
|
|
}
|
|
app.UseHangfireServer();
|
|
app.UseHangfireDashboard();
|
|
|
|
|
|
app.UseRouting();
|
|
app.UseEndpoints(endpoints =>
|
|
{
|
|
endpoints.MapControllers();
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|