Ubuntu 16.04 LTS で ASP.NET Core を試してみる

Ubuntu 16.04 LTS で ASP.NET Core を試してみる

 

そういえばそんな話もあったよね・・・ ということで試してみる

開発環境は Windows 10 Pro x64 + VS2017 Community

 

参考にしたのは以下のサイト

Nginx 搭載の Linux で ASP.NET Core をホストする

.NET Core で dll ではなく、exe を作りたい

Get started with ASP.NET Core以下

dotnet publish

 

・インストール

All Downloads

から current を選択後 ubuntu のバージョンを選択

Install .NET Core 2.1 SDK (v2.1.301) on Linux Ubuntu 16.04

を参考に

$ wget -q https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install dotnet-sdk-2.1
~
取得:1 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 liburcu4 amd64 0.9.1-3 [47.3 kB]
取得:2 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 liblttng-ust-ctl2 amd64 2.7.1-1 [72.2 kB]
取得:3 http://jp.archive.ubuntu.com/ubuntu xenial/universe amd64 liblttng-ust0 amd64 2.7.1-1 [127 kB]
取得:4 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 dotnet-runtime-deps-2.1 amd64 2.1.1-1 [2,604 B]
取得:5 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 dotnet-host amd64 2.1.1-1 [36.4 kB]
取得:6 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 dotnet-hostfxr-2.1 amd64 2.1.1-1 [143 kB]
取得:7 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 dotnet-runtime-2.1 amd64 2.1.1-1 [20.5 MB]
取得:8 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 aspnetcore-runtime-2.1 amd64 2.1.1-1 [22.1 MB]
取得:9 https://packages.microsoft.com/ubuntu/16.04/prod xenial/main amd64 dotnet-sdk-2.1 amd64 2.1.301-1 [87.5 MB]
~
This software may collect information about you and your use of the software, and send that to Microsoft.
Please visit http://aka.ms/dotnet-cli-eula for more information.
Welcome to .NET Core!
---------------------
Learn more about .NET Core: https://aka.ms/dotnet-docs
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli-docs

Telemetry
---------
The .NET Core tools collect usage data in order to help us improve your experience. 
The data is anonymous and doesn't include command-line arguments. The data is collected 
by Microsoft and shared with the community. 
You can opt-out of telemetry by setting the 
DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET Core CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

Configuring...
--------------
A command is running to populate your local package cache to improve restore speed 
and enable offline access. This command takes up to one minute to complete and only runs once.
libc-bin (2.23-0ubuntu10) のトリガを処理しています ...

microsoft-prod.list と msprod.list が同一なので microsoft-prod.listを削除

※プロダクトごとに名前を変えるなって気もする

 

ASP.NET Core のアプリを作成して Ubuntu で動かしてみる

.NET Core

 -> ASP.NET Core Web アプリケーション

  -> Web アプリケーション

> dotnet publish -c Release -r ubuntu.16.04-x64 Core0001\Core0001.csproj
~
Restore completed in 1.27 min for C:\Dev\code\vs2017\Core0001\Core0001\Core0001.csproj.
Core0001 -> C:\Dev\code\vs2017\Core0001\Core0001\bin\Release\netcoreapp2.0\ubuntu.16.04-x64\Core0001.dll
Core0001 -> C:\Dev\code\vs2017\Core0001\Core0001\bin\Release\netcoreapp2.0\ubuntu.16.04-x64\publish\

publish フォルダごと WinSCPUbuntu にコピーする

※sambaが楽なんだろうなぁ・・・

$ cd publish
$ dotnet Core0001.dll
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {76244e4e-d7d6-451d-a0cd-022e99508643} may be persisted to storage in unencrypted form.
Hosting environment: Production
Content root path: /home/tabizou/dev/code/core/publish
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

ブラウザで http://localhost:5000 をアクセスして動作を確認