微软最近正式发布了.NET Core 1.0、它支持Windows、OS X和Linux三种操作系统。其中.NET Core最受瞩目,这是一款跨平台、开源且模块化的.NET平台,可以用来搭建web应用、微服务、创立应用库和控制台应用。
.NET Core的组成:
- .NET runtime:提供运行环境,汇编码加载,垃圾回收等基础服务。
- 框架库:完全整合传统的.NET标准库。提供基础数据类型和不同类型应用的基础组件等。
- .NET Core SDK:相关系列的SDK工具和语言编译器
- .NET Core应用的命令行工具集。
Ubuntu 16.04 安装 .NET Core
依次执行如下命令安装 .Net Core
1 2 3 4 |
$ sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' $ sudo apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893 $ sudo apt-get update $ sudo apt-get install dotnet-dev-1.0.0-preview2-003121 |
.NET Core 各个平台的安装:https://www.microsoft.com/net/core
Hello World
创建一个项目目录:
1 2 |
$ mkdir hello-world $ cd hello-world |
创建一个.NET Core项目:
1 |
$ dotnet new |
restore项目依赖:
1 |
$ dotnet restore |
编译运行项目:
1 |
$ dotnet run |
搭建ASP.NET Core网站
有个这个东西,我们就可以在Linux上挂ASP网站了。一个例子:
创建项目目录:
1 2 3 |
$ sudo mkdir -p /srv/aspnet/example.com $ sudo chown -R :www-data /srv/aspnet $ sudo chmod -R g+s /srv/aspnet |
下载一个ASP.NET例子:
1 2 |
$ git clone https://github.com/aspnet/cli-samples $ cd cli-samples/HelloMvc |
编译:
1 2 |
$ dotnet restore $ sudo dotnet publish -c Release -o /srv/aspnet/example.com |
使用www-data用户运行:
1 |
$ sudo -u www-data bash -c "dotnet /srv/aspnet/example.com/HelloMvc.dll" |
访问:http://localhost:5000:
后续工作:
- 设置nginx做为ASP.NET Core网站的反向代理
- 把HelloMvc设置为后台服务,可以使用 supervisor
nginx的配置文件大似如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1:5000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection keep-alive; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } |
到此,就在Ubuntu上搭建了一个完整的ASP.NET网站。
其它资源
- 编码工具:VS Code
- 更多例子代码:https://github.com/dotnet/core/
- ASP.NET文档:https://docs.asp.net/en/latest/getting-started.html
- .NET文档:http://docs.microsoft.com/dotnet
- 交互式学习教程:https://www.microsoft.com/net/tutorials/csharp/getting-started
- .Net Core常用库:https://github.com/thangchung/awesome-dotnet-core