在本地运行Azure WebJob

问题描述 投票:0回答:1

我正在使用的应用程序必须可在Azure和Windows Server上部署。

我正在使用连续Azure Web作业,该作业具有一些具有[TimerTrigger]属性的方法,这些方法在Azure上运行良好。但是我希望它们在部署在场所时也会触发。

这是否受支持?从DevOps角度来看,将exe与Windows Server上的Azure WebJob SDK连续运行的推荐方法是什么?

我正在使用.NET Core 3和Microsoft.Azure.Webjobs 3.0 SDK。

azure-webjobs azure-webjobssdk
1个回答
0
投票

您可以将Web作业项目视为控制台项目,只需单击your_webjob.exe即可连续运行它(通过指定TimerTrigger)。您可以按照此article创建一个.NET核心webjob。

这里是一个例子:

1。创建一个.NET core 3.0控制台项目,并安装以下nuget包:

Microsoft.Azure.WebJobs.Extensions, version 3.0.0

Microsoft.Azure.WebJobs.Extensions.Storage, version 3.0.10

Microsoft.Extensions.Logging.Console, version 3.0.0

2。这是program.cs中的代码:

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            var builder = new HostBuilder();
            builder.ConfigureWebJobs(b =>
            {

                b.AddTimers();
                b.AddAzureStorageCoreServices();
                b.AddAzureStorage();
            });
            builder.ConfigureLogging((context, b) =>
            {
                b.AddConsole();
            });

            var host = builder.Build();
            using (host)
            {
                host.Run();
            }
        }
    }
}

3。将appsettings.json文件添加到项目中,然后右键单击appsettings.json文件->选择属性->将“复制到输出目录”设置为“如果更新则复制”。这是appsettings.json:

{
  "ConnectionStrings": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xxx;EndpointSuffix=core.windows.net"
  }
}
  1. 将Functions.cs添加到项目。这是Functions.cs中的代码:

    using Microsoft.Azure.WebJobs;
    using Microsoft.Extensions.Logging;
    
    namespace ConsoleApp2
    {
        public class Functions
        {
            public static void CronJob([TimerTrigger("0 */1 * * * *")] TimerInfo timer, ILogger logger)
            {
                logger.LogInformation("this is a test message!");
            }
        }
    }
    

5。构建项目,然后转到your_webjob.exe所在的文件夹,只需双击.exe文件,它就可以在本地连续运行:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.