LoginSignup
0
1

More than 3 years have passed since last update.

ASP.NET Core webアプケーションを公開する

Posted at

目的

ASP.NET Coreで作成したwebアプケーションをRaspberry pi上で実行し、PCからアクセスする

対象物

以下記事で作成したチャットアプリを公開する
https://qiita.com/takmot/items/7fc13b50a3c8d0f6d2aa

dotnet runで実行する場合

Properties/launchSettings.jsonを以下のように変更


{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49801",
      "sslPort": 44357
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "SignalRChat": {
      "commandName": "Project",
      "launchBrowser": true,
-     "applicationUrl": "https://localhost:5001;http://localhost:5000", // 変更前
+     "applicationUrl": "https://0.0.0.0:5001;http://0.0.0.0:5000",     // 変更後
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

実行結果
1.png

publishによって発行したファイルを実行する場合

Properties/launchSettings.jsonの変更では、publishで発行した場合に設定が反映されなかった
そのため、Program.csを以下のように変更


namespace SignalRChat
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>()
                    .UseUrls("http://0.0.0.0:5001"); // 追加
                });
    }
}

Raspberry pi準備

Raspberry piに.NET Coreをインストール

公式ドキュメント通りでうまくいかなかったため、以下記事を参照
https://qiita.com/logikuma/items/de8c987dc2308a96256d

以下抜粋
下記コマンドを実行し、必要な事前取得パッケージをインストールする

sudo apt-get install curl libunwind8 gettext

下記コマンドを実行し、.NET Core ランタイムをCurlでダウンロードする

curl -sSL -o dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/Runtime/release/2.0.0/dotnet-runtime-latest-linux-arm.tar.gz

下記コマンドを実行し、.NET Coreランタイムをインストールする

sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet

下記コマンドを実行し、「dotnet」コマンドを実行可能にする

sudo ln -s /opt/dotnet/dotnet /usr/local/bin

下記コマンドを実行し、結果を確認する

dotnet --help.

Raspberry pi上での実行

  1. dotnet publish -r linux-armでRaspberry pi用に実行ファイルを生成
  2. bin\Debug\netcoreapp3.1\linux-armに生成されたpublishフォルダをzip圧縮して、 Raspberry piにFTP等で転送
  3. 転送したファイルをunzip publish
  4. publishフォルダの権限を変更chmod 755 -R publish
  5. アプリを実行./SignalRChat

実行結果

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://0.0.0.0:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /home/pi/work/DGC/publish

http://localhost:5001ではなくhttp://0.0.0.0:5001になっていれば成功

0
1
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
1