.Net Core 系列:一、环境搭建

  目录:

      一、环境搭建html

      二、ADO.Net 基础docker

      三、ASP.Net Core 基础编程

      四、Entity Framework Core 基础json

      五、实现登陆注册功能windows

      六、实现目录管理功能centos

      七、实现文章发布、编辑、阅览和删除功能bash

      八、实现文章回复功能服务器

      九、实现文章点赞功能app

      十、正式发布文章系统框架


前言:

       2016年6月28日微软宣布发布 .NET Core 1.0、ASP.NET Core 1.0 和 Entity Framework Core 1.0。 .NET Core是微软在两年前发起的开源跨平台.NET框架项目,支持Windows、OS X和Linux平台,可用于开发各类类型的应用。这次发布包括了 .NET Core运行时、库和工具,以及  ASP.NET Core库。微软还释出了Visual Studio和Visual Studio Code扩展,容许开发者建立 .NET Core项目。若是要使用Visual Studio构建 .NET Core应用,开发者须要安装最新的 Visual Studio 2015 Update 32。

       .NET Core 目前还有不少功能没法和传统Framework及mono比。并且在老项目的迁移,各类开源类库的支持上,目前相对匮乏。不少人会有疑问,微软历时2年发布的一个.NET Core、ASP.NET Core、Entity Framework Core 能为咱们解决什么难题?咱们能够是用它们来完成什么业务?

       可是不管什么业务,万行“代码”从Hello Word起。。。。。

1、环境搭建

       不管使用什么语言,编程必需要学习的第一件事情,环境搭建。人类是高级动物,由于人类会使用工具,环境搭建实际上是构造工具的过程。目前官网上(https://www.microsoft.com/net/core#windows)有包含Windows、Linux、Mac、Docker的环境教程,相对仍是很清晰的,可是这个网站说的是开发环境的使用,运行环境呢?并无详细介绍。并且就开发环境来讲对于老NET人,一贯是神器在手(Visual Studio 201×)什么开发环境都一键搞定。可是这个一键搞定在跨平台后就形成了各类的坑。

       习惯上咱们在安装开发环境时安装的是.NET Core SDK,安装运行环境时安装的是.NET Core

       .NET Core SDK = Develop apps with .NET Core and the SDK+CLI (Software Development Kit/Command Line Interface) tools
       .NET Core = Run apps with the .NET Core runtime

       由于本人比较熟悉Linux Centos 7 因此本人将以Centos 7 为基础讲述。

       Centos 7.1 安装开发环境

#依赖包
sudo yum install libunwind libicu
#开发环境下载的仍是preview2
curl -sSL -o dotnet.tar.gz https://go.microsoft.com/fwlink/?LinkID=809131
#官网的是将文件安装到/opt/目录 sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
sudo mkdir -p /usr/share/dotnet-dev-1.0.0 && tar zxf dotnet.tar.gz -C /usr/share/dotnet-dev-1.0.0
#将dotnet 执行程序映射到/usr/bin执行目录;
sudo ln -s /usr/share/dotnet-dev-1.0.0/dotnet /usr/bin

  dotnet --help显示信息以下所示

[root@968a822651a3 hellword]# dotnet --help
.NET Command Line Tools (1.0.0-preview2-003121) Usage: dotnet [host-options] [command] [arguments] [common-options] Arguments: [command] The command to execute [arguments] Arguments to pass to the command [host-options] Options specific to dotnet (host) [common-options] Options common to all commands Common options: -v|--verbose Enable verbose output -h|--help Show help Host options (passed before the command): -v|--verbose Enable verbose output --version Display .NET CLI Version Number --info Display .NET CLI Info Common Commands: new Initialize a basic .NET project restore Restore dependencies specified in the .NET project build Builds a .NET project publish Publishes a .NET project for deployment (including the runtime) run Compiles and immediately executes a .NET project test Runs unit tests using the test runner specified in the project pack Creates a NuGet package

 

  Centos 7.1 安装运行环境

#依赖包
sudo yum install libunwind libicu
#运行环境下载
curl -sSL -o dotnet.tar.gz https://dotnetcli.blob.core.windows.net/dotnet/preview/Binaries/1.0.0/dotnet-centos-x64.1.0.0.tar.gz
#官网的是将文件安装到/opt/目录 sudo mkdir -p /opt/dotnet && sudo tar zxf dotnet.tar.gz -C /opt/dotnet
sudo mkdir -p /usr/share/dotnet-1.0.0 && tar zxf dotnet.tar.gz -C /usr/share/dotnet-1.0.0
#将dotnet 执行程序映射到/usr/bin执行目录;
sudo ln -s /usr/share/dotnet-1.0.0/dotnet /usr/bin

    dotnet --help显示信息以下所示

[root@19b9997445b9 dotnet]# dotnet --help

Microsoft .NET Core Shared Framework Host

  Version  : 1.0.1
  Build    : cee57bf6c981237d80aa1631cfe83cb9ba329f12

Usage: dotnet [common-options] [[options] path-to-application]

Common Options:
  --help                           Display .NET Core Shared Framework Host help.
  --version                        Display .NET Core Shared Framework Host version.

Options:
  --fx-version <version>           Version of the installed Shared Framework to use to run the application.
  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.

Path to Application:
  The path to a .NET Core managed application, dll or exe file to execute.

If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.

To get started on developing applications for .NET Core, install .NET SDK from:
  http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409

    Windows 10 安装开发环境(非vs2015)

    (只有在win10下才能使用命令行的方式执行,在win7下提示一下错误:Failed to load the dll from [C:\Program Files\dotnet\host\fxr\1.0.1\hostfxr.dll], HRESULT: 0x80070057

    下载DotNetCore.1.0.0-SDK.Preview2-x64:https://download.microsoft.com/download/A/3/8/A38489F3-9777-41DD-83F8-2CBDFAB2520C/packages/DotNetCore.1.0.0-SDK.Preview2-x64.exe

    Windows 10 安装运行环境

    下载DotNetCore.1.0.0-Runtime-x64.exe:https://download.microsoft.com/download/A/3/8/A38489F3-9777-41DD-83F8-2CBDFAB2520C/packages/DotNetCore.1.0.0-Runtime-x64.exe

    Docker 安装运行环境

    官网上的Docker是以Debian 为基础镜像的,而我使用Docker为Centos为镜像,因此我必须本身编写Dockerfile来构建我要的docker images

# VERSION 1.0
FROM maydear.com/centos:7 #//其实就是官方镜像改TAG,本身docker pull centos后修改语句
MAINTAINER kelvin "kelvin@maydear.com"

# 更新系统源
RUN yum -y update && yum clean all
# 安装依赖包
RUN yum -y install libunwind libicu

# Install .NET Core
ENV DOTNET_VERSION 1.0.0
ENV DOTNET_DOWNLOAD_URL https://dotnetcli.blob.core.windows.net/dotnet/preview/Binaries/$DOTNET_VERSION/dotnet-centos-x64.$DOTNET_VERSION.tar.gz

RUN curl -sSL -o dotnet.tar.gz $DOTNET_DOWNLOAD_URL

#安装dotnet core
RUN mkdir -p /usr/share/dotnet-$DOTNET_VERSION && tar zxf dotnet.tar.gz -C /usr/share/dotnet-$DOTNET_VERSION

#安装dotnet core 到启动命令
RUN ln -s /usr/share/dotnet-$DOTNET_VERSION/dotnet /usr/bin

RUN rm -rf dotnet.tar.gz
CMD [ "/bin/bash" ]


#build image:
#sudo docker build --rm -t="maydear.com/dotnet:1.0" .

#run container:
#sudo docker run -it --name=dotnet -d -v /xxx:/xxx --privileged=true maydear.com/dotnet:1.0

2、第一个.Net Core 程序

   Centos 7 秒出Hello World

mkdir helloworld
cd helloworld

sudo dotnet new && dotnet restore && dotnet build

sudo dotnet run

    发布dotnet Core 程序

sudo dotnet publish

    拷贝文件到运行服务器,并运行

 3、使用VS2015开发第一个Hello Word

     一、安装Visual Studio Community 2015 Update 3 (略。。)

     二、安装 DotNetCore.1.0.0.RC2-VS2015Tools.Preview1插件。http://download.microsoft.com/download/A/3/8/A38489F3-9777-41DD-83F8-2CBDFAB2520C/DotNetCore.1.0.0-VS2015Tools.Preview2.exe

     三、建立项目

4、总结

     project.json

{
  "version": "1.0.0-*",/*程序版本之前写在AssemblyInfo*/
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {/*依赖关系(引用包)*/
    "Microsoft.NETCore.App": {
        "type": "platform",
        "version": "1.0.0"
    },
    "test": "1.0.2-*"/*我本身定义的NET core 类库,测试类库引用*/
  },

  "frameworks": {
    "netcoreapp1.0": {
        "imports": "dnxcore50"
    }
  }
}

 

从Hello World 上手习惯上惟一的区别就是.NET Core 的引用是经过nuget 引用包,可是nuget若是没有支持.NET Core的包的时候,引起如图的错误:

这个nuget的自动识别,期待在Nuget3.0能有更强的优化。

相关文章
相关标签/搜索