当 Blazor WASM 应用托管在 Kubernetes Pod 中时,如何调试该应用的客户端 Razor 代码?

How to debug Client side Razor code for Blazor WASM app when it is hosted in a Kubernetes Pod?

提问人:Rohit Mistry 提问时间:11/15/2023 更新时间:11/15/2023 访问量:25

问:

我有一个简单的托管 Blazor WASM 应用,它在 Kubernetes Pod 中运行。

该应用程序适用于托管端口的 Ingress + TLS(例如 my-public-hostname:443 -> internal-service-name:5678)

但是,如果我在服务器端启用调试,如何从本地设备调试客户端代码?(例如,使用环境变量"ASPNETCORE_ENVIRONMENT": "Development"

我的调试Dockerfile如下:

# Reference: https://docs.tilt.dev/example_csharp.html
# This is used with Tilt's Live Sync feature to sync code changes to a remote container.
# Dotnet watch takes care of recompiling the code when changes are detected.
FROM mcr.microsoft.com/devcontainers/dotnet:0-7.0

# Install VS Debugger for C# / .NET
# This is needed for VS Code to attach to the running process.
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

# Install Dotnet Tools

# # This is if you want to use the dotnet-script tool. (e.g. execute csx files)
# RUN dotnet tool install --global dotnet-script \

#  # This is if you want to use the dotnet-repl tool. (e.g. Interactive C# like ipython)
#  && dotnet tool install --global dotnet-repl \

#  # This is if you want to use the dotnet-interactive tool. (e.g. Jupyter Notebooks)
#  && dotnet tool install --global Microsoft.dotnet-interactive

# Add "~/.dotnet/tools" to the PATH environment variable.
# This is needed to run the dotnet tools.
ENV PATH="${PATH}:/root/.dotnet/tools"

# Add .NET Environment variables
ENV ASPNETCORE_ENVIRONMENT="Development"

# The source code is not copied to the container, but is mounted from the local machine.
# This is done by Tilt's Live Sync feature.
COPY . /app

WORKDIR /app

# # The dotnet watch command will recompile the code when changes are detected.
# # The entrypoint is also set by Tilt's Live Sync feature.
ENTRYPOINT ["dotnet", "watch", "run", "--launch-profile=https", "--project=Server", "--no-hot-reload", "--non-interactive", "--verbosity=minimal"]
# ENTRYPOINT [ "bash", "-c", "while true; do sleep 10; done" ]

通过上述配置,我有 TiltOkteto 等外部工具,可以将我的本地源代码同步到 Kubernetes Pod 的文件夹中。/app

我可以通过以下配置使用 to ssh 在本地调试服务器端代码:kubectllaunch.json

{
    "name": "Kubectl: .NET Core Kubernetes Attach",
    "type": "coreclr",
    "request": "attach",
    "processName": "/app/Server/bin/Debug/net7.0/my-pwa.Server",
    // "restart": true, // Waiting for: https://github.com/OmniSharp/omnisharp-vscode/issues/4822
    "pipeTransport": {
        "debuggerPath": "/root/vsdbg/vsdbg",
        "pipeCwd": "${workspaceRoot}",
        // This is a workaround for launching kubectl exec with pod name but the script gets the pod name
        "pipeProgram": "${workspaceFolder}/.vscode/kubectl-launch.sh",
        "quoteArgs": false,
        "justMyCode": false
    },
    "sourceFileMap": {
        "/app": "${workspaceRoot}"
    }
},

在哪里:kubectl-launch.sh

#!/bin/bash

# Stop script on error
set -e

# Get Pod name by label selector. app.kubernetes.io/component=webapp and app.kubernetes.io/name=my-pwa
POD_NAME=$(kubectl get pods --namespace my-pwa --selector app.kubernetes.io/component=webapp,app.kubernetes.io/name=my-pwa -o jsonpath="{.items[0].metadata.name}")

# Use Kubectl to execute command inside the pod as arguments from this file
kubectl exec -it $POD_NAME --namespace my-pwa -- $@

但是我无法弄清楚如何使用此设置使用 VS Code 调试客户端 Web 程序集。

我尝试使用以下“launch.json”配置:

{
    "name": "Launch and Debug Standalone Blazor WebAssembly App",
    "type": "blazorwasm",
    "request": "launch",
    "preLaunchTask": "dotnet: build",
    "browser": "edge",
    "hosted": false,
},

这将首先启动 Web 浏览器,服务器也在本地运行。但是我无法弄清楚如何使浏览器的调试实例在"localhost:5678""my-public-hostname:443"


注意:我需要托管的 Web 应用程序在 Kubernetes 中运行,因为它对集群中的其他微服务具有服务器端依赖关系。这些很难使用 Docker-compose 在本地重现,因为还有一个通过服务网格的路由/网络组件。

注意:在这里,我对调试的定义是指在 IDE (VS Code) 中设置断点,并让 IDE 在该断点处停止以检查变量、单步执行代码等。是的,可以在 WASM 客户端进行控制台日志记录,这就是我到目前为止一直在调试的方式,但这变得非常乏味且不可持续。

Kubernetes Blazor-WebAssembly 远程调试

评论


答: 暂无答案