liuxiaoyun111 发表于 2017-12-22 08:42:44

Asp.Net Core 集成 Hangfire 配置使用 Redis 存储

/// <summary>  
/// 启动类
  
/// </summary>

  
public>  
{
  
/// <summary>
  
/// 配置接口
  
/// </summary>
  
public IConfigurationRoot Configuration { get; }
  

  
/// <summary>
  
/// Redis 服务
  
/// </summary>
  
public static ConnectionMultiplexer Redis;
  

  
/// <summary>
  
/// 构造方法
  
/// </summary>
  
/// <param name="env"></param>
  
public Startup(IHostingEnvironment env)
  
{
  
var builder = new ConfigurationBuilder()
  
.SetBasePath(env.ContentRootPath)

  
.AddJsonFile("appsettings.json", optional: false,>  
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
  
.AddEnvironmentVariables();
  
Configuration = builder.Build();
  
Redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("Redis"));
  
}
  

  
// This method gets called by the runtime. Use this method to add services to the container.
  
public void ConfigureServices(IServiceCollection services)
  
{
  
services.AddOptions();
  
//自定义的配置
  
services.Configure<DbSetting>(Configuration.GetSection("ConnectionStrings"));
  
//返回大小写问题
  
            services.AddMvc()
  
.AddJsonOptions(option => option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());
  
//注入Hangfire服务
  
services.AddHangfire(config => config.UseRedisStorage(Redis));
  
}
  

  
// 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)
  
{
  
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
  
loggerFactory.AddDebug();
  

  
if (env.IsDevelopment())
  
{
  
app.UseDeveloperExceptionPage();
  
app.UseBrowserLink();
  
}
  
else
  
{
  
app.UseExceptionHandler("/Home/Error");
  
}
  

  
app.UseStaticFiles();
  

  
app.UseHangfireServer();
  
app.UseHangfireDashboard("/hangfire", new DashboardOptions
  
{
  
Authorization = new[] { new HangfireDashboardAuthorizationFilter() }
  
});
  

  
app.UseMvc(routes =>
  
{
  
routes.MapRoute(
  
name: "default",
  
template: "{controller=Hq}/{action=Index}/{id?}");
  
});
  
}
  
}
页: [1]
查看完整版本: Asp.Net Core 集成 Hangfire 配置使用 Redis 存储