【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

 

posted @ 2016-05-23 12:10  iMhager  阅读(2045)  评论(0编辑  收藏  举报