vs调试linux wsl_从Windows上的Visual Studio远程调试WSL2中的.NET Core Linux应用

vs调试linux wsl

vs调试linux wsl

With Visual Studio Code and WSL (Windows Subsystem for Linux) you can be in a real Linux environment and run "code ." from the Linux prompt and Visual Studio Code will launch in Windows and effectively split in half. A VSCode-Server will run in Linux and manage the Language Services, Debugger, etc, while Windows runs your VS Code instance. You can use VS Code to develop on remote machines over SSH as well and it works great. In fact there's a whole series of Remote Tutorials to check out here.

使用Visual Studio Code和WSL(Linux的Windows子系统),您可以在真实Linux环境中运行“ code”。 从Linux提示符中可以看到,Visual Studio Code将在Windows中启动并有效地分成一半 VSCode-Server将在Linux中运行,并管理语言服务,调试器等,而Windows将运行VS Code实例。 您也可以使用VS Code在SSH上的远程计算机上进行开发,并且效果很好。 实际上,这里有完整的远程教程系列

VS Code is a great Code Editor but it's not a full IDE (Integrated Development Environment) so there's still lots of reasons for me to use and enjoy Visual Studio on Windows (or Mac).

VS Code是一个很棒的代码编辑器,但它不是完整的IDE(集成开发环境),因此我仍然有很多理由可以使用和享受Windows(或Mac)上的Visual Studio。

I wanted to see if it's possible to do 'remote' debugging with WSL and Visual Studio (not Code) and if so, is it something YOU are interested in, Dear Reader.

我想看看是否可以使用WSL和Visual Studio(而不是Code)进行“远程”调试,如果可以,亲爱的读者是否对您感兴趣?

  • To start, I've got WSL (specifically WSL2) on my Windows 10 machine. You can get WSL1 today on Windows from "windows features" just by adding it. You can get WSL2 today in the Windows Insiders "Slow Ring."

    首先,我在Windows 10计算机上安装了WSL(特别是WSL2)。 您现在可以通过添加“ Windows功能”在Windows上获得WSL1。 您现在可以在Windows Insiders“慢速响动”获得WSL2

  • Then I've got the new Windows Terminal. Not needed for this, but it's awesome if you like the command line.

    然后我有了新的Windows Terminal 。 不需要这样做,但是如果您喜欢命令行,那就太好了。

  • I've got Visual Studio 2019 Community

    我有Visual Studio 2019社区

I'm also using .NET Core with C# for my platform and language of choice. I've installed from https://dot.net/ inside Ubuntu 18.04, under Windows. I've got a web app (dotnet new razor) that runs great in Linux now.

我还将C#与.NET Core一起用于我选择的平台和语言。 我是在Windows下的Ubuntu 18.04中从https://dot.net/安装的。 我有一个Web应用程序( dotnet new razor ),现在可以在Linux上很好地运行。

RemoteWebApp in the Terminal

From the WSL prompt within terminal, I can run "explorer.exe ." and it will launch Windows Explorer at the path \\wsl$\Ubuntu-18.04\home\scott\remotewebapp, but VS currently has some issues opening projects across this network boundary. I'll instead put my stuff at c:\temp\remotewebapp and access it from Linux as /mnt/c/temp/remotewebapp.

在终端中的WSL提示符下,我可以运行“ explorer.exe”。 它将在路径\\ wsl $ \ Ubuntu-18.04 \ home \ scott \ remotewebapp中启动Windows资源管理器,但是VS当前在跨此网络边界打开项目时存在一些问题。 我将我的东西放到c:\ temp \ remotewebapp并从Linux以/ mnt / c / temp / remotewebapp的方式访问它。

RemoteWebApp in Explorer

In a perfect world - this is future speculation/brainstorming, Visual Studio would detect when you opened a project from a Linux path and "Do The Right Thing(tm)."

在一个完美的世界中-这是未来的猜测/头脑风暴,Visual Studio会检测您何时从Linux路径和“做正确的事(tm)”打开项目。

I'll need to make sure the VSDbg is installed in WSL/Linux first. That's done automatically with VS Code but I'll do it manually in one line like this:

我需要确保先在WSL / Linux中安装VSDbg。 这是使用VS Code自动完成的,但我将在一行中手动完成,如下所示:

curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg

We'll need a launch.json file with enough information to launch the project, attach to it with the debugger, and notice when things have started. VS Code will make this for you. In some theoretical future Visual Studio would also detect the context and generate this file for you. Here's mine, I put it in .vs/launch.json in the project folder.

我们将需要一个包含足够信息的launch.json文件来启动项目,并使用调试器将其附加到该文件,并在启动时发出通知。 VS Code将为您做到这一点。 在某些理论上,Visual Studio还将检测上下文并为您生成此文件。 这是我的,我将其放在项目文件夹中的.vs / launch.json中。

VS will make a launch.json also but you'll need to add the two most important parts, the $adapter and $adapterArgs part as I have here.

VS还将创建一个launch.json,但是您需要添加两个最重要的部分,即$ adapter和$ adapterArgs部分,就像我在这里一样。

{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"$adapter": "C:\\windows\\sysnative\\bash.exe",
"$adapterArgs": "-c ~/vsdbg/vsdbg",
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "/mnt/c/temp/remotewebapp/bin/Debug/netcoreapp3.0/remotewebapp.dll",
"args": [],
"cwd": "/mnt/c/temp/remotewebapp",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "^\\s*Now listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
},
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "bash.exe",
"pipeArgs": [ "-c" ],
"debuggerPath": "~/vsdbg/vsdbg"
},
"logging": { "engineLogging": true }
}
]
}

These launch.json files are used by VS and VS Code and other stuff and give the system and debugger enough to go on. There's no way I know of to automate this next step and attach it to a button like "Start Debugging" - that would be new work in VS - but you can start it like this by calling a VS2019 automation command from the "Command Window" you can access with View | Other Windows | Command Window, or Ctrl-Alt-A.

这些launch.json文件供VS和VS Code以及其他功能使用,并为系统和调试器提供足够的运行空间。 我没有办法自动化下一步,并将其附加到“开始调试”之类的按钮上-这在VS中是新工作-但您可以通过从“命令窗口”调用VS2019自动化命令来像这样启动它您可以使用查看| 其他Windows | 命令窗口或Ctrl-Alt-A。

Once I've typed this once in the Command Window, I can start the next Debug session by just pressing Up Arrow to get the command from history and hitting enter. Again, not perfect, but a start.

在“命令窗口”中键入一次后,可以通过按向上箭头从历史记录中获取命令并按Enter键来启动下一个调试会话。 再次,不是完美的,而是一个开始。

DebugAdapterHost.Launch /LaunchJson:C:\temp\remotewebapp\.vs\launch.json  

Here's a screenshot of me debugging a .NET Core app running in Linux under WSL from Windows Visual Studio 2019.

这是我调试Windows Visual Studio 2019中以WSL在Linux下运行的.NET Core应用程序的屏幕快照。

VS 2019

Thanks to Andy Sterland for helping me get this working.

感谢Andy Sterland帮助我完成这项工作。

So, it's possible, but it's not falling-off-a-log automatic. Should this setup and prep be automatic? Is development in WSL from Visual Studio (not Code) something you want? There is great support for Docker development within a container including interactive debugging already, so where do you see this fitting in...if at all? Does this add something or is it more convenient? Would you like "F5" debugging for WSL apps within VS like you can in VS Code?

因此,这是有可能的,但这并不是自动完成的。 此设置和准备是否应该是自动的? 是否需要从Visual Studio(而非代码)中进行WSL开发? 容器内对Docker开发的强大支持已经包括交互式调试,因此,如果有的话,您在哪里看到这种适合呢? 这会增加一些东西还是更方便? 您想像在VS Code中一样对VS中的WSL应用程序进行“ F5”调试吗?

Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!

发起人:喜欢C#吗? 我们也这样做! 这就是为什么我们开发了一个快速,智能,跨平台的.NET IDE的原因,它为您提供了更多的编码能力。 巧妙的代码分析,丰富的代码完成,即时搜索和导航,高级调试器...使用JetBrains Rider,您所需的一切都唾手可得。 在Linux,Mac或Windows上以思考的速度编写C#代码。 立即尝试JetBrains Rider

翻译自: https://www.hanselman.com/blog/remote-debugging-a-net-core-linux-app-in-wsl2-from-visual-studio-on-windows

vs调试linux wsl

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值