【NetCore入門】centos7.x下搭建netcore環境和helloworld的demo


環境:

  1、centos7

  2、dotnet core 1.0.0-preview1-002702

 

步驟:

  1、安裝環境(在非root權限下安裝即可,如果用root,后續用Vscode的時候,權限上有問題【至少我這邊測試的時候是這樣】),指定安裝目錄為/opt/dotnet

  

curl -sSL https://raw.githubusercontent.com/dotnet/cli/rel/1.0.0-preview1/scripts/obtain/dotnet-install.sh | 
bash /dev/stdin --version 1.0.0-preview1-002702 --install-dir /opt/dotnet

  2、創建軟連接(需要提權)

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

 

  3、創建項目

# 創建文件夾
mkdir hwapp

# 進入文件夾
cd hwapp

#創建項目
dotnet new

 

  4、還原nuget依賴,編譯和運行項目

#還原nuget依賴
dotnet restore

# 編譯和運行
dotnet run

 

  5、增加自己測試代碼    

//UserModel.cs

using System;
namespace ConsoleApplication
{
    public class UserModel
    {
        public int UserId { get; set; }

        public string UserName { get; set; }

        public DateTime Birthday { get; set; }
    }
}

//UserService.cs

using System;
using System.Collections.Generic;
namespace ConsoleApplication
{
    public class UserService
    {

        private static List<UserModel> UserList;

        public UserService()
        {
            UserList = new List<UserModel>(){
            new UserModel(){UserId=1,UserName="zhangsan",Birthday=new DateTime(2016,05,23)},
            new UserModel(){UserId=2,UserName="lisi",Birthday=new DateTime(2016,05,23)},
            new UserModel(){UserId=3,UserName="wangwu",Birthday=new DateTime(2016,05,23)},
            new UserModel(){UserId=4,UserName="zhaoliu",Birthday=new DateTime(2016,05,23)},
        };
        }

        public UserModel GetUser(int id)
        {
            if (id < 0) return null;

            var user = UserList.Find(p => p.UserId == id);
            if (user == null)
                return null;

            return user;
        }
    }
}


//program.cs
using System;
namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            UserService service = new UserService();            
            for (int i = 1; i < 4; i++)
            {
                var user=service.GetUser(i);
               Console.WriteLine($"[userid={user.UserId}]{user.UserName},{user.Birthday:yyyy-MM-dd HH:mm:ss}");
            }
        }
    }
}

 

  6、修改完代碼,再次執行 dotnet run ,運行結果(運行方式一,運行方式二見下面)

[hager@localhost hwapp]$ dotnet run
Project hwapp (.NETCoreApp,Version=v1.0) will be compiled because inputs were modified
Compiling hwapp for .NETCoreApp,Version=v1.0

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

Time elapsed 00:00:07.1856909
 

Hello World!
[userid=1]zhangsan,2016-05-23 00:00:00
[userid=2]lisi,2016-05-23 00:00:00
[userid=3]wangwu,2016-05-23 00:00:00
[hager@localhost hwapp]$ 

 

運行方式二:

  在VScode下,按F5,如果VSCode沒有安裝過 DotNet Core Debugger ,會自動安裝。然后在output面板下,輸出結果:

Hello World!
[userid=1]zhangsan,2016-05-23 00:00:00
[userid=2]lisi,2016-05-23 00:00:00
[userid=3]wangwu,2016-05-23 00:00:00
The program '/home/hager/hwapp/bin/Debug/netcoreapp1.0/hwapp.dll' has exited with code 0 (0x00000000).

 

感覺DotNetCore 的組件化還是挺牛逼的,靈活配置,按需依賴加載。

 

踩坑:

 1、在安裝SDK是,全部都是root權限,但是在vscode中出現了點小意外,不知為何提示我dotnet command不存在。估計是因為root下安裝的在hager用戶下找不到這個命令;而且默認dotnet安裝在了/root/dotnet 這個目錄下。應該是普通用戶沒有root/文件夾權限。后來用普通用戶安裝SDK,VScode,后運行沒問題了。

 

參考:

https://www.microsoft.com/net/core#centos

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM