Hangfire 指定redis db

master
wanghaomei 3 years ago
parent e7baaabadf
commit 919a8412e5

@ -33,26 +33,29 @@ namespace djyweb_djyPaasApi
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddControllers().ConfigureApiBehaviorOptions(options=> { services.AddControllers().ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = context => options.InvalidModelStateResponseFactory = context =>
{var result = new BadRequestObjectResult(context.HttpContext); {
var result = new BadRequestObjectResult(context.HttpContext);
return result; return result;
}; };
}); });
services.AddHttpClient(); services.AddHttpClient();
//读取配置信息 //读取配置信息
Configuration.Bind("WebConfig", sysOptionConfig.YsWebconfig); Configuration.Bind("WebConfig", sysOptionConfig.YsWebconfig);
sysOptionConfig._Configuration = Configuration; sysOptionConfig._Configuration = Configuration;
services.AddAuthentication("Bearer").AddJwtBearer("Bearer",option=> { services.AddAuthentication("Bearer").AddJwtBearer("Bearer", option =>
{
option.Authority = sysOptionConfig.YsWebconfig.IdentServerUrl;//授权服务的URl地址 option.Authority = sysOptionConfig.YsWebconfig.IdentServerUrl;//授权服务的URl地址
option.RequireHttpsMetadata = false;//是否使用了https option.RequireHttpsMetadata = false;//是否使用了https
option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters option.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{ ValidateAudience = false }; { ValidateAudience = false };
IdentityModelEventSource.ShowPII = true; IdentityModelEventSource.ShowPII = true;
}); });
//初始化数据库连接池 //初始化数据库连接池
@ -73,9 +76,10 @@ namespace djyweb_djyPaasApi
services.AddAutoMapper(typeof(AutoMapperConfig)); services.AddAutoMapper(typeof(AutoMapperConfig));
//任务配置 //任务配置
services.AddHangfire(config => { services.AddHangfire(config =>
config.UseRedisStorage(sysOptionConfig.YsWebconfig.Redis, new Hangfire.Redis.RedisStorageOptions { Prefix = "hf_"+sysOptionConfig.YsWebconfig.WebName,InvisibilityTimeout=TimeSpan.FromHours(1) }); {
config.UseRedisStorage(sysOptionConfig.YsWebconfig.Redis, new Hangfire.Redis.RedisStorageOptions { Prefix = "hf_" + sysOptionConfig.YsWebconfig.WebName, InvisibilityTimeout = TimeSpan.FromHours(1), Db = 3 });
}); });
services.AddHangfireServer(); services.AddHangfireServer();
//CAP 消息队列总线 //CAP 消息队列总线
@ -105,11 +109,11 @@ namespace djyweb_djyPaasApi
c.SwaggerDoc("v1", new OpenApiInfo { Title = "web_djyPaasapi", Version = "v1" }); c.SwaggerDoc("v1", new OpenApiInfo { Title = "web_djyPaasapi", Version = "v1" });
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location); var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
var xmlPath = Path.Combine(basePath, "AppServer.xml"); var xmlPath = Path.Combine(basePath, "AppServer.xml");
// c.IncludeXmlComments(Path.Combine(basePath, "djyweb_djyPaasApi.xml")); // c.IncludeXmlComments(Path.Combine(basePath, "djyweb_djyPaasApi.xml"));
//c.IncludeXmlComments(Path.Combine(basePath, "djy.Model.xml")); //c.IncludeXmlComments(Path.Combine(basePath, "djy.Model.xml"));
//c.IncludeXmlComments(Path.Combine(basePath, "Ys.Core.Common.xml")); //c.IncludeXmlComments(Path.Combine(basePath, "Ys.Core.Common.xml"));
}); });
//初始化IOC //初始化IOC
IOC.container = services; IOC.container = services;
@ -121,23 +125,24 @@ namespace djyweb_djyPaasApi
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
//swagger //swagger
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {
app.UseDeveloperExceptionPage(); app.UseDeveloperExceptionPage();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => { app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "web_djyuserapi v1"); c.SwaggerEndpoint("/swagger/v1/swagger.json", "web_djyuserapi v1");
// c.RoutePrefix = "apidoc"; // c.RoutePrefix = "apidoc";
}); });
} }
app.UseMiddleware<MytMiddleware>();//管道 app.UseMiddleware<MytMiddleware>();//管道
app.UseRouting(); app.UseRouting();
DefaultFilesOptions defaultFiles = new DefaultFilesOptions(); DefaultFilesOptions defaultFiles = new DefaultFilesOptions();
defaultFiles.DefaultFileNames.Clear(); defaultFiles.DefaultFileNames.Clear();
@ -145,7 +150,8 @@ namespace djyweb_djyPaasApi
app.UseDefaultFiles(defaultFiles); app.UseDefaultFiles(defaultFiles);
app.UseStaticFiles(); app.UseStaticFiles();
app.UseCors(builder => { app.UseCors(builder =>
{
builder.AllowAnyHeader(); builder.AllowAnyHeader();
builder.AllowAnyMethod(); builder.AllowAnyMethod();
//builder.WithOrigins("http://www.baidu.com");//跨域配置 //builder.WithOrigins("http://www.baidu.com");//跨域配置
@ -154,32 +160,33 @@ namespace djyweb_djyPaasApi
app.UseAuthentication();//认证 app.UseAuthentication();//认证
app.UseAuthorization();//授权 app.UseAuthorization();//授权
app.UseHangfireDashboard("/job", new DashboardOptions() { Authorization = new[] { new HangFireAuthorizeFilter() } });
app.UseEndpoints(endpoints => app.UseHangfireDashboard("/job", new DashboardOptions() { Authorization = new[] { new HangFireAuthorizeFilter() } });
{
endpoints.MapControllers(); app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
//启动运行启动 //启动运行启动
_Job(env); _Job(env);
} }
/// <summary> /// <summary>
/// 启动任务请求 /// 启动任务请求
/// </summary> /// </summary>
private void _Job(IWebHostEnvironment env) { private void _Job(IWebHostEnvironment env)
{
//初始化数据字典 //初始化数据字典
var _itool = IOC.AddServer<IToolsService>(); var _itool = IOC.AddServer<IToolsService>();
_itool.bindConfigDict(true, 5); _itool.bindConfigDict(true, 5);
_itool.AutoJob();
_itool.AutoJob();
} }
} }
} }

Loading…
Cancel
Save