azure - 部署连续的Azure WebJob和WebApp会阻止App

标签 azure azure-devops azure-webjobs

我目前正在通过 Azure DevOps 将一个 Azure WebJob 和相应的 Web 应用部署到 Azure。我的 Web Job 启动主要取自各种示例,例如 https://github.com/Azure/azure-webjobs-sdk/tree/dev/sample/SampleHost

public class Program
{
    public static async Task Main()
    {
        IConfigurationRoot configRoot = null;

        var reg = new ServiceRegistry();
        reg.Scan(
            scanner =>
            {
                scanner.AssembliesFromApplicationBaseDirectory();
                scanner.LookForRegistries();
            });

        var builder = new HostBuilder()
            .ConfigureWebJobs(
                f =>
                {
                    f.AddAzureStorageCoreServices();
                    f.AddTimers();
                })
            .ConfigureAppConfiguration(
                (context, configurationBuilder) =>
                {
                    configurationBuilder.Sources.Clear();
                    configRoot = configurationBuilder
                        .SetBasePath(Directory.GetCurrentDirectory())
                        .AddJsonFile("appsettings.json", false, true)
                        .Build();
                })
            .ConfigureLogging(
                (context, b) =>
                {
                    b.AddApplicationInsightsWebJobs(
                        cfg =>
                        {
                            cfg.InstrumentationKey = "xxxx";
                        });

                    b.AddConsole();
                })
            .UseConsoleLifetime();

        var host = builder
            .UseLamar(reg)
            .Build();

        using (host)
        {
            var container = host.Services.GetService<IContainer>();
            var section = configRoot.GetSection(AppSettings.SectionKey);
            var settings = section.Get<AppSettings>();

            container.Configure(
                cfg =>
                {
                    cfg.AddSingleton(settings);
                });

            await host.RunAsync();
        }
    }
}

据我了解,由于我有第二个具有 crontriggers 等功能的文件,因此网络作业必须以连续模式运行才能保持事件状态。由于我想将 webjob 部署为 Web 应用程序的一部分,因此我在发布构建上添加了 YAML 任务,如下所示:

  - task: DotNetCoreCLI@2
    displayName: Publish Web Jobs
    inputs:
      command: publish

      publishWebProjects: false

      projects: 'Backend/Sources/Application/WebJobs/xxx.WebJobs.csproj'

      arguments: '--configuration $(BuildConfiguration) --output $(PublishPath)/App_Data/jobs/continuous/WebJobs /p:DeleteExistingFiles=True'

      zipAfterPublish: false

      modifyOutputPath: false

该任务的一个变体取自 https://www.rahulpnath.com/blog/azure-webjobs-dotnet-core-build-depoy/

构建完成后,我通过任务“Azure 应用服务部署”的默认配置一次性部署 Web 应用程序和作业。

有趣的是:如果我开始使用此任务和代码,Web 应用程序将无法再正确部署。检查日志,我发现 Web 作业实际上已部署并立即开始工作。因此,我认为,网络作业现在有点阻止网络应用程序完全部署,因为它已经在运行(并且可能锁定东西?)

我发现的大多数例子都是以前版本的网络作业,而且我没有找到任何提到这个问题的例子。有没有可能,这种组合不起作用,我需要单独部署 Web 应用程序和 Web 作业,或者还有其他问题吗?

最佳答案

Is it possible, this combination doesn't work and I would need to deploy web app and web job seperately, or is there another problem around?

据我所知,您不需要单独部署 Web 应用程序和 Web 作业。

如果我们部署一个带有相应 Web 应用程序的 Azure WebJob,我们需要通过右键单击您的 Web 应用程序并选择来创建 WebJob 项目:

Add / New Azure WebJob project

或者,如果您的解决方案中已有 WebJobs 项目:

Add / Existing WebJob project

enter image description here

此外,由于 WebJob 与 Web 应用程序关联,WebJob 包含在 Web 应用程序包中,因此您只需将 Web 应用程序部署到 Azure 应用程序服务即可。

发布时也会生成webjob的包(有两个zip文件),但你只需要在Azure App Service部署任务中指定web应用程序包即可。 (选中“使用 Web 部署发布”选项)

enter image description here

关于azure - 部署连续的Azure WebJob和WebApp会阻止App,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67202230/

相关文章:

asp.net-mvc-4 - 在 Azure Web 中启用 CORS

azure - 创建表期间写入 UserMetadata 字段

azure - 使用 azure 构建管道、自托管代理和 docker-desktop 创建 docker 镜像

c# - 如何在Azure服务总线选项中的MessageHandlerOptions中设置自动完成(使用Net Core 3.1)

Azure 安全 MFA 电话调用设置

c# - .NET Core控制台应用程序: Read list of Azure storage blobs as read-only user

tfs - 如何重用环境特定变量?

azure - 仅下载特定版本中最后发布的包文件

azure - 为什么我的连续 azure webjob 运行该函数两次?

Azure Function v1 中的 Azure 服务总线 session 支持