dotnet publish -c Release -r win-x64 --self-contained false
出力先:
bin/Release/net8.0/win-x64/publish/
$exe = "C:\path\to\publish\YourApp.exe" # 実行ファイル
New-Service -Name "MinimalStaticFileServer" `
-BinaryPathName "`"$exe`"" `
-DisplayName "Minimal Static File Server" `
-StartupType Automatic
# 受信許可(例: TCP 8080/8443)
New-NetFirewallRule -DisplayName "MinimalStaticFileServer-HTTP" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow
New-NetFirewallRule -DisplayName "MinimalStaticFileServer-HTTPS" -Direction Inbound -Protocol TCP -LocalPort 8443 -Action Allow
# 起動
Start-Service MinimalStaticFileServer
既に
appsettings.json
で Kestrel の URL を設定していれば、そのポートで待受します。 例:http://0.0.0.0:8080
、https://0.0.0.0:8443
(証明書設定済みなら)
Stop-Service MinimalStaticFileServer
sc.exe delete MinimalStaticFileServer
上記は PowerShell で Windows サービスを登録・起動しているもの。
sc create
と似た役割ですが、仕組みと書き方が少し違います。
$exe = "C:\path\to\publish\YourApp.exe" # 実行ファイルのパスを変数に入れる
# 新しいサービスを登録
New-Service -Name "MinimalStaticFileServer" `
-BinaryPathName "`"$exe`"" `
-DisplayName "Minimal Static File Server" `
-StartupType Automatic
# Windows ファイアウォールで TCP 8080/8443 を開ける
New-NetFirewallRule -DisplayName "MinimalStaticFileServer-HTTP" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow
New-NetFirewallRule -DisplayName "MinimalStaticFileServer-HTTPS" -Direction Inbound -Protocol TCP -LocalPort 8443 -Action Allow
# サービスを起動
Start-Service MinimalStaticFileServer
-
New-Service
- Windows サービスとして exe を登録
- サービス名(内部名) =
"MinimalStaticFileServer"
- 実行ファイル =
$exe
- 表示名(管理ツールに出る名前) =
"Minimal Static File Server"
- 自動起動(Windows 起動と一緒に立ち上がる)
-
New-NetFirewallRule
- Windows ファイアウォールで指定ポートを開放
- 内部の TCP 接続を受けられるようにする
-
Start-Service
- 登録したサービスを起動
-
sc create
→ 古くからある サービス制御コマンド(cmd.exe 系)。sc create MinimalStaticFileServer binPath= "C:\path\to\YourApp.exe" start= auto
-
New-Service
→ PowerShell 版。引数がオブジェクト指向っぽく扱える。
両方とも サービス登録処理をしている点は同じです。 違いは「コマンド体系が cmd か PowerShell か」という程度。
管理者権限で PowerShell を開き、次のように読み込みます:
. "C:\scripts\Manage-StaticFileServer.ps1"
※ 頭の .
(ドット)+スペースを忘れないでください。
これで、そのセッション内に Register-StaticFileServerService
と Unregister-StaticFileServerService
関数が使えるようになります。
Register-StaticFileServerService -BinaryPath "C:\apps\MinimalStaticFileServer\MinimalStaticFileServer.exe"
-BinaryPath
: 実行ファイル(ビルドした ASP.NET Core サーバーの exe)のフルパス
Register-StaticFileServerService `
-BinaryPath "C:\apps\MinimalStaticFileServer\MinimalStaticFileServer.exe" `
-Arguments '--urls "http://*:8080"' `
-DisplayName "Minimal Static File Server" `
-Description "ASP.NET Core static file server (Negotiate auth for logging only)" `
-StartupType Automatic `
-Force
-Arguments
:--urls
など Kestrel の起動引数を渡せる-DisplayName
: サービス管理画面(services.msc)に表示される名前-Description
: サービスの説明-StartupType
: 自動起動 (Automatic
)、手動 (Manual
)、無効 (Disabled
)-Force
: 既に同名サービスがあれば削除して作り直す
登録したサービスを削除するときは:
Unregister-StaticFileServerService
必要なら名前を変えて削除できます:
Unregister-StaticFileServerService -Name "MinimalStaticFileServer"
サービスの状態を見るには:
Get-Service -Name "MinimalStaticFileServer"
開始・停止は普通に PowerShell のコマンドでできます:
Start-Service -Name "MinimalStaticFileServer"
Stop-Service -Name "MinimalStaticFileServer"
✅ まとめると:
- スクリプトを保存 → 読み込み
Register-StaticFileServerService
でサービス登録- 不要になったら
Unregister-StaticFileServerService
で削除