| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using Hangfire;
- using WebApplication1.Filter;
- using NLog.Extensions.Logging;
- using NLog.Web;
- using System.Transactions;
- using StackExchange.Redis;
- namespace WebApplication1
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddMvc();
- //添加 Hangfire 组件,并且指定使用 SQL Server 进行持久化存储
- services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("hangfire.sqlserver")));
- //添加 Hangfire 组件,并且指定使用 Redis 进行持久化存储
- services.AddHangfire(x =>
- {
- //从appsettings中获取 Redis 连接字符串
- x.UseRedisStorage(ConnectionMultiplexer.Connect(Configuration.GetConnectionString("hangfire.redis")));
- });
- //添加日志组件
- services.AddLogging();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc();
- #region HangFire
- //启动hangfire面板
- app.UseHangfireDashboard();
- //配置hangfire面板访问权限
- var options = new DashboardOptions
- {
- Authorization = new[] { new HangfireAuthorizationFilter() }
- };
- app.UseHangfireDashboard("/hangfire", options);
- //hangfire 配置项
- var jobOptions = new BackgroundJobServerOptions
- {
- Queues = new[] { "test", "default" },//队列名称,只能为小写
- WorkerCount = Environment.ProcessorCount * 5, //并发任务数
- ServerName = "hangfire1",//服务器名称
- };
- //启动Hangfire服务
- app.UseHangfireServer(jobOptions);
- #endregion
- #region NLog
- loggerFactory.AddNLog();//添加NLog
- env.ConfigureNLog("StaticConfig/nlog.config");//读取Nlog配置文件
- #endregion
- }
- }
- }
|