首页 > 其他分享 >.NET6中配置Hangfire定时任务

.NET6中配置Hangfire定时任务

时间:2023-07-28 21:45:33浏览次数:27  
标签:false -- SqlServer Hangfire new using NET6 定时

1.安装Nuget包

  Hangfire.Core

  Hangfire.SqlServer                           --sqlserver数据库

    SqlServer需要配置其中一个包

    Microsoft.Data.SqlClient 

    system.data.sqlclient             

  Hangfire.AspNetCore                      --AspNetCore支持   Hangfire.Dashboard.BasicAuthorization              --可视化+权限控制   Hangfire.HttpJob                        --httpJob  

2.创建SqlServer数据库

  CREATE DATABASE HangFireText

3.appsettings.json配置连接字符串

  "ConnectionStrings": { "Hangfire": "Data Source=.;Initial Catalog=HangFireText;Integrated Security=True;Encrypt=false" }   其中 Encrypt=false 解决 证书链是由不受信任的颁发机构颁发的 问题  

4.Program.cs

 

using Hangfire;
using Hangfire.HttpJob;
using Hangfire.AspNetCore;
using Hangfire.SqlServer;
using Hangfire.Dashboard.BasicAuthorization;

 

var Config = builder.Configuration;

 

builder.Services.AddHangfire(config => config
  .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
  .UseSimpleAssemblyNameTypeSerializer()
  .UseRecommendedSerializerSettings()
  .UseStorage(new SqlServerStorage(
  Config["ConnectionStrings:Hangfire"],
    new SqlServerStorageOptions
    {
      CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
      SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
      QueuePollInterval = TimeSpan.FromSeconds(15),
      UseRecommendedIsolationLevel = true,
      UsePageLocksOnDequeue = true,
      DisableGlobalLocks = true
    })).UseHangfireHttpJob());

 

// Add the processing server as IHostedService
builder.Services.AddHangfireServer();

 

 

app.UseHangfireDashboard("/hangfire", new DashboardOptions
{

  Authorization = new[] {new BasicAuthAuthorizationFilter(new BasicAuthAuthorizationFilterOptions
  {

    RequireSsl = false,
    SslRedirect = false,
    LoginCaseSensitive = true,
    Users = new []
    {
      new BasicAuthAuthorizationUser
      {
        Login = "admin",
        PasswordClear = "admin"
      }
    }

  })}

});

 

app.UseRouting();

 

app.UseEndpoints(endpoints =>
{

  endpoints.MapGet("/", async context =>
  {
    await context.Response.WriteAsync("Hello World!");
  });

});

 

标签:false,--,SqlServer,Hangfire,new,using,NET6,定时
From: https://www.cnblogs.com/g2594/p/17588946.html

相关文章

  • xxl-job 定时任务的调研及学习
    xxl-job定时任务官网xxl-jobspringboot整合资料参考一、xxl-job简介xxl-job是一个开源的分布式定时任务框架,它可以与其他微服务组件一起构成微服务集群。它的调度中心(xxl-job)和执行器(自己的springboot项目中有@XxlJob("定时任务名称")的方法)是相互分离,分开部署的,两者通......
  • linux 定时任务清理数据
    1、查找及删除文件的命令find对应目录-mtime+天数-name"文件名"-execrm-rf{}\;应用命令为:find/fastdfs/storage/store/data/-mtime+10-name"*.*"-execrm-rf{}\;2、编辑脚本vi/fastdfs/storage/deleteScratchFile.sh内如如下:(删除30天前的文件。) ......
  • Java定时弹窗提示与展示图片
    自顶向下介绍,首先是定时弹窗功能,可以用Java自带的ScheduledExecutorService库完成函数调用。packagehealthReminder;importjava.util.concurrent.Executors;importjava.util.concurrent.ScheduledExecutorService;importjava.util.concurrent.TimeUnit;publicclassT......
  • .Net6实现定时任务
    首先创建一个类Background实现代码:usingMicrosoft.Extensions.Hosting;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceSmartMedicalCare.Web{publicclassBackground:IHostedService,......
  • NET6 EF Error: The certificate chain was issued by an authority that is not trus
    ErrorAconnectionwassuccessfullyestablishedwiththeserver,butthenanerroroccurredduringtheloginprocess.(provider:SSLProvider,error:0-Thecertificatechainwasissuedbyanauthoritythatisnottrusted.)解决方法:在DB连接字符串后面添加......
  • shell定时备份数据库
    摘要讲解如何使用shell每天定时编写数据库一、要求每天凌晨2:30备份数据库db1到/data/backup/db备份开始和备份结束能够给出相应的提示信息备份后的文件要求以备份时间为文件名,并打包成.tar.gz的形式,比如:2021-03-12_230201.tar.gz在备份的同时,检查是否有10天前备份的数......
  • linux at定时任务:at指令
    摘要at指令创建定时任务一、linux任务调度这个也是属于linux任务调度范畴里面的二、atd守护进程at命令是一次性定时计划任务,at的守护进程atd会以后台模式运行,检查作业队列来运行。默认情况下,atd守护进程每60秒检查作业队列,有作业时,会检查队列中的所有作业运行时间,如果......
  • .Net6基于layui和ztree完成树形选择器添加和反填和修改
    以责任科室为例存储两个值ResponsibleDepartment和AoId,ResponsibleDepartment:是科室名称,AoId是科室Id添加:<divclass="layui-form-itemlayui-form-text"><labelclass="layui-form-label">责任科室</label><divclass="layui-inpu......
  • STM32定时器(TIM)中断
    一、什么是定时器中断?大白话理解:使用一个定时器进行计时(计数),数数的方式可以从上到下(从设定值到0),也可从下到上(从0到设定值),或者中间计数(一般用不上,记住前面两者解决大部分问题,)要学会释怀自己,哈哈(允许我找个不会的借口)。当计数完成以后,将会发出冲断请求(就是给中断控制系统说,我想......
  • 直播平台软件开发,JavaWeb如何设置定时任务
    直播平台软件开发,JavaWeb如何设置定时任务1.在xml文件中添加监听器 <?xmlversion="1.0"encoding="UTF-8"?><web-appversion="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......