Asp.Net 5 was renamed to Asp.Net Core 1.0. This blog post has not been updated for reference sake.
For RC2 and RTM see the newer post here.
Azure App Service has a nice feature called WebJobs. I use it a lot. In case you do not know what that is, you can read more about WebJobs here and here.
Lately, I have been moving toward automating build and deployment using Visual Studio Team Services (VSTS) (formerly Visual Studio Online -VSO). I have also been using Asp.Net 5 since beta 4 and since the support for building and deploying this new WebApps is lagging in VSO/VSTS, I have gotten used to scripting every process that is not implemented.
There are already enough blogs on how to deploy the Asp.Net 5 WebApps. I will not discuss that in this post. I recommend looking at this link for anyone using beta8 upwards.
Before I begun using a lot of this automated builds and deployments, I deployed from within Visual Studio for the WebApps and using the Azure Portal for the WebJobs. I searched content on automating the process including the WebJob but could not find any. I quickly wrote a small script to fix it. The script is less than 10 lines! Let's get straight into it.
First, the solution with both the WebApp and the WebJob is available on Github. The scripts for automation are also included.
The build process is simple:
- Download DNU, DNX and restore NuGet packages for the solution.
- Build the WebApp project and produce outputs
- Build the WebJob project and produce outputs
- Upload the artifacts
- Move the outputs of the webJob into the App_Data folder of the WebApp
- Upload the webApp to Azure
Let's quickly look at each.
Step 1: Download DNU, DNX and restore NuGet Packages
There is nothing new here that is not available on the other blog post. Use a Powershell script step. Run Prebuild.ps1 with no arguments at all.
Step 2: Build the WebApp
-projectName TutorialWebsite -buildConfiguration $(BuildConfiguration) -packOutput $(Build.SourcesDirectory)\artifacts\TutorialWebsite\$(BuildConfiguration)\Pub
Step 3: Build the WebJob
The step is similar to the one above but change the project name from
TutorialWebjob. Mine looks like this:
-projectName TutorialWebjob -buildConfiguration $(BuildConfiguration) -packOutput $(Build.SourcesDirectory)\artifacts\TutorialWebjob\$(BuildConfiguration)\Pub
Step 4: Upload your artifacts
I put this step here because it would fail if I put it any lower. In addition, I wanted the build artifacts (outputs) to be upload int their default folder structure. I modified mine a little bit so that the sources are not included. You can tune yours differently.
Step 5: Mode the outputs to the appropriate folder
In this step, I made the scripts below which is available on Github.
param($websiteOutput, $webJobOutput, $webJobName) $source = "$webJobOutput\approot" $destination = "$websiteOutput\wwwroot\App_Data\jobs\continuous\$webJobName" Write-Host("Copying files from '$source' to '$destination'") Copy-Item $source -Destination $destination -Force -Recurse Write-Host("Deleting files from '$source'") Remove-Item $source -Recurse Write-Host("Done preparing WebJob for publishing!")
The output of the WebJob will be copied to the App_Data folder. More details about where your webJob is located are in the Microsoft Docs.
This is another Powershell script step and the arguments are:
-webJobName "blocking_jobhost_cs" -websiteOutput $(Build.SourcesDirectory)\artifacts\TutorialWebsite\$(BuildConfiguration)\Pub -webJobOutput $(Build.SourcesDirectory)\artifacts\TutorialWebjob\$(BuildConfiguration)\Pub
Step 6: Upload the webApp to Azure
This is not any different since we have already setup the folder structure. It is an azure Powershell script: PublishWebsite.ps1. Remember to setup your Azure subscription prior.
The arguments here include the name of the website or web app you are uploading to in Azure App Service. My arguments are:
-websiteName "your_webapp_name" -packOutput $(Build.SourcesDirectory)\artifacts\TutorialWebsite\$(BuildConfiguration)\Pub
All my steps look like this:
- There are path name restrictions in windows (260 characters) that you must watch out for. Otherwise, the data step 5 will fail.
- If you are not sure about the process, you can try it out locally by running the PowerShell scripts with the arguments that apply to your case.
- The step 5 script only supports continuous WebJobs and requires modification for periodic WebJobs. I have not needed periodic WebJobs because I schedule WebJobs using the WebJobs SDK extensions.
- The setups do not include tests. But you can add the step that between step 3 and 4