【问题标题】:Run Azure WebJob on premises server在本地服务器上运行 Azure WebJob
【发布时间】:2020-01-02 17:33:48
【问题描述】:

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

我正在使用 Continuous Azure WebJob,它有一些带有 [TimerTrigger] 属性的方法,可以在 Azure 上正常工作。但我希望它们在部署在本地时也能触发。

这是支持的吗?从 DevOps 的角度来看,让带有 Azure WebJob SDK 的 exe 在 Windows Server 上持续运行的推荐方法是什么?

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

【问题讨论】:

    标签: azure-webjobs azure-webjobssdk


    【解决方案1】:

    您可以将 Web 作业项目视为控制台项目,只需单击 your_webjob.exe 即可连续运行它(通过指定 TimerTrigger)。你可以按照这个article 来创建一个.NET core 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文件即可在本地连续运行:

    【讨论】:

    • 谢谢。我仍然想知道在 AzureWebJobsStorage 连接中放置什么。可以使用本地的东西吗?我知道有 AzureStorageEmulator,但不确定它应该用于生产。
    • @despuestambien,出于测试目的,您可以使用 AzureStorageEmulator,它是本地的。但是对于生产,你最好使用真正的天蓝色存储帐户连接字符串。
    • @despuestambien,如果帖子有帮助,请帮忙标记。谢谢。
    • 感谢您的回复。但是在我看来,对于需要 AzureStorage 的 WebJob 来说,没有一个好的替代方案。有必要从本地服务器连接到云,这是我想要避免的。
    • @despuestambien,eeem,这是为 webjob sdk 设计的,我们无法更改。但祝你好运,如果你有任何解决方案,请与我们分享:)
    猜你喜欢
    • 2018-04-28
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 2020-01-09
    • 1970-01-01
    • 2018-05-20
    相关资源
    最近更新 更多