星期二, 8月 30, 2016

.NET Core with CentOS 安裝測試小記

.NET Core with CentOS 安裝小記

因為工作的關係, 之前有同事問 .NET Core 可否在 linux container 內執行?

上星期六有去參加 Microsoft 辦的 Community open camp 活動( https://community-open-camp.azurewebsites.net/ ), 有看到上官 demo Virtual Studio Core with .NET core, 所以本周就來嘗試



OS: CentOS 7.2


安裝 .NET Core SDK


# yum  install  libunwind  libicu
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: centos.cs.nctu.edu.tw
* extras: centos.cs.nctu.edu.tw
* updates: centos.cs.nctu.edu.tw
Package 2:libunwind-1.1-5.el7_2.2.x86_64 already installed and latest version
Package libicu-50.1.2-15.el7.x86_64 already installed and latest version
Nothing to do


# ls
anaconda-ks.cfg  initial-setup-ks.cfg


下載 dotnet.tar.gz
# curl   -sSL   -o   dotnet.tar.gz   https://go.microsoft.com/fwlink/?LinkID=809131


# ls
anaconda-ks.cfg  dotnet.tar.gz  initial-setup-ks.cfg


建立資料夾
# mkdir   /opt/dotnet


解壓縮 dotnet.tar.gz 到 /opt/dotnet


# tar   zxvf   dotnet.tar.gz   -C   /opt/dotnet/

建立 link ( 官方是用 # ln  -s  /opt/dotnet/dotnet   /usr/local/bin , 雖然 link 到目錄會自動建立 dotnet 在 /usr/local/bin 下, 但是我還是習慣完整打出來 )
# ln  -s  /opt/dotnet/dotnet   /usr/local/bin/dotnet


進行測試
$ pwd
/home/max


$ mkdir  hwapp


$ cd    hwapp/


$ dotnet  new


Welcome to .NET Core!
---------------------
Learn more about .NET Core @ https://aka.ms/dotnet-docs. Use dotnet --help to see available commands or go to https://aka.ms/dotnet-cli-docs.
Telemetry
--------------
The .NET Core tools collect usage data in order to improve your experience. The data is anonymous and does not include commandline arguments. The data is collected by Microsoft and shared with the community.
You can opt out of telemetry by setting a DOTNET_CLI_TELEMETRY_OPTOUT environment variable to 1 using your favorite shell.
You can read more about .NET Core tools telemetry @ https://aka.ms/dotnet-cli-telemetry.
Configuring...
-------------------
A command is running to initially populate your local package cache, to improve restore speed and enable offline access. This command will take up to a minute to complete and will only happen once.
Decompressing 100% 3872 ms
Expanding 100% 5549 ms
Created new C# project in /home/max/hwapp.


觀察目錄
$ ls
Program.cs  project.json


觀察相關檔案內容
$ cat   Program.cs
using System;


namespace ConsoleApplication
{
   public class Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
       }
   }
}

$ cat   project.json
{
 "version": "1.0.0-*",
 "buildOptions": {
   "debugType": "portable",
   "emitEntryPoint": true
 },
 "dependencies": {},
 "frameworks": {
   "netcoreapp1.0": {
     "dependencies": {
       "Microsoft.NETCore.App": {
         "type": "platform",
         "version": "1.0.0"
       }
     },
     "imports": "dnxcore50"
   }
 }
}

執行程式
$ dotnet   restore
log  : Restoring packages for /home/max/hwapp/project.json...
log  : Writing lock file to disk. Path: /home/max/hwapp/project.lock.json
log  : /home/max/hwapp/project.json
log  : Restore completed in 1372ms.


觀察目錄
$ ls  -a
.  ..  Program.cs  project.json  project.lock.json


$ dotnet    run
Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling hwapp for .NETCoreApp,Version=v1.0


Compilation succeeded.
   0 Warning(s)
   0 Error(s)


Time elapsed 00:00:02.6575843


Hello World!


觀察目錄
$ ls  -a
.  ..  bin  obj  Program.cs  project.json  project.lock.json


到目前為止是在 console 輸出 Hello World!


但是之後可能是在網頁上面輸出


建立一個新的或是修改舊的來測試也可以, 這邊我是建立一個新的
回到家目錄
$ cd


建立資料夾
$ mkdir  webapp


進入到工作目錄
$ cd   webapp/


透過 dotnet  new 建立新專案
$ dotnet  new
Created new C# project in /home/max/webapp.


$ ls
Program.cs  project.json


修改 project.json


$ vi   project.json
加入 Microsoft.AspNetCore.Server.Kestrel  相關設定
{
 "version": "1.0.0-*",
 "buildOptions": {
   "debugType": "portable",
   "emitEntryPoint": true
 },
 "dependencies": {},
 "frameworks": {
   "netcoreapp1.0": {
     "dependencies": {
       "Microsoft.NETCore.App": {
         "type": "platform",
         "version": "1.0.0"
       },
       "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
     },
     "imports": "dnxcore50"
   }
 }
}


這個部份個人推測是告訴等下的 dotnet  restore 指令要匯入哪些 lib


執行  dotnet  restore
$ dotnet   restore
log  : Restoring packages for /home/max/webapp/project.json...
log  : Writing lock file to disk. Path: /home/max/webapp/project.lock.json
log  : /home/max/webapp/project.json
log  : Restore completed in 1852ms.

建立 Startup.cs
$ vi   Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace ConsoleApplication {
   public class Startup{
       public void Configure(IApplicationBuilder app){
           app.Run(context => {
               return context.Response.WriteAsync("Hello world");
           });
       }
   }
}

修改 Program.cs
$ vi  Program.cs
using System;
using Microsoft.AspNetCore.Hosting;
namespace ConsoleApplication
{
   public class Program
   {
       public static void Main(string[] args)
       {
           Console.WriteLine("Hello World!");
           var host = new WebHostBuilder()
           .UseKestrel()
           .UseStartup<Startup>()
           .Build();
           host.Run();
       }
   }
}


執行 dotnet  run
$ dotnet  run
Project webapp (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling webapp for .NETCoreApp,Version=v1.0


Compilation succeeded.
   0 Warning(s)
   0 Error(s)


Time elapsed 00:00:02.9192331


Hello World!
Hosting environment: Production
Content root path: /home/max/webapp/bin/Debug/netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

這個時候可以看到 程式在 localhost port 5000 listen


開個 firefox 來測試一下
2016-08-30 15-51-00 的螢幕擷圖.png

算是對 .NET Core 有個初步嘗試
但是後續的 code 撰寫還是要透過 Virtual Studio Core 來連接會比較好


先記下來


~ enjoy it

參考文件





沒有留言: