Debugging ASP Core on Linux with Visual Studio 2017


Today’s post comes to us from Premier Developer consultant Randy Patterson.  It’s an excellent tutorial that walks you through setting up Visual Studio to remotely debug ASP.NET Core applications running on a Linux server.


.NET Core and specifically ASP Core is an Open Source, High Performance implementation of .NET that runs on Windows, Linux and Mac. After you deploy your web application to a Linux server it can be difficult to troubleshoot when issues arise. In this article, I’ll show you how to configure your Linux test server and Development environment to allow remote debugging giving you all the greatness of debugging in Visual Studio while your code is running on a Linux server.

What you’ll need

Install dependencies on the Linux test server

Many different distributions of Linux are supported but for this article we’ll assume a fresh install of Ubuntu 16.04 server edition. In order for Visual Studio to remotely connect and debug .NET Core applications, the Linux server will require an SSH Server for use by Visual Studio (more on this later) and Unzip and Curl packages installed to allow Visual Studio to install the correct version of the remote debugger.

Log into the Linux test VM and issue the following commands:

sudo apt-get update
sudo apt-get install openssh-server unzip curl

clip_image002

Next, install .NET Core SDK on the server:

First, register the Microsoft signature key

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg

sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list'

Next, install the .NET Core SDK 2.0

sudo apt-get update
sudo apt-get install dotnet-sdk-2.0.0

**For updated instructions or additional Linux distributions, see the official installation instructions

Finally, verify the installation by issuing the following command to display the installed version of the .NET Core SDK

dotnet --version

clip_image004

Deploy application to your Linux VM

First, create a new ASP Core application by opening Visual Studio 2017

  1. Create an ASP Core Web Application
  2. Name the project LinuxDebug
  3. Press OK

    clip_image006

  4. On the next dialog make sure the following options are selected
    • .NET Core framework
    • ASP Net Core 2.0
    • Web Application (Model-View-Controller)
    • Docker support is NOT Checked
    • Authentication is set to No Authentication
    • Press OK

    clip_image008

The default behavior for ASP Core is to accept requests only from localhost. To change this, modify the program.cs file and add UseUrls(http://*:5000) to the webhost setup

namespace LinuxDebug
{

public class Program

{

public static IWebHost BuildWebHost(string[] args) =>

WebHost.CreateDefaultBuilder(args)

.UseStartup<Startup>()

.UseUrls("http://*:5000")

.Build();

}

}

Next, publish the web application to a directory that contains the files needed to run the application on the Linux Test VM.

  1. Right click on the LinuxDebug Project in the Solution Explorer and select Publish

    clip_image010

  2. Choose the Folder as the Publish Method and press Publish

    clip_image012

  3. After publish change the configuration to Debug by
    1. Selecting Settings

      clip_image014

    2. Change configuration to DEBUG
    3. Then press Save. A debug build is necessary to step through the code using the remote debugger.

      clip_image016

    4. Click Publish

      clip_image018

    5. Copy the files from the publish directory (\bin\Debug\PublishOutput\) to a directory with execute permission on the Linux Test VM.


Start the ASP Core Application on the Linux VM

Launch the ASP Core application and make sure everything runs on Linux without errors.

  1. Connect to Linux server using SSH
  2. Navigate to the directory the application was copied to
  3. At the command prompt type
    dotnet LinuxDebug.dll

    Note: The Linux operating system is case sensitive so make sure you use the proper case for the name of the dll.

    clip_image020

  4. Back on Windows, open up your favorite browser and navigate to the IP address of the Linux server followed by a “:5000” http://<server_ip_address>:5000
  5. After a few seconds you should see your web application.

    Note
    : If your Linux server has a firewall running you may need to update the rules to allow incoming connections on port 5000

Attach Debugger to ASP Core on Linux

Using Visual Studio running on Windows we will attach the debugger to an ASP Core application running on a remote Linux Server.

First, you will need:

  1. An ASP Core application running on Linux
  2. The IP Address of the Linux Server running the application
  3. A user on the Linux server that has permissions to
    1. Log into SSH
    2. Read/Write access to home directory

In Visual Studio, click on the Debug menu item and select Attach To Process

clip_image022

On the Attach To Process Dialog set the following properties:

  1. Connection Type = SSH
  2. Connection Target = IP Address of your Linux Test VM (Press ENTER)
  3. Pressing enter on connection type displays the "Connect to Remote System" dialog box
  4. Enter the user name and password for a user that has SSH Access
  5. Press connect and if everything is entered correctly then you will see a list of processes on the Ubuntu server

    Note: If not already installed, Visual Studio will install the remote debugger into the user’s home directory in a subfolder named .vs-debugger

clip_image024


Make sure the web application is running on the Ubuntu server and look for a process named "dotnet"

  1. Type “dotnet” to only show process names that begin with “dotnet”
  2. Locate the dotnet process running your application
  3. Press Attach

    clip_image026

  4. Click “Managed” then click OK

    clip_image028


Task 2: Step through code

  1. Set a break point in the Home controller About action method
  2. Press the about link on the web site
  3. Step through the code running on the Linux server

    clip_image030


The debug experience on a remote Linux server is similar to developing on a local Windows server. Visual Studio has excellent support for debugging your applications on Linux machines but it is less than intuitive to setup the first time.

Comments (0)

Skip to main content