Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

visual studio 2015 - Publish simple Asp.Net Core App (.Net Framework) with WebJob

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

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

To start, I create the corresponding project in VS2017:

enter image description here

I select the basic web application template:

enter image description here

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".

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

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).

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

VS 2015 + project.json

  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
    
  5. 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"
      ]
    }
    

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 + .csproj

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

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.

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>

Hope it helps :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...