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.
132 lines
4.4 KiB
132 lines
4.4 KiB
using Hangfire.Common;
|
|
using Hangfire.SqlServer;
|
|
using Hangfire;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.Windows;
|
|
using System.Data.SqlClient;
|
|
using System;
|
|
using Hangfire.Server;
|
|
using Autofac;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Collections.ObjectModel;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
namespace WpfApp4
|
|
{
|
|
/// <summary>
|
|
/// Interaction logic for App.xaml
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
|
|
private BackgroundJobServer _backgroundJobServer;
|
|
//private IContainer _container;
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
|
|
base.OnStartup(e);
|
|
#region 动态加载插件需要
|
|
//var startup = new Startup();
|
|
//_container = startup.ConfigureContainer();
|
|
//// 启动Hangfire服务器
|
|
//startup.StartHangfireServer(_container);
|
|
#endregion
|
|
// 配置 Hangfire
|
|
ConfigureHangfire();
|
|
// 启动作业服务器
|
|
StartHangfireServer();
|
|
|
|
BuilderHost();
|
|
|
|
|
|
}
|
|
#region 动态加载插件需要
|
|
//protected override void OnExit(ExitEventArgs e)
|
|
//{
|
|
// // 应用程序关闭时停止 Hangfire 服务器
|
|
// _container?.Dispose();
|
|
// base.OnExit(e);
|
|
//}
|
|
|
|
#endregion
|
|
|
|
public static class HangfireConfig
|
|
{
|
|
public static string DashboardPath => ConfigurationManager.AppSettings["Hangfire:DashboardPath"] ?? "/hangfire";
|
|
public static bool RequireAuthorization => bool.Parse(ConfigurationManager.AppSettings["Hangfire:RequireAuthorization"] ?? "true");
|
|
public static int WorkerCount => int.Parse(ConfigurationManager.AppSettings["Hangfire:WorkerCount"] ?? Environment.ProcessorCount.ToString());
|
|
public static string[] Queues => (ConfigurationManager.AppSettings["Hangfire:Queues"] ?? "default").Split(',');
|
|
|
|
public static string StorageType => ConfigurationManager.AppSettings["Hangfire:Storage:Type"] ?? "SqlServer";
|
|
|
|
public static string Url => ConfigurationManager.AppSettings["Hangfire:Url"] ?? "http://localhost:58886";
|
|
|
|
|
|
public static string ConnectionString =>
|
|
ConfigurationManager.AppSettings["Hangfire:Storage:ConnectionString"] ??
|
|
ConfigurationManager.ConnectionStrings["HangfireDb"]?.ConnectionString;
|
|
}
|
|
|
|
private void BuilderHost()
|
|
{
|
|
var host = CreateHostBuilder().Build();
|
|
// 启动WebHost
|
|
host.Start();
|
|
// 应用程序退出时停止WebHost
|
|
App.Current.Exit += (s, e) => host.StopAsync();
|
|
}
|
|
public static IHostBuilder CreateHostBuilder() =>
|
|
Host.CreateDefaultBuilder()
|
|
.ConfigureWebHostDefaults(webBuilder =>
|
|
{
|
|
webBuilder.UseUrls("http://localhost:5645");
|
|
webBuilder.UseStartup<WebStartup>();
|
|
});
|
|
private void ConfigureHangfire()
|
|
{
|
|
// 设置作业存储
|
|
GlobalConfiguration.Configuration
|
|
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
|
|
.UseSimpleAssemblyNameTypeSerializer()
|
|
.UseRecommendedSerializerSettings()
|
|
.UseSqlServerStorage(
|
|
"Server=127.0.0.1;Database=HangFire;User ID=sa;Password=1;TrustServerCertificate=True",
|
|
new SqlServerStorageOptions
|
|
{
|
|
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
|
|
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
|
|
QueuePollInterval = TimeSpan.Zero,
|
|
UseRecommendedIsolationLevel = true,
|
|
DisableGlobalLocks = true
|
|
});
|
|
}
|
|
|
|
private void StartHangfireServer()
|
|
{
|
|
// 创建并启动后台作业服务器
|
|
_backgroundJobServer = new BackgroundJobServer();
|
|
}
|
|
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
// 应用程序关闭时停止 Hangfire 服务器
|
|
_backgroundJobServer?.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|