當前位置: 妍妍網 > 碼農

dotnet-exec 0.18.0 Released

2024-03-05碼農

dotnet-exec 0.18.0 Released

Intro

dotnet-exec 是一個 dotnet tool 一個命令列小工具,可以幫助我們方便地執行 C# 程式碼,可以用來執行一些簡單的 C# 程式而無需建立計畫檔,讓 C# 像 python/nodejs 一樣簡單,而且可以自訂計畫的入口方法,支持但不限於 Main 方法。

0.18.0 開始支持了 .NET 9 框架,可以用來測試一些 .NET 9 的一些 API

Install/Update

dotnet-exec 是一個 dotnet tool,可以使用安裝 dotnet tool 的命令來安裝

安裝/更新最新穩定版本:

dotnet tool update -g dotnet-execute

安裝最新的 preview 版本:

dotnet tool update -g dotnet-execute --prerelease

執行 dotnet-exec -h 或者 dotnet-exec --help 即可看到一些使用說明

dotnet-exec help

也可以透過 docker/podman 來在不安裝 dotnet sdk 的情況下體驗,例如:

docker run --rm --pull=always weihanli/dotnet-exec:0.18.1 "ApplicationHelper.RuntimeInfo"

docker

Features

net9.0 target support

這一版本中新增了 net9.0 的 target,安裝了 .NET 9 SDK 的環境在安裝 tool 的時候就會自動使用 .NET 9 target

從這一版本開始 docker image 也從之前 .NET 8 升級到了 .NET 9

Export compile result

這一版本中新增了 --compile-out 來匯出編譯的 dll,可以指定一個目錄,或者一個具體的檔名,範例如下:

de-compile

Multi-platform container image

從這一版本開始支持了多平台的容器映像,之前的版本只支持 linux/amd64 架構,0.18.0 版本新增支持了 linux/arm64 linux/arm

docker-images

linux/amd64 架構映像的範例

docker-linux/amd64

arm64 架構映像的範例

docker-linux/arm64

大家如果有樹莓派之類的 arm 架構的裝置也可以試一下哈

Enhancements

ProjectReference enhancement

ProjectReference 的原理是 build 計畫檔並參照 build 之後生成的程式集,但是最近在使用的過程中發現有時候會有檔占用的報錯,重試大機率會成功,導致 CI 偶爾會報錯,於是在 build project 的邏輯加了自動 retry,最多 retry 5 次,這樣大大提高了 project reference 的成功率,加了 retry 之後,CI 就沒再報錯了

System.Net.Http.Json namespace using

最近覺得 System.Net.Http.Json 這個名稱空間還是蠻常用的,所以考慮預設包含了這個名稱空間,這樣在使用 GetFromJsonAsync / PostAsJsonAsync 等方法時就可以不再需要加名稱空間了

如果你遇到了名稱空間沖突的問題,也可以使用 --using "-System.Net.Http.Json" 來移除這個名稱空間參照

More

在釋出了 0.18.0 之後,早上發現 Roslyn 釋出了一個新版本還是一個穩定版本,於是升級了一個小版本 0.18.1

這一版本和 0.18.0 的功能一致,只是 Roslyn 的版本升級了一下

References

  • https://github.com/WeihanLi/dotnet-exec

  • https://www.nuget.org/packages/dotnet-execute/

  • https://hub.docker.com/r/weihanli/dotnet-exec

  • https://github.com/WeihanLi/dotnet-exec/compare/0.17.0...0.18.0

  • https://github.com/WeihanLi/dotnet-exec/compare/0.18.0...0.18.1