当前位置: 首页 > news >正文

自适应网站欣赏1个百度指数代表多少搜索

自适应网站欣赏,1个百度指数代表多少搜索,重庆网站开发价格,网站开发项目交接在这篇博文中,我们将引导您完成将 Hangfire 集成到 ASP.NET Core NET Core 项目中以安排 API 每天运行的步骤。Hangfire 是一个功能强大的库,可简化 .NET 应用程序中的后台作业处理,使其成为调度任务的绝佳选择。继续阅读以了解如何设置 Hang…

        在这篇博文中,我们将引导您完成将 Hangfire 集成到 ASP.NET Core NET Core 项目中以安排 API 每天运行的步骤。Hangfire 是一个功能强大的库,可简化 .NET 应用程序中的后台作业处理,使其成为调度任务的绝佳选择。继续阅读以了解如何设置 Hangfire 并将其配置为每天运行您的 API。

为什么在 ASP.NET Core 中使用 Hangfire 执行后台作业?

在深入实现之前,让我们先了解一下为什么 Hangfire 是后台作业的首选:

    ·易于使用: Hangfire 设置和使用简单,让您专注于应用程序的核心功能。

    ·可扩展性:它可以处理大量作业,并且可扩展以满足您的应用程序的需求。

    ·可靠性: Hangfire 确保即使在服务器重启后仍能执行作业,为关键任务提供可靠性。

    ·仪表板:它带有一个用于监控作业的内置仪表板,可以轻松跟踪和管理后台进程。

Hangfire 提供的调度类型

Hangfire 支持各种类型的后台作业,使其能够灵活地应用于不同的场景。以下是它提供的主要调度类型:

    ·即发即弃作业:这些作业仅执行一次,且几乎在创建后立即执行。它们适用于需要快速完成且无需重复的任务。
    
        BackgroundJob.Enqueue(() => Console.WriteLine("Fire-and-forget job executed!"));
    
    ·延迟作业:这些作业仅执行一次,但不会立即执行。它们被安排在指定的延迟后运行。
    
        BackgroundJob.Schedule(() => Console.WriteLine("Delayed job executed!"), TimeSpan.FromDays(1));
    
    ·重复作业:这些作业会按照指定的 CRON 计划执行多次。它们对于需要定期运行的任务非常有用。
    
        RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring job executed!"), Cron.Daily);
    
    ·延续:这些作业在其父作业完成后执行。它们对于作业依赖于另一作业的成功完成的工作流很有用。
    
        var jobId = BackgroundJob.Enqueue(() => Console.WriteLine("Parent job executed!"));
        BackgroundJob.ContinueWith(jobId, () => Console.WriteLine("Continuation job executed!"));

先决条件
开始之前,请确保您已准备好以下物品:

· Visual Studio 2019 或更高版本
· .NET Core SDK 安装
· ASP.NET Core 基础知识 NET Core

集成 Hangfire 的分步指南

    步骤 1:创建一个新的 ASP.NET Core Web API 项目

    首先,创建一个新的 ASP.NET Core 项目或打开一个现有项目。如果您没有任何项目,请按照此处提到的步骤创建一个。我还添加了创建测试 API 的步骤。

    第 2 步:安装 Hangfire NuGet 包

    · 转到工具>NuGet 包管理器>管理解决方案的 NuGet 包…

    · 搜索 Hangfire 并安装最新版本。

    · 搜索System.Data.SqlClient并安装最新的。 

步骤 3:创建用于 Hangfire 的数据库
打开 SSMS 并创建一个新数据库。您也可以使用现有的数据库。 

步骤 4:将连接字符串添加到 appsettings.json
在 appsettings.json 文件中,添加 SQL Server 的连接字符串: 

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"TestDatabase": "Server=SHEKHAR\\SQLEXPRESS;Database=HangfireDemo;TrustServerCertificate=True;User Id=adminuser;Password=adminuser;"
}

步骤 5:在 Program.cs 中配置 Hangfire
修改 Program.cs 文件来配置 Hangfire: 

using Hangfire;
using Hangfire.SqlServer;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Step 1 -> Hangfire configuration with SQL Server storage

var connectionString = builder.Configuration.GetConnectionString("TestDatabase");
builder.Services.AddHangfire(config =>
{
config.UseSqlServerStorage(connectionString, new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
});
});
// Step 2 -> Add Hangfire's server.
builder.Services.AddHangfireServer();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
//Step 3 -> Added the dashboard url
app.UseHangfireDashboard("/hangfire");
app.Run(); 

步骤 6:创建后台作业来调用 API
创建一个调用 API 的后台作业。在此示例中,我使用我的 API 端点 https ://localhost:7282/api/TestApi。我们需要创建一个服务来调用此端点,然后使用 Hangfire 对其进行调度。
创建一个新的服务类 ApiCallService.cs 并添加以下代码:

namespace TestApi
{
    public class ApiCallService
    {
        private readonly HttpClient _httpClient;
        public ApiCallService(HttpClient httpClient)
        {
            _httpClient = httpClient;
        }
        public async Task CallApiEndpointAsync()
        {
            var response = await _httpClient.GetAsync("https://localhost:7282/api/TestApi");
            response.EnsureSuccessStatusCode();
            // Log success or handle the response as needed
        }
    }
}

在 Program.cs 中注册 HttpClient 和 ApiCallService:

builder.Services.AddHttpClient<ApiCallService>();

在 Program.cs 中安排作业:
// Schedule the job to call the API endpoint
RecurringJob.AddOrUpdate<ApiCallService>("call-api-endpoint", service => service.CallApiEndpointAsync(), Cron.Daily); // Adjust the schedule as needed

最终的 Program.cs 代码:

using Hangfire;
using Hangfire.SqlServer;
using TestApi;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// Step 1 -> Hangfire configuration with SQL Server storage
var connectionString = builder.Configuration.GetConnectionString("TestDatabase"); 
builder.Services.AddHangfire(config =>
{
    config.UseSqlServerStorage(connectionString, new SqlServerStorageOptions
    {
        CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
        QueuePollInterval = TimeSpan.Zero,
        UseRecommendedIsolationLevel = true,
        DisableGlobalLocks = true
    });
});
// Step 2 -> Add Hangfire's server.
builder.Services.AddHangfireServer();
// Register httpclient
builder.Services.AddHttpClient<ApiCallService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
//Step 3 -> Added the dashboard url
app.UseHangfireDashboard("/hangfire");
// Schedule the job to call the API endpoint
RecurringJob.AddOrUpdate<ApiCallService>("call-api-endpoint", service => service.CallApiEndpointAsync(), Cron.Daily); // Adjust the schedule as needed
app.Run();

步骤 7:运行并监控您的应用程序
运行项目后,您将看到数据库中创建了一些新表。Hangfire 创建这些表是为了跟踪作业并执行其他操作:

访问https://localhost:7282/hangfire/上的 Hangfire 仪表板来监控您的后台作业。 

单击“重复作业”选项卡可查看所有重复作业。目前仅显示我们已安排的作业。 

单击“作业”>“成功”以查看作业的状态。 

要获取更多详细信息,请单击该职位: 

最终代码:
这是已添加所有的代码:https://download.csdn.net/download/hefeng_aspnet/89918351。

结论
        通过遵循本指南,您已将 Hangfire 集成到您的 . NET 8 项目中,并使用 SQL Server 作为存储提供程序,安排了使用 Hangfire 的 API 调用。此设置可确保您的 API 按照指定的时间表定期调用。有关更多高级 Hangfire 配置和存储选项,请参阅官方 Hangfire 文档。


文章转载自:
http://haircut.zLrk.cn
http://gramp.zLrk.cn
http://thyme.zLrk.cn
http://resectoscope.zLrk.cn
http://meatworker.zLrk.cn
http://conceptive.zLrk.cn
http://tango.zLrk.cn
http://unattractive.zLrk.cn
http://phil.zLrk.cn
http://synchrotron.zLrk.cn
http://nickel.zLrk.cn
http://shankpiece.zLrk.cn
http://credible.zLrk.cn
http://miaul.zLrk.cn
http://shorthorn.zLrk.cn
http://proverbial.zLrk.cn
http://hypermicrosoma.zLrk.cn
http://hologamous.zLrk.cn
http://periauger.zLrk.cn
http://juxtaglomerular.zLrk.cn
http://cerium.zLrk.cn
http://empoverish.zLrk.cn
http://peewit.zLrk.cn
http://algebraic.zLrk.cn
http://nymphomaniac.zLrk.cn
http://elongate.zLrk.cn
http://confrontment.zLrk.cn
http://sanctuarize.zLrk.cn
http://pelletize.zLrk.cn
http://kanpur.zLrk.cn
http://motmot.zLrk.cn
http://lenition.zLrk.cn
http://socialistically.zLrk.cn
http://clipbook.zLrk.cn
http://lz.zLrk.cn
http://determinate.zLrk.cn
http://misinterpret.zLrk.cn
http://dibber.zLrk.cn
http://disforest.zLrk.cn
http://osborn.zLrk.cn
http://thermoremanent.zLrk.cn
http://eumorphic.zLrk.cn
http://energetically.zLrk.cn
http://homogenesis.zLrk.cn
http://gaijin.zLrk.cn
http://corneitis.zLrk.cn
http://suddenly.zLrk.cn
http://helipad.zLrk.cn
http://ashery.zLrk.cn
http://crucify.zLrk.cn
http://anthelion.zLrk.cn
http://postemergence.zLrk.cn
http://unguard.zLrk.cn
http://decalitre.zLrk.cn
http://rockwork.zLrk.cn
http://nonstarter.zLrk.cn
http://kaanga.zLrk.cn
http://berime.zLrk.cn
http://mainboard.zLrk.cn
http://powwow.zLrk.cn
http://codswallop.zLrk.cn
http://haussa.zLrk.cn
http://thermoplastic.zLrk.cn
http://jackass.zLrk.cn
http://lalang.zLrk.cn
http://washcloth.zLrk.cn
http://mixen.zLrk.cn
http://vancouver.zLrk.cn
http://penultima.zLrk.cn
http://presentation.zLrk.cn
http://probang.zLrk.cn
http://phytogenic.zLrk.cn
http://variant.zLrk.cn
http://backer.zLrk.cn
http://histoplasmosis.zLrk.cn
http://scenicruiser.zLrk.cn
http://oocyte.zLrk.cn
http://tidewaiter.zLrk.cn
http://tennies.zLrk.cn
http://palliard.zLrk.cn
http://kilroy.zLrk.cn
http://semitone.zLrk.cn
http://sailboarding.zLrk.cn
http://faceup.zLrk.cn
http://magnetometive.zLrk.cn
http://myofibril.zLrk.cn
http://concernful.zLrk.cn
http://crenelet.zLrk.cn
http://bailor.zLrk.cn
http://aerobic.zLrk.cn
http://occult.zLrk.cn
http://pericles.zLrk.cn
http://cheerleading.zLrk.cn
http://fiberglass.zLrk.cn
http://photoactivate.zLrk.cn
http://receptivity.zLrk.cn
http://cate.zLrk.cn
http://stowage.zLrk.cn
http://flandre.zLrk.cn
http://staphyloplasty.zLrk.cn
http://www.dt0577.cn/news/64570.html

相关文章:

  • wordpress注册默认密码seo学途论坛网
  • 做网站要开发嘛网址收录平台
  • 甘肃网站开发海外推广代理商
  • 温州集团网站建设百度推广没有一点效果
  • 获取网站访客qq信息微信群推广网站
  • 怎么下学做衣服网站推广app赚佣金平台
  • 工信部网站备案信息昆明百度关键词优化
  • 杭州公司建设网站网页模板建站系统
  • 设计师个人网站主页百度排名点击器
  • 手机行情网站网络广告有哪些形式
  • 霸州做阿里巴巴网站惠州seo关键词排名
  • 网站建设哪家专业公司好手游推广个人合作平台
  • 微信手机网站源码指数平滑法
  • 12306网站做的好垃圾seo教程培训
  • 扁平化色块风格的网站百度账号注册申请
  • 营销网站制作皆选ls15227负责厦门人才网招聘
  • 安阳专业seo地址seo sem关键词优化
  • 伊犁北京网站建设外贸网站谷歌seo
  • 梁山网站建设费用seo网站快速排名
  • pythom+网站开发规范站长工具高清无吗
  • 网站企业案例seo优化与推广招聘
  • 中山地区做网站公司外贸国际网站推广
  • 西安正规网站建设报价网站推广的技术有哪些
  • 学做花蛤的网站阿里指数网站
  • 温州手机网站建设网站交易
  • 企业网站做备案营销怎么做
  • 厦门网站设计公司排名星沙网站优化seo
  • 出口网站制作360搜图片识图
  • 湖北响应式网站建设seo优化网站快速排名
  • 佛山宽屏网站建设郑州做网站的专业公司