【问题标题】:Debugging Web Project w/ Visual Studio Code + ASP.NET 5 + Mono + Kestrel + OS X使用 Visual Studio Code + ASP.NET 5 + Mono + Kestrel + OS X 调试 Web 项目
【发布时间】:2015-11-22 20:38:42
【问题描述】:

我是 OS X 的新手(我的新 mac 用了 4 天),我正在努力建立一个环境,我可以使用 Visual Studio Code 中内置的调试器在本地调试我的 Asp.Net 5 Web 项目,特别是在附加到单声道时让断点工作。

我按照这些说明安装了 asp.net 5 和 Visual Studio 代码。 (http://docs.asp.net/en/latest/getting-started/installing-on-mac.html)

然后我安装了 yeoman,并搭建了一个 aspnet 项目。

我运行了命令 dnu restorednu builddnx web

该项目在 Kestrel 上本地运行 localhost:5000 就好了。我还可以通过 VS Code 调试器将它附加到单声道。

当我尝试在 C# 代码中设置断点时出现问题。调试器不会命中它们。

在做了一些研究后,我发现我需要将 Startup.cs 文件指定为 mono 作为要调试的文件。 (https://code.visualstudio.com/Docs/editor/debugging)

但是在运行mcs -debug Startup.cs 之后,我得到了这些错误:

Startup.cs(5,17): error CS0234: The type or namespace name `AspNet'     does not exist in the namespace `Microsoft'. Are you missing an assembly  reference?
Startup.cs(6,17): error CS0234: The type or namespace name `AspNet' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(7,17): error CS0234: The type or namespace name `AspNet' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(8,17): error CS0234: The type or namespace name `Data' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(9,17): error CS0234: The type or namespace name `Extensions' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(10,17): error CS0234: The type or namespace name `Extensions' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(11,17): error CS0234: The type or namespace name `Extensions' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(12,17): error CS0234: The type or namespace name `Extensions' does not exist in the namespace `Microsoft'. Are you missing an assembly reference?
Startup.cs(13,16): error CS0234: The type or namespace name `Models' does not exist in the namespace `palmtree'. Are you missing an assembly reference?
Startup.cs(14,16): error CS0234: The type or namespace name `Services' does not exist in the namespace `palmtree'. Are you missing an assembly reference?
Startup.cs(20,24): error CS0246: The type or namespace name `IHostingEnvironment' could not be found. Are you missing an assembly reference?
Startup.cs(20,49): error CS0246: The type or namespace name `IApplicationEnvironment' could not be found. Are you missing an assembly reference?
Startup.cs(39,16): error CS0246: The type or namespace name `IConfigurationRoot' could not be found. Are you missing an assembly reference?
Startup.cs(42,39): error CS0246: The type or namespace name `IServiceCollection' could not be found. Are you missing an assembly reference?
Startup.cs(62,31): error CS0246: The type or namespace name `IApplicationBuilder' could not be found. Are you missing an assembly reference?
Startup.cs(62,56): error CS0246: The type or namespace name `IHostingEnvironment' could not be found. Are you missing an assembly reference?
Startup.cs(62,81): error CS0246: The type or namespace name `ILoggerFactory' could not be found. Are you missing an assembly reference?

看起来 mono 无法正确调试编译 Startup.cs。尝试设置调试时是否有其他人收到此错误?或者有没有人在使用 mono、asp.net 5、kestrel、vs code 和 OS X 时有断点?

【问题讨论】:

  • 注意:使用版本“1.0.0-rc1-final”

标签: macos mono asp.net-core visual-studio-code kestrel-http-server


【解决方案1】:

调试 Visual Studio Code 和 ASP.NET 5 处于预览阶段,在此 Visual Studio Code 不支持时间调试 ASP.NET 5(在 任何平台)。请放心,我们正在努力带来这些 在不久的将来为您提供经验。

参考:https://code.visualstudio.com/docs/runtimes/ASPnet5

您所指的 Mono 部分用于编译和附加到 mono/.Net 程序(不是基于 dnx ASP.NET 的程序)。 ASP.NET 5 应用程序是使用 Roslyn 编译器编译的,而不是 Mono 的 mcs

调试 Mono 应用程序示例:

创建一个目录,cd 进入并输入code . 以使用该目录启动 VSCode。

mkdir monoconsoletest
cd monoconsoletest
code .

在 VSCode 中创建一个新文件

将其命名为program.cs

在编辑器中输入以下代码:

using System;

namespace consoleViaVSCode
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Console.WriteLine ("Hello VSCode!");
        }
    }
}

你可以在 shell 中编译和运行它,但是让我们创建一个 tasks.json 文件,这样我们就可以通过 Command Palette 来完成它。

创建一个tasks.json 文件:

下一步是设置任务配置。为此,请使用 F1 打开命令面板并输入配置任务运行器,然后按 Enter 将其选中。

这将在 .vscode 文件夹中创建一个示例 tasks.json 文件。初始文件中包含大量示例。全部突出显示,删除并添加:

{
    "version": "0.1.0",
    "command": "mcs",
    "isShellCommand": true,
    "showOutput": "silent",
    "args": ["-debug", "program.cs"]
}

现在您有一个任务,您可以通过 task mcs 向下执行 Command Palette,此任务将使用 Mono 的 mcs 编译器和“-debug”选项构建您的 program.cs,这将创建一个 program.exe.mdb 文件具有program.exe 应用程序的调试符号。

所以执行该任务,如果没有错误,program.exe 和 program.exe.mdb` 将显示在文件窗格中:

如果您有任何错误,它们将显示在Output 窗格中(源代码窗格的右侧)。

现在让我们添加一个task 以使用mono .Net 运行时运行您的程序,并提供等待调试器附加的选项。

注意:我会向您展示mono debugger task,但在 VSC 0.10.1 中运行这样的 shell 进程会被破坏。你会将它包装在一个 glup 例程中...... :-/ 所以......

process.cs 中,在Console.Write.... 行设置断点:

从你启动 VSCode 的 shell:

mono --debug --debugger-agent=transport=dt_socket,server=y,address=127.0.0.1:5858 program.exe

返回 VSCode:

单击Bug IconConfig\Gear Icon 并选择“C# mono”以生成默认的“attach”配置。选择“附加”并点击绿色的开始按钮:

你会遇到你的断点:

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-07-09
  • 2018-08-18
  • 2015-07-09
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
相关资源
最近更新 更多