使用 WebJob 发布简单的 Asp.Net Core App (.Net Framework) [英] Publish simple Asp.Net Core App (.Net Framework) with WebJob

本文介绍了使用 WebJob 发布简单的 Asp.Net Core App (.Net Framework)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的 ASP.NET Core Web 应用程序 (.NET Framework) 并将一个 WebJob 与它一起发布到 Azure.

I would like to create a simple ASP.NET Core Web Application (.NET Framework) and publish a WebJob alongside it to Azure.

我使用的是 Visual Studio 2017,但结果在 VS2015 中似乎相同.

I'm using Visual Studio 2017, but the result seems to be the same in VS2015.

首先,我在VS2017中创建了相应的项目:

To start, I create the corresponding project in VS2017:

我选择了基本的 Web 应用程序模板:

I select the basic web application template:

在这一点上,我有点迷失了.在较旧的 Asp.Net MVC 5 方法中,我曾经能够单击 Asp.Net 项目并找到添加 => 新建 Azure Webjobs 项目".然后,WebJob 将始终与发布相关联,并且只是工作".

At this point I get a bit lost. In the older Asp.Net MVC 5 approach, I used to be able to click on the Asp.Net project and find "Add => New Azure Webjobs Project". The WebJob would then always be linked in publishing and just "work".

使用新的 Asp.Net Core 方法,我似乎找不到类似简单的方法来实现这一点.

With the new Asp.Net Core approach, I can't seem to find an analogously-easy way to get this happening.

我尝试在解决方案中手动添加一个 WebJobs 项目,然后在 Asp.Net 项目中手动设置 webjobs-list.json 文件,但发布步骤无法识别.我注意到各种答案,例如 this一个建议挂钩到发布过程来复制文件,但添加到 *.pubxml 文件的标记没有任何影响(即文件未被复制).

I tried manually adding a WebJobs project to the solution and then manually setting up the webjobs-list.json file in the Asp.Net project, but the publish step didn't recognize it. I noticed various answers like this one which suggest hooking into the publishing process to copy the files, but the markup added to the *.pubxml file didn't have any affect (i.e. the files weren't being copied).

似乎有一些关于此任务的过时信息.有人能详细说明当前在 Visual Studio 中为包含 Asp.Net Core 应用的新解决方案执行此操作的过程吗?

It seems that there's a bit of outdated information around regarding this task. Might somebody be able to detail the current process of doing this for a new solution containing an Asp.Net Core app in Visual Studio?

推荐答案

有一种方法可以做到,它适用于 VS 2015 和 project.json(甚至允许您从 VSTS 构建和部署到 Azure,而无需任何修改),并且在 VS 2017 中有点工作 - 从 VS 发布按预期工作,但我没能在不添加一些额外构建步骤的情况下使其在 VSTS 上工作.

There is a way to do it, it works fine with VS 2015 and project.json (and even allows you to build and deploy to Azure from VSTS without any modifications), and kinda works in VS 2017 - publish from VS works as expected, but I didn't manage to make it work on VSTS without adding some additional build steps.

  1. 像往常一样创建网络应用项目,无需任何额外的网络作业相关文件.
  2. 为 webjob 创建 .NET Core 控制台应用程序.
  3. 为 web 应用项目中的 webjob 创建必要的文件夹.对于名为 TheJob 的连续网络作业,它应该是 App_Data/Jobs/Continuous/TheJob.
  4. run.cmd 文件添加到上面的文件夹中,内容如下:

  1. Create web app project as usual, without any additional webjob related files.
  2. Create .NET Core console app for webjob.
  3. Create necessary folders for webjob in web app project. For continuous webjob named TheJob it should be App_Data/Jobs/Continuous/TheJob.
  4. Add run.cmd file to above folder with content like:

@echo off
The.Job.Project.Name.exe

  • 在 web 项目的 project.json 文件中,添加以下脚本:

  • In web project's project.json file, add following scripts:

    "scripts": {
      "postpublish": [
        "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%",
        "dotnet publish ..\Web.Job.Project.Name\ -o %publish:OutputPath%\app_data\jobs\continuous\TheJob\"
      ]
    }
    

  • 诀窍在于第二个脚本 - 它将 webjob 项目直接发布到 web 应用程序的 App_Data 目录中,因此它会与网站一起发布.

    The trick is in the second script - it publishes webjob project directly into web app's App_Data directory, so it gets published with the website.

    将项目转换为 VS 2017 格式后,脚本正在转换,但不幸的是不起作用,webjob 文件未发布.

    After converting projects to VS 2017 format, the scripts are getting converted, but unfortunately don't work, webjob files aren't published.

    我发现使其部分工作的一种方法是将 webjob 发布到 Web 应用程序的临时发布目录,因此它稍后会被提取并从 VS 发布,但不幸的是,它仅在直接从 Visual Studio 发布时才有效.它不适用于 VSTS 版本.

    One way I found to make it partially work is to publish webjob to web app's temporary publish directory, so it is picked up later and published from VS, but unfortunately it works only when publishing directly from Visual Studio. It doesn't work with VSTS builds.

    为此,请将以下部分添加到 Web 应用程序的项目文件中:

    To do it, add following section to web app's project file:

    <Target Name="PostpublishScript" AfterTargets="Publish">
      <Exec Command="dotnet publish ..Web.Job.Project.Name -o $(ProjectDir)obj$(Configuration)$(TargetFramework)$(RuntimeIdentifier)PubTmpOutApp_DataJobsContinuousTheJob" />
    </Target>
    

    希望有帮助:)

    这篇关于使用 WebJob 发布简单的 Asp.Net Core App (.Net Framework)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆