Use .NET Core in an Azure web app on Linux

Note

Azure Web App on Linux is currently in public preview and supports running web apps natively on Linux.

Web App on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. This tutorial contains step-by-step instructions showing how to create a .NET Core app on Azure web app on Linux.

Web App on Linux

You can follow the steps below using a Mac, Windows, or Linux machine.

Prerequisites

To complete this tutorial:

If you don't have an Azure subscription, create a free account before you begin.

Create a local .NET Core application

Start a new terminal session. Create a directory named hellodotnetcore, and change the current directory to it. Then type the following:

dotnet new web

This command creates three files (hellodotnetcore.csproj, Program.cs, and Startup.cs) and one empty folder (wwwroot/) under the current directory. The content of .csproj file should look like the following:

  <!-- Empty lines are omitted. -->

  <Project Sdk="Microsoft.NET.Sdk.Web">
        <PropertyGroup>
        <TargetFramework>netcoreapp1.1</TargetFramework>
        </PropertyGroup>
        <ItemGroup>
        <Folder Include="wwwroot\" />
        </ItemGroup>
        <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
        </ItemGroup>
  </Project>

Since this app is a web application, a reference to an ASP.NET Core package was automatically added to the hellodotnetcore.csproj file. The version number of the package is set according to the chosen framework. This example is referencing ASP.NET Core version 1.1.2 because .NET Core 1.1 is used.

Build and test the application locally

You can build and run your .NET Core app with the dotnet restore command followed by the dotnet run command, as shown here:

dotnet restore
dotnet run

When the application starts, it displays a message indicating the app is listening to incoming requests at a port.

Hosting environment: Production
Content root path: C:\hellodotnetcore
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Test it by browsing to http://localhost:5000/ with your browser. If everything works fine, you see "Hello World!" as the result text.

Test with browser

Create a .NET Core app in the Azure Portal

First you need to create an empty web app. Log in to the Azure portal and create a new Web App on Linux.

Creating a web app

When the Create page opens, provide details about your web app:

Choosing a .NET Core runtime stack

Use the following table as a guide to fill out the Create page, then select OK and Create to create the app.

Setting Suggested value Description
App name hellodotnetcore The name of your app. This name must be unique.
Subscription Choose an existing subscription The Azure subscription.
Resource Group myResourceGroup The Azure resource group to contain the web app.
App Service Plan Existing App Service Plan name The App Service plan.
Configure Container .NET Core 1.1 The type of container for this web app: Built-in, Docker, or Private registry.
Image source Built-in The source of the image.
Runtime Stack .NET Core 1.1 The runtime stack and version.

Deploy your application via Git

Use Git to deploy the .NET Core application to Azure App Service Web App on Linux.

The new Azure web app already has Git deployment configured. You will find the Git deployment URL by navigating to the following URL after inserting your web app name:

https://{your web app name}.scm.azurewebsites.net/api/scm/info

The Git URL has the following form based on your web app name:

https://{your web app name}.scm.azurewebsites.net/{your web app name}.git

Run the following commands to deploy the local application to your Azure web app:

git init
git remote add azure <Git deployment URL from above>
git add *.csproj *.cs
git commit -m "Initial deployment commit"
git push azure master

You don't need to push any files under bin/ or obj/ directories because your application is built in the cloud when the application's source files are pushed to Azure. After the build process is complete, binary files are copied into the application's directory at /home/site/wwwroot/.

Confirm that the remote deployment operations report success. Push operations may take a while since package resolution and build process run in the cloud. You will see several status messages, including ones stating that files have been copied. The output should look similar to the following:

/* some output has been removed for brevity */
remote: Copying file: 'System.Net.Websockets.dll' 
remote: Copying file: 'System.Runtime.CompilerServices.Unsafe.dll' 
remote: Copying file: 'System.Runtime.Serialization.Primitives.dll' 
remote: Copying file: 'System.Text.Encodings.Web.dll' 
remote: Copying file: 'hellodotnetcore.deps.json' 
remote: Copying file: 'hellodotnetcore.dll' 
remote: Omitting next output lines...
remote: Finished successfully.
remote: Running post deployment commands...
remote: Deployment successful.
To https://hellodotnetcore.scm.azurewebsites.net/
 * [new branch]           master -> master

Once the deployment has completed, restart your web app for the deployment to take effect. To do this, go to the Azure portal and navigate to the Overview page of your web app. Select the Restart button in the page. When a popup window shows up, select Yes to confirm. You can then browse your web app, as shown here:

Browsing .NET Core app deployed to Azure App Service on Linux

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, you can delete them by deleting the resource group.

From the left menu in the Azure portal, select Resource groups and then select myResourceGroup.

On the resource group page, make sure that the listed resources are the ones you want to delete.

Select Delete, type myResourceGroup in the text box, and then select Delete.

Next steps