0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.NETのお勉強2_ホットリロード

Last updated at Posted at 2025-01-28

概要

AmazonLinux2023インスタンス上にコンテナで.NETを利用したサーバをたてる
そのお勉強メモ2

C#だから当然と言えば当然だけど、phpと違って毎度ビルドしないといけないの超面倒
どうにかしたい

ホットリロード

sdkイメージにも簡易なwebサーバが入っているらしく、これを使うことでビルド成果物をruntimeイメージ通さずに = 都度ビルドせず更新内容の反映が可能

ソース

hotload:
	docker compose -f docker-compose.hotload.yml up -d
docker-compose.hotload.yml
services:
  build:
    image: builder
    build:
      context: .
      dockerfile: Dockerfile.build
    volumes:
      - ./src:/src
    ports:
      - "80:8080"
    command: dotnet watch run --urls=http://0.0.0.0:8080

実行テスト

make hotload実行してコンテナを立ち上げ、xx.xx.xx.xx/helloアクセスで「Hello from API!」表示を確認した後、HelloController.csの出力メッセージを変更してブラウザ更新後に変更が反映されてればok

注意点

万能ではないっぽい
もちろん本番環境はビルド後プロジェクトをruntimeイメージで動かす必要があるし、変更内容によってはビルドし直しも必要

変更内容 再ビルドの必要性 即時反映
メソッドの中身を変更 必要なし 可能
既存のコントローラーのロジック修正 必要なし 可能
新しいクラスやコントローラーの追加 必要 不可
プロジェクトファイル(.csproj)の変更 必要 不可

トラブルシューティング

ホットリロードした内容でエラーが出る場合はコンテナログを確認すればエラーログが出力される
逆に問題がなければ以下のメッセージがログに出力される

dotnet watch 🔥 [MyServerApp (net9.0)] Hot reload succeeded.
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?