Visual Studio 2019遠程除錯 Linux Docker中的 .net core應用程式
Visual Studio 2019遠程除錯 Linux Docker中的 .net core應用程式
在國內的服務器上進行遠程除錯還是比較麻煩的,因為我們沒辦法正常通過Curl獲取到VsDbg包,或者下載極其緩慢,po主是完全獲取不到,直接報錯下載例外,下邊呢,我就帶大家一步一步的重現問題,以及我解決該問題的方法
步驟還原(多圖預警)
第一步:點擊選單欄中的除錯

第二部:附加到行程

第三步:選擇要除錯服務所在的Docker鏡像

第四步:找到 dotnet行程并附加

然后,我們就得到了這個錯誤提示

研讀 VS Code遠程除錯方案(Aaron Powell大神原文),找到了GetVsDbg.sh(下載)腳本檔案下載地址,此時下載該檔案,并手動放入Docker鏡像中,參考上圖報錯資訊,填寫命令,
#首先進入鏡像的掛載檔案夾,移動sh腳本到/root/.vs-debugger檔案夾
mv GetVsDbg.sh /root/.vs-debugger
#原封不動的運行報錯中的腳本命令
/bin/sh "/root/.vs-debugger/GetVsDbg.sh" -v vs2019 -l "/root/.vs-debugger/vs2019" -a "/remote_debugger"
#然后這邊會提示安裝資訊
Info: Previous installation at '/root/.vs-debugger/vs2019' not found
Info: Using vsdbg version '16.9.20122.2'
Using arguments
Version : 'vs2019'
Location : '/root/.vs-debugger/vs2019'
SkipDownloads : 'false'
LaunchVsDbgAfter : 'false'
RemoveExistingOnUpgrade : 'false'
Info: Using Runtime ID 'linux-x64'
HTTP/2 200
Downloading https://vsdebugger.azureedge.net/vsdbg-16-9-20122-2/vsdbg-linux-x64.tar.gz
#報錯了,沒法下載
ERROR: Could not download https://vsdebugger.azureedge.net/vsdbg-16-9-20122-2/vsdbg-linux-x64.tar.gz
Ok,發現問題所在了,Docker中 vsdbg-linux-x64.tar.gz 下載失敗了,這就好說了,復制命令列中的下載地址,在瀏覽器中下載,
然后接下來我們打開GetVsDbg.sh檔案看看他都運行了哪些命令,以及 vsdbg-linux-x64.tar.gz 這個檔案下載好后都做了哪些操作,
轉到402行
download()
{
if [ "$__UseZip" = false ]; then
vsdbgFileExtension=".tar.gz"
else
echo "Warning: Version '${__VsDbgMetaVersion}' is only avaliable in zip."
vsdbgFileExtension=".zip"
fi
vsdbgCompressedFile="vsdbg-${__RuntimeID}${vsdbgFileExtension}"
target="$(echo "${__VsDbgVersion}" | tr '.' '-')"
url="https://vsdebugger.azureedge.net/vsdbg-${target}/${vsdbgCompressedFile}"
check_internet_connection "$url"
echo "Downloading ${url}"
if hash wget 2>/dev/null; then
wget -q "$url" -O "$vsdbgCompressedFile"
elif hash curl 2>/dev/null; then
curl -s "$url" -o "$vsdbgCompressedFile"
fi
#例外就是這里拋出的
if [ $? -ne 0 ]; then
echo
echo "ERROR: Could not download ${url}"
exit 1;
fi
__VsdbgCompressedFile=$vsdbgCompressedFile
}
看到了報錯的地方,然后我們再往下看,這個下載方法是在509行呼叫的
if [ "$__UseAltDebuggerLocation" = false ]; then
if [ "$__SkipDownloads" = true ]; then
echo "Info: Skipping downloads"
else
prepare_install_location
cd "$__InstallLocation" || fail "Command failed: 'cd \"$__InstallLocation\"'"
# For the rest of this script we can assume the working directory is the install path
# Check to see if we already have a compressed file to extract, if not, we need to download it.
if [ -z "$__VsdbgCompressedFile" ]; then
if [ -z "$__RuntimeID" ]; then
get_dotnet_runtime_id
elif [ "$__ExactVsDbgVersionUsed" = "false" ]; then
# Remap the old distro-specific runtime ids unless the caller specified an exact build number.
# We don't do this in the exact build number case so that old builds can be used.
remap_runtime_id
fi
echo "Info: Using Runtime ID '$__RuntimeID'"
#########################此處呼叫下載方法#################################
#########################此處呼叫下載方法#################################
#########################此處呼叫下載方法#################################
download
fi
extract
echo "$__VsDbgVersion" > success.txt
# per greggm, this 'cd' can fail sometimes and is to be expected.
# shellcheck disable=SC2164
cd "$__InitialCWD"
echo "Info: Successfully installed vsdbg at '$__InstallLocation'"
fi
fi
解讀腳本可知,在這個 __VsdbgCompressedFile 變數沒有值的時候,他會喚醒下載,然而該變數的賦值只有兩種途徑,一種是通過 腳本命令 -e 直接賦值,另外一種就是呼叫 download() 方法進行賦值,
得知這些以后,我選擇的是改動他的sh腳本的 download() 方法,洗掉掉下載程序,直接把檔案放在正確位置讓其繼續進行后續步驟即可
修改后的 download() 方法
download()
{
if [ "$__UseZip" = false ]; then
vsdbgFileExtension=".tar.gz"
else
echo "Warning: Version '${__VsDbgMetaVersion}' is only avaliable in zip."
vsdbgFileExtension=".zip"
fi
vsdbgCompressedFile="vsdbg-${__RuntimeID}${vsdbgFileExtension}"
echo "file: ${vsdbgCompressedFile}"
#target="$(echo "${__VsDbgVersion}" | tr '.' '-')"
#url="https://vsdebugger.azureedge.net/vsdbg-${target}/${vsdbgCompressedFile}"
# check_internet_connection "$url"
# echo "Downloading ${url}"
#if hash wget 2>/dev/null; then
# wget -q "$url" -O "$vsdbgCompressedFile"
#elif hash curl 2>/dev/null; then
# curl -s "$url" -o "$vsdbgCompressedFile"
#fi
#if [ $? -ne 0 ]; then
# echo
# echo "ERROR: Could not download ${url}"
# exit 1;
#fi
__VsdbgCompressedFile=$vsdbgCompressedFile
echo "file downloaded: ${__VsdbgCompressedFile}"
}
然后,把 從瀏覽器下載的 vsdbg-linux-x64.tar.gz 檔案上傳到docker鏡像,并且移動到 /root/.vs-debugger/vs2019/ 檔案夾,再次運行 /bin/sh “/root/.vs-debugger/GetVsDbg.sh” -v vs2019 -l “/root/.vs-debugger/vs2019” -a “/remote_debugger” 命令,

大功告成!安裝完畢!
讓我們從頭操作,再次通過VS2019附加到Docker鏡像中的行程,此時發現沒有報錯,我們測個介面試試

Bang! 成功進入除錯
至此,問題解決~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/272038.html
標籤:區塊鏈
上一篇:域乎X螞蟻鏈:打造新購物模式?
