如何在 Visual Studio 中使用 .NET 5(独立进程)调试 Azure Functions?

Posted

技术标签:

【中文标题】如何在 Visual Studio 中使用 .NET 5(独立进程)调试 Azure Functions?【英文标题】:How can I debug Azure Functions using .NET 5 (isolated process) in Visual Studio? 【发布时间】:2021-08-04 04:10:26 【问题描述】:

我最近为 C# 中的 Azure Function 项目从 .NET Core 3.1 迁移到 .NET 5.0 (using isolated/out-of-process runtime)。一切都按预期工作。但是,每当我调试时,我的断点都没有命中。为什么我现在不能调试我的 Azure Function 应用,但我以前可以?

【问题讨论】:

【参考方案1】:

如果您使用的是 Visual Studio 16.10 或更高版本,则在 Visual Studio 中进行调试非常简单。

The updated steps from Microsoft are as follows:

Visual Studio 与 Azure Functions 核心工具集成,因此您可以 可以使用完整的 Azure Functions 在本地测试您的函数 运行时。

要运行您的函数,请在 Visual Studio 中按 F5。您可能需要启用防火墙例外,以便工具可以处理 HTTP 要求。运行时永远不会强制执行授权级别 在本地运行。

从 Azure Functions 运行时输出中复制函数的 URL 并运行请求。欢迎使用 Functions 消息是 当函数成功运行并写入日志时显示 运行时输出。

要停止调试,请在 Visual Studio 中按 Shift+F5。

在您验证该函数在您的本地上正确运行后 计算机,是时候将项目发布到 Azure。


以下部分仅适用于使用 Visual Studio 16.9 或更早版本的情况。我强烈建议升级 Visual Studio 而不是使用这种“PITA”方法。

(请see the answer from Andrii获取替代解决方案)

在网上做了大量研究后,我了解到 .NET 5 用于 Azure Functions 的隔离进程默认不支持调试。在 Visual Studio 中执行此操作,you need to follow these steps(链接曾经有效,但已更新)。

    在 Visual Studio 中打开您的解决方案 在 Visual Studio 中打开 PowerShell(查看 -> 终端,或 Ctrl-`) 导航到您的项目cd MyProject 开启调试功能func start –-dotnet-isolated-debug 此时,您应该会在终端输出中看到打印的 PID,类似于以下内容
 Functions:
     HttpExample: [GET,POST] http://localhost:7071/api/HttpExample
 For detailed output, run func with --verbose flag.
 [2021-03-09T08:41:41.904Z] Azure Functions .NET Worker (PID: 81720) initialized in debug mode. Waiting for debugger to attach...
    打开附加进程(打开 -> 调试)窗口,并选择在控制台输出中找到的 PID 此时,断点现在有效并将被命中。

【讨论】:

【参考方案2】:

已添加原生 Visual Studio 支持,因此不再需要

有一个简单的方法!

public static class Program

    public static void Main()
    
        Debugger.Launch(); // The trick! <<=======================================

        using var host = new HostBuilder()
...

然后像往常一样启动而不进行调试 (Ctrl+F5) 并将相同的 Visual Studio 实例附加到 dotnet 进程

瞧,你调试你的函数!

【讨论】:

您好@Andrii - 我看到Visual Studio 已更新为无需这些额外步骤即可支持调试,因此我更新了答案并将其标记为正确。只是想让你知道它发生了变化。【参考方案3】:

这对我不起作用。 我在 DotnetCore5 中有一个函数应用程序,我使用 Ctrl+F5 运行它。 我有调试窗口,我选择运行实例。 调试触发根文件“program.cs”,其中包含:

#if DEBUG
            Debugger.Launch();
#endif

但不在此其他文件中包含:

       [Function(FunctionName)]
        public async Task Run([TimerTrigger("0 */1 * * * *"
#if DEBUG
                , RunOnStartup = true
#endif
            )]TimerInfo myTimer, FunctionContext executionContext)
        
            // Application du log
            _log = executionContext.GetLogger(FunctionName);

谁运行良好,我在控制台中看到它启动(在我下面的“GetRubixData”示例中)

我尝试过 VS 2019 和 2022 我要检查什么?

控制台包含:

    Functions:
            PostWassaSalissureMesureData: [POST] http://localhost:7071/api/PostWassaSalissureMesureData
    
            GetRubixData: TimerTrigger
    
    For detailed output, run func with --verbose flag. 
[2021-12-22T07:38:06.596Z] Retrying to start listener for function 'Functions.GetRubixData' (Attempt 1) 
[2021-12-22T07:38:06.598Z] Listener successfully started for function 'Functions.GetRubixData' after 1 retries. [2021-12-22T07:38:09.430Z] Host lock lease acquired by instance ID '000000000000000000000000DA099322'. 
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
          Request starting HTTP/2 POST http://127.0.0.1:55251/AzureFunctionsRpcMessages.FunctionRpc/EventStream application/grpc 
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
          Executing endpoint 'gRPC - /AzureFunctionsRpcMessages.FunctionRpc/EventStream' 
[2021-12-22T07:38:59.183Z]  
[2021-12-22T07:38:59.183Z]   "ProcessId": 3352, 
[2021-12-22T07:38:59.184Z]   "WorkerVersion": "1.3.1.0", 
[2021-12-22T07:38:59.185Z]   "ProductVersion": "1.3.1\u002B9d5d2f326090a62d4496dfb45611fb696b8960e9", 
[2021-12-22T07:38:59.185Z]   "FrameworkDescription": ".NET 5.0.13", 
[2021-12-22T07:38:59.186Z]   "OSDescription": "Microsoft Windows
    10.0.19044", 
[2021-12-22T07:38:59.186Z]   "OSArchitecture": "X64", 
[2021-12-22T07:38:59.187Z]   "RuntimeIdentifier": "win10-x64", 
[2021-12-22T07:38:59.187Z]   "CommandLine": "C:\\Sources\\Comin-API\\ByCN.ComIn\\src\\00-WEB\\02-FunctionApps\\ByCN.ComIn.FunctionApps.Data\\bin\\Debug\\net5.0\\ByCN.ComIn.FunctionApps.Data.dll
    --host 127.0.0.1 --port 55251 --workerId 66a7fb37-4a35-4e8c-99cf-7c294ca7aea7 --requestId 3be30260-aca2-4e29-b9e8-93a35f06566f --grpcMaxMessageLength 2147483647" 
[2021-12-22T07:38:59.188Z]  
[2021-12-22T07:38:59.200Z] Worker process started and initialized.

它停在这里,尽管脚本应该每分钟循环一次,但没有其他行在写之后

问候

【讨论】:

请阅读我的解决方案和其他解决方案 - 它现在原生支持,您无需执行这些额外步骤。不需要#if#endif 预处理器指令。注意:Ctrl-F5 无需调试即可启动。尝试删除预处理器指令并仅使用 F5 进行正常调试。 好的,我已经解决了问题,这是我们的配置。谢谢。

以上是关于如何在 Visual Studio 中使用 .NET 5(独立进程)调试 Azure Functions?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Visual Studio 2008 中打开使用 Visual Studio 2005 创建的 rdl?

visual studio 中 如何还原.bak数据文件

如何在visual studio开发android程序

visual studio 2012中如何查看一个工程需要哪些dll文件???

如何在Visual Studio Code 中运行 Python

visual studio 如何生成动态库