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

How can I debug Azure Functions using .NET 5 (isolated process) in Visual Studio?

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

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

The updated steps from Microsoft are as follows:

Visual Studio integrates with Azure Functions Core Tools so that you can test your functions locally using the full Azure Functions runtime.

  • To run your function, press F5 in Visual Studio. You might need to enable a firewall exception so that the tools can handle HTTP requests. Authorization levels are never enforced when you run a function locally.

  • Copy the URL of your function from the Azure Functions runtime output and run the request. A welcome to Functions message is displayed when the function runs successfully and logs are written to the runtime output.

  • To stop debugging, press Shift+F5 in Visual Studio.

After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.


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

(请 获取替代解决方案)

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

  1. 在 Visual Studio
  2. 中打开您的解决方案
  3. 在 Visual Studio 中打开 PowerShell(查看 -> 终端,或 Ctrl-`)
  4. 导航到您的项目cd MyProject
  5. 在启用调试的情况下启动函数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...
  1. 打开附加进程(打开 -> 调试)window,select 在控制台输出中找到的 PID 至此,断点生效,将被命中。

不再需要,因为本机 Visual Studio 支持已添加

有个简单的方法!

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

        using var host = new HostBuilder()
...

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

瞧,你调试了你的函数!

它对我不起作用。 我在 DotnetCore5 中有一个 functionAPPp,我用 Ctrl+F5 运行 它。 我有调试 window,我 select 运行 实例。 调试触发根文件“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);

谁好 运行ning,我在控制台启动时看到它(在我下面的“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.

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

此致