在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
hf被定义为分布式后台服务,更加类似job作业的服务 // Add Hangfire services. services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .UseSimpleAssemblyNameTypeSerializer() .UseRecommendedSerializerSettings() .UseSqlServerStorage(Configuration.GetConnectionString("default"), new SqlServerStorageOptions { CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), QueuePollInterval = TimeSpan.Zero, UseRecommendedIsolationLevel = true, UsePageLocksOnDequeue = true, DisableGlobalLocks = true })); services.AddHangfireServer(); 关键是这个链如何配,UseSqlServerStorage专门读取hangfire会写入哪个数据库,如果用现在的业务数据库,hangfire的表会写进去的,也可以设计分开来存放,例如hangfire的DB,集中去管理。当然一般小的项目,直接跟业务数据库,集中而独立的架构。 Configure里面的配置 var jobOptions = new BackgroundJobServerOptions { Queues = new[] { "back", "front", "default" },//队列名称,只能为小写 WorkerCount = Environment.ProcessorCount * 5, //并发任务数 ServerName = "conference hangfire1",//服务器名称 }; app.UseHangfireServer(jobOptions);//启动Hangfire服务 app.UseHangfireDashboard();//启动hangfire面板 backgroundJobs.Enqueue(() => Console.WriteLine("Hangfire 服务器已启动")); app.UserHangfireAdminService(service); //这个是自己写的服务扩展 jobOptions如果不配置,会有自己默认的名称,例如服务器会读取你当前系统的名称,队列那些会读取对应服务器的程序集名称。 3.写服务 完成了第一二步,这里看这个作业如何写 public static class HangfireAdminService { private static BackgroundJobServer _server; /// <summary> /// 使用后台作业 /// </summary> /// <param name="app"></param> /// <param name="service"></param> /// <param name="client"></param> /// <returns></returns> public static IApplicationBuilder UserHangfireAdminService(this IApplicationBuilder app, IConferenceService service) { var mcfg = service.MailConfigDefault(); BackgroundJob.Enqueue(() => Console.WriteLine($"测试单个作业: {DateTime.Now}")); RecurringJob.AddOrUpdate( () => Console.WriteLine($"测试循环作业: {DateTime.Now}"), Cron.Minutely ); BackgroundJob.Schedule(() => Console.WriteLine($"测试延迟作业: {DateTime.Now}"), TimeSpan.FromMinutes(1)); return app; } } 这个是刚才configure里面的方法。 可以写成一个IApplicationBuilder的扩展方法,加进管道 /// <summary> /// 发送邮件 /// </summary> /// <param name="mail"></param> [HttpPost] [Route("SendPwd")] public MsgResult<bool> SendPwd(MailConfigDto mail) { MsgResult<bool> result = new MsgResult<bool>(ResultCode.Success); UserAccountDto user = null; try { MailConfigDto defaultMail = _service.MailConfigDefault(); user = _service.UserDtoById(mail.ExtensionId); defaultMail.Body = $"用户注册成功!登录名是:{user.UserName},登录密码是:{user.Password},注册类型是:{user.CustomerProfileName}"; defaultMail.Subject = $"用户:{user.Name}注册成功!"; List<KeyValuePair<string, string>> To = new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>(user.Name,user.Email) }; defaultMail.To = To; SendEmail(defaultMail); BackgroundJob.Enqueue<IConferenceService>((s) => s.LogAdd("注册成功", $"用户{user.Name}注册成功,已发送邮件", OPLog.AdminLogin) ); } catch (Exception ex) { result = new MsgResult<bool>(ResultCode.Exception, ex.Message); } return result; } 关键点, BackgroundJob.Enqueue<IConferenceService>((s) =>s.LogAdd()); 4.总结,当然还有很多细节没谈,不过这样基本的配置和使用,已经满足软件的日常用途了。 当然还有其他的,例如批量处理,延续,批量延续等 |
请发表评论