-
Notifications
You must be signed in to change notification settings - Fork 0
MS_DotNetCommand
.NET Core の dotnet コマンドを使い倒す。
補足(dotnet コマンドの立ち位置):
dotnetは
SDK と実行ホストを兼ねた単一の入口である。dotnet new … テンプレートからプロジェクトを作る dotnet restore … 依存パッケージを取得(NuGet) dotnet build … ビルド(内部で MSBuild を呼ぶ) dotnet test … テスト実行 dotnet publish … 発行(配置用の成果物を作る) dotnet run … ビルドして実行(開発時) dotnet App.dll … 発行済みアプリの実行(本番時)Visual Studio に依存せずに
同じことが CLI で完結することが、
クロスプラットフォーム化と CI/CD の前提になっている。
$ dotnet --info
.NET Command Line Tools (2.0.0)
Product Information:
Version: 2.0.0
Commit SHA-1 hash: cdcd1928c9
Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
Base Path: /usr/share/dotnet/sdk/2.0.0/
Microsoft .NET Core Shared Framework Host
Version : 2.0.0
Build : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d
補足:
dotnet --infoはインストール済みの SDK とランタイムの
一覧も表示する(現在の版)。
「ビルドは通るのに実行できない」といった問題の切り分けで最初に見る。dotnet --list-sdks # SDK の一覧 dotnet --list-runtimes # ランタイムの一覧なお、どの SDK を使うかは
global.jsonで固定できる
(後述のテンプレート一覧にもglobaljsonがある)。
チーム内で SDK のバージョンを揃えたい場合に使う。
現時点でサポートされているテンプレートを知ることが出来る。
>dotnet new --help
Templates Short Name Language Tags
----------------------------------------------------------------------------------------------------------------------------------
Console Application console [C#], F#, VB Common/Console
Class library classlib [C#], F#, VB Common/Library
WPF Application wpf [C#] Common/WPF
WPF Class library wpflib [C#] Common/WPF
WPF Custom Control Library wpfcustomcontrollib [C#] Common/WPF
WPF User Control Library wpfusercontrollib [C#] Common/WPF
Windows Forms (WinForms) Application winforms [C#] Common/WinForms
Windows Forms (WinForms) Class library winformslib [C#] Common/WinForms
Worker Service worker [C#] Common/Worker/Web
Unit Test Project mstest [C#], F#, VB Test/MSTest
NUnit 3 Test Project nunit [C#], F#, VB Test/NUnit
NUnit 3 Test Item nunit-test [C#], F#, VB Test/NUnit
xUnit Test Project xunit [C#], F#, VB Test/xUnit
Razor Component razorcomponent [C#] Web/ASP.NET
Razor Page page [C#] Web/ASP.NET
MVC ViewImports viewimports [C#] Web/ASP.NET
MVC ViewStart viewstart [C#] Web/ASP.NET
Blazor Server App blazorserver [C#] Web/Blazor
ASP.NET Core Empty web [C#], F# Web/Empty
ASP.NET Core Web App (Model-View-Controller) mvc [C#], F# Web/MVC
ASP.NET Core Web App webapp [C#] Web/MVC/Razor Pages
ASP.NET Core with Angular angular [C#] Web/MVC/SPA
ASP.NET Core with React.js react [C#] Web/MVC/SPA
ASP.NET Core with React.js and Redux reactredux [C#] Web/MVC/SPA
Razor Class Library razorclasslib [C#] Web/Razor/Library/Razor Class Library
ASP.NET Core Web API webapi [C#], F# Web/WebAPI
ASP.NET Core gRPC Service grpc [C#] Web/gRPC
dotnet gitignore file gitignore Config
global.json file globaljson Config
NuGet Config nugetconfig Config
Dotnet local tool manifest file tool-manifest Config
Web Config webconfig Config
Solution File sln Solution
Protocol Buffer File proto Web/gRPC
Examples:
dotnet new mvc --auth Individual
dotnet new webconfig
dotnet new --help
補足(最新化): テンプレートの一覧は SDK の版によって変わる。
現在の主な差分は次のとおり。
変化 内容 一覧の表示 dotnet new --help→dotnet new listSPA テンプレート angular/react/reactreduxは削除(原文の .NET 6への移行 が「非推奨」としていたとおり)Blazor blazorserver/blazorwasm→blazorに統合(レンダリング モードを選ぶ方式)追加 .NET Aspire関連(aspire-*)、webapiaotなど
--auth Individualのようなオプションもテンプレートごとに違い、
dotnet new mvc --helpで確認できる。
以下の様に使用している。
dotnet msbuild /p:Configuration=%BUILD_CONFIG% -v:d ".\xxxx.sln"
- 参考
補足(
dotnet buildとの違い):dotnet buildは
内部で MSBuild を呼ぶラッパーであり、
暗黙にrestoreを実行するなどの気配りが入っている。dotnet build -c Release # 通常はこちら dotnet msbuild /p:... /t:... # MSBuild のターゲットを直接指定したいとき原文が
dotnet msbuildを使っているのは、
ターゲットやプロパティを細かく制御するためと解される。
-v:d(詳細ログ)はビルドが落ちる原因を追うときに有用。
詳細は MSBuild を参照。
- 以下のエラーが発生する場合、
Error:
An assembly specified in the application dependencies manifest (hogehoge.deps.json) was not found:
- 以下の SCD 設定で発行する。
$ dotnet publish -c Release -r XXXX --self-contained- XXXX は、RuntimeIdentifiers(RID 値)。
- RuntimeIdentifiers に何を設定すべきか?は以下のコマンドから確認できる。
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="16.04.3 LTS (Xenial Xerus)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 16.04.3 LTS"
VERSION_ID="16.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=xenial
UBUNTU_CODENAME=xenial
補足(deps.json とは何か): 発行時に生成される
依存関係のマニフェストで、「どのアセンブリを、どこから読むか」を
ランタイムに指示するファイルである。App.dll … アプリ本体 App.deps.json … 依存の一覧(これが実行時に読まれる) App.runtimeconfig.json … どのランタイムで動かすか原文のエラーは、deps.json が要求するアセンブリが
実行環境に無いときに出る。典型的には、
- ネイティブ依存(プラットフォーム固有の
.so/.dll)が足りない、- FDD で発行したが、RID 固有のアセットが解決できない、
という状況。SCD にするとすべて同梱されるため解決する、
というのが原文の対処である。補足(最新化): RID の指定は現在
ubuntu.16.04-x64のようなディストリビューション固有の値は非推奨で、
linux-x64のような簡素な RID を使う
(/etc/os-releaseを見る必要は基本的に無くなった)。dotnet publish -c Release -r linux-x64 --self-contained true
- システムコールをトレースして原因を特定する。
$ strace -o log.txt dotnet XXXX.dll- 原因の特定
ログ・ファイルから以下を発見。- カレント・ディレクトリが違っていてファイルを読み込めなかった。
open("SHA256RSA.pfx", O_RDONLY) = -1 ENOENT (No such file or directory)
- bcrypt.dll ファイルが読み込めなかったもよう。
open("/usr/lib/bcrypt.dll.so", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
・・・
open("/usr/lib/libbcrypt.dll", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
補足(この 2 つの発見が示すこと):
straceで見つかった原因は、
いずれも Windows 前提のコードが Linux で破綻する典型例である。
症状 原因 SHA256RSA.pfxが見つからない相対パスがカレント ディレクトリ依存。実行方法で変わる bcrypt.dllを探しに行くWindows の暗号 API (CNG) を P/Invoke している 前者は .NET CoreのDockerコンテナ化 の
「カレント・ディレクトリを変更する」と同じ問題で、
AppContext.BaseDirectoryを基準にするのが正解。後者はWindows 固有の実装が残っていることを示す。
.NET Core ではSystem.Security.Cryptographyが
OS ごとに実装を切り替える(Linux では OpenSSL)ため、
クロスプラットフォームな API を使えば起きない。
strace(Linux)で「何のファイルを開こうとして失敗したか」を見る、
という切り分け手法自体が有用で、
例外メッセージだけでは分からない原因にたどり着ける。なお、現在は
dotnet-trace/dotnet-dump/dotnet-counters
といった .NET 専用の診断ツールが用意されており、
マネージド側の問題はこちらの方が追いやすい。
- Deep-dive into .NET Core primitives: deps.json, runtimeconfig.json, and dll's
https://natemcmaster.com/blog/2017/12/21/netcore-primitives/ - asp.net > core > コンソールアプリを WindowsServer で動作エラー
https://qiita.com/sugasaki/items/3b9c16f3d62ffcf60227
- .NET のランタイム識別子 (RID) のカタログ
https://learn.microsoft.com/ja-jp/dotnet/core/rid-catalog - Linux OS のバージョンを調べる方法 - Qiita
https://qiita.com/spite400k/items/96dfd0d4340afc35a22e
- .NET CLI の概要
https://learn.microsoft.com/ja-jp/dotnet/core/tools/ - dotnet new
https://learn.microsoft.com/ja-jp/dotnet/core/tools/dotnet-new - .NET 診断ツール
https://learn.microsoft.com/ja-jp/dotnet/core/diagnostics/
Tags: 移行, .NET開発, .NET Core
このWikiは「Open棟梁Project」,「OSSコンソーシアム 開発基盤部会」によって運営されています。