Running and debugging .net framework / asp.net (mvc / web api) applications on macOS / Linux / (etc unix-based OS) or how I've configured VS Code Remote Development over SSH.

 Hello everyone, as a person who switched to unix systems in 2010, but a loving .net always lacked the main thing: to write and debug the code that I want on the system that I like. From 2010 to 2020, Microsoft has done a lot of work to take it for granted, but that's not the task, there are many legacy projects on the .net framework (hereinafter .net fx), what about them? I have been tormented by this question for several years now, and so I finally solved it ...

Meet: Microsoft Visual Studio Code: Remote Development over SSH 

The essence is very simple, in fact:

  1. VS Code via SSH connects to the host.
  2. Deploys its server there and connects to it.
  3. Executes all commands on the host and calculates the ports of the running software to your computer (client in this context).


VS Code acts as a kind of client that natively (as far as this term is applicable to electron.js applications) exists on your computer.

The experience itself is like running asp.net debugging on your computer. (In our context, this is asp.net, but VS Code has no technology restrictions)

So, let's get started setting up:


First thing we need to do is install openSSH server on our windows. Microsoft has kindly provided a very simple instruction in a few PowerShell commands. For the lazy, I will duplicate the commands that work for the state of 11/15/2020, but I recommend using the original source, because in the time when you read this, everything may change and the official instructions will be updated. But I can forget to update this article for new realities.

Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

Start-Service sshd # OPTIONAL but recommended: Set-Service -Name sshd -StartupType 'Automatic' # Confirm the Firewall rule is configured. It should be created automatically by setup. Get-NetFirewallRule -Name *ssh* # There should be a firewall rule named "OpenSSH-Server-In-TCP", which should be enabled # If the firewall does not exist, create one 

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22 



The second step is to set up VS Code config files to run commands and debug. On the root directory you should have a .vscode folder, if not, then you should create it, usually at the same level there are .idea and .vs folders, in this folder you should create 2 files: launch.json & tasks.json:

YourSolutionRootFolder:

    -- .idea

    -- .vs

    -- .vscode:

        -- launch.json

        -- tasks.json


The third step, we should add a task in the tasks.json file, in which you specify the path to your msbuild and what to do. In fact, this is the very "remote" software launch on the machine to which you are connected. This task will collect the project for us.


{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"dependsOn": [ "clean" ],
"type": "process",
"windows": {
"command": "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\amd64\\MSBuild.exe",
"args": [
"-p:Configuration=Debug;DebugType=portable;PlatformTarget=x64",
"YourSolutionName.sln"
]
},
"options": { "cwd": "${workspaceFolder}/" },
"group": "build"
}
]}


Then we should tell our VS Code to launch IIS Express with our newly built project. We will do it like this: specify in the launch.json file where to get IIS Express
and with what parameters it should start. Usually the config file is generated by Visual Studio or Rider. In my case, I'm using a file from Rider.



{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Framework Launch",
"type": "clr",
"request": "launch",
"preLaunchTask": "build",
"program": "C:\\Program Files\\IIS Express\\iisexpress.exe",
"args": ["/config:D:\\projects\\YourSolution\\YourSolution.WebApp\\.idea\\config\\applicationhost.config"],
"cwd": "C:\\Program Files\\IIS Express",
"stopAtEntry": false
}
]
}

The next step is much easier: you need in all * .csproj files set DebugType to the portable in the configurations you need, here is an example:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>

Further, by F5 (or which key you have assigned debugging in VS Code), debugging will start, and all ports from the VS Code host via SSH will be forwarded to your machine and everything will be available via localhost

P.S
This is the first time I am writing an article / guide / manual of this kind, so any constructive criticism on improvement is welcomed in the @the_RAMZAY telegram

Comments

Popular posts from this blog

Приветы всем!