您好,欢迎来到二三娱乐。
搜索
您的当前位置:首页.Net Core入门简介

.Net Core入门简介

来源:二三娱乐

Core And Framework
  1. 2014年7月 - 5)的前身,这时还是alpha阶段
  2. 2014年11月 - 5的名称
  3. 2015年10月 - 发布了beta8,这是最后的一个beta版本,这一年的时间里,微软逐步提供了对于macOS和Linux操作系统的支持;同时随着 Visual Studio Code 5
  4. 2015年11月 - 5基本具备了使用在生产环境的能力
  5. 2016年6月27日 - Core 1.0,标志着微软这一跨平台开发工具的正式发布

core

项目的GitHub地址:

发布计划

  1. 2016.6 发布1.0
  2. 2016秋季 发布1.1

1.1计划

  1. Framewok和Mono在BCL级别上等价。

Version 1.0 OS Support:

OS Version Architectures Configurations
Windows Client 7 SP1 - 10 x64, x86
Windows Server 2008 R2 SP1 - 2016 x64, x86 Full, Server Core, Nano (2016 only)
Red Hat Enterprise Linux 7.2 x64
Fedora 23 x64
Debian 8.2 x64
Ubuntu 14.04 LTS, 16.04 LTS x64
Linux Mint 17 x64
openSUSE 13.2 x64
Centos 7.1 x64
Oracle Linux 7.1 x64
Mac OS X 10.11 (El Capitan) x64

standard

  • Standard Library 生态系统建立更大的统一性。
  • Standard Library实现了以下主要方案:
1. 定义了统一的BCL 
2.  运行时上。
3.  API和那些仅适用于OS的API。

。。(向下兼容)

例如:
Framework 4.6 Standard Library 1.3,那么意味着 Standard Library versions 1.0 through Framework 4.6.2 实现 Standard Library 1.5,while Core 1.0 实现了 Standard Library 1.6.

Standard关系图
Target Platform Name Alias
Platform Standard netstandard 1.0 1.1 1.2 1.3 1.4 1.5 1.6
Core netcoreapp 1.0
Framework net 4.6.3
4.6.2
4.6.1
4.6
4.5.2
4.5.1
4.5
Universal Windows Platform uap 10.0
Windows win 8.1
8.0
Windows Phone wpa 8.1
Windows Phone Silverlight wp 8.1
8.0
Mono/Xamarin Platforms *
Mono *

NuGet

Platform Standard version mapping
Platform Standard version NuGet identifier
1.0 - 1.6 netstandard1.0 - netstandard1.6
Specific platform mapping
Platform NuGet identifier
Framework 2.0 - 4.6 net20 - net46
Core netcoreapp
Micro Framework netmf
Windows 8 win8, netcore45
Windows 8.1 win8, netcore451
Windows Phone Silverlight (8, 8.1) wp8, wp81
Windows Phone 8.1 wpa8.1
Universal Windows Platform 10 uap10.0, netcore50
Silverlight 4, 5 sl4, sl5
MonoAndroid monoandroid
MonoTouch monotouch
MonoMac monomac
Xamarin iOS xamarinios
Xamarin PlayStation 3 xamarinpsthree
Xamarin PlayStation 4 xamarinpsfour
Xamarin PlayStation Vita xamarinpsvita
Xamarin Watch OS xamarinwatchos
Xamarin TV OS xamarintvos
Xamarin Xbox 360 xamarinxboxthreesixty
Xamarin Xbox One xamarinxboxone
弃用的包名
Platform Deprecated NuGet identifier Current NuGet identifier
5.0 on Framework aspnet50 net46
5.0 on Core aspnetcore50 netcoreapp1.0
DNX on Framework 4.5.1 - 4.6 dnx451 - dnx46 net451 - net46
DNX on Core 5.0 dnxcore50 netcoreapp1.0
Standard Application 1.5 netstandardapp1.5 netcoreapp1.0
Platform 5.1 - 5.6 dotnet5.1 - dotnet5.6 netstandard1.0 - netstandard1.5
Platform 5.0 dotnet netstandard1.3
Windows 8 winrt win8 or netcore45

环境搭建

windows

最好的方式:

ps:如果已经安装了 vs2015 update3 Core 1.0.0 - VS 2015 Tooling Preview 2时还是提示没有安装update3,
可以使用“SKIP_VSU_CHECK=1”这个参数忽略vs的检查。
在命令行使用: DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1 进行安装。

其他方式

Linux

Mac

  1. 安装Homebrew
  2. 安装一些依赖环境
brew update
brew install openssl
ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
  1. Core SDK
    最好的方式就是下载pkg的包进行安装

    安装好之后会将工具的路径加入到PATH中。

装好环境后用运行命令 dotnet 进行检验

dotnet命令介绍

  1. dotnet new 初始化一个
  2. dotnet restore 使用NuGet还原在项目文件project.json 中定义的依赖关系和项目特定的工具。
    注:运行 dotnet 还原生成一个锁文件 project.json.lock,其中包括有关所有被恢复的软件包的详细的信息。
  3. dotnet build
build命令会把项目和他所依赖的项目编译成一个二进制文件,默认情况下二进制文件是IL 和.dll 为文件扩展名。编译过程依赖于已经存在锁文件project.json.lock,这是restore命令生成的。
为了生成一个可执行的应用程序,您需要确保该项目配置的编译选项设置应用的入口点︰
 "buildOptions": {
    "emitEntryPoint": true
  }
  1. dotnet run
  2. dotnet pack 创建一个nuget包
pack命令编译项目并生成NuGet包,该操作会生成两个NuGet程序包:
一个包括已编译代码的程序集文件
一个包括调试符号和已编译代码的程序集文件
项目依赖的NuGet项目添加到生成nuspec文件中,默认情况不打包项目之间的引用关系,但可以通过更改项目的相关性类型。
  1. dotnet test 用Test运行工具运行项目中指定的单元测试
  2. dotnet publish
发布命令会编译应用程序并读取项目文件,
然后将结果集的文件发布到一个目录。生成目录的内容将取决于项目的类型,但可以包括一个跨平台的 IL 应用程序和他依赖项,
这就是通常用的Portable部署方式,
 Core运行时环境与程序集依赖,
 Core SDK,
然后用dotnet命令运行程序。或者是每个本机平台的子文件夹或自包含的应用程序,
其中包括目标平台的运行时,这就是Self-contained部署方式。
 Core运行时环境与程序集依赖,
 Core SDK,将应用程序文件夹拷贝过来就能运行。
默认的project.json编译出来的应用没有包括跨平台,需要修改project.json文件,需要在 project.json 加入 runtimes 节点 注释掉 "type": "platform"。
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {"hwapp":"1.0.0"},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
         {
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
     }
    },

   "runtimes":{
      "win7-x64": { },
      "win7-x86": { },
      "osx.10.10-x64": { },
      "osx.10.11-x64": { },
      "ubuntu.14.04-x64":{ },
      "centos.7-x64":{}
   }

}

具体的用法可以通过
dotnet [new|restore|build|run|pack|publish|test] -h 获取

IDE推荐

  1. Visual Studio 2015
  2. Visual Studio Code
  3. Project Rider(JetBrains)
Project Rider简介

DNX projects use a global.json and project.json files to describe a solution and projects. Rider cannot yet open DNX projects based on these files, and requires a .sln file and all projects to also have a .xproj file. These can be generated by opening the solution in Visual Studio. Rider itself does not use these MSBuild files, but talks directly to the DNX Design Time Host. Future builds will open DNX projects based on global.json and project.json.

用dotnet命令

mkdir dotnetcoreapp
cd dotnetcoreapp
dotnet new 
dotnet restore
dotnet run

Project dotnetcoreapp  will be compiled because expected outputs are missing
Compiling dotnetcoreapp for 

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

Time elapsed 00:00:01.4853106
 

Hello World!

项目结构如下所示:

├── Program.cs
├── bin
│   └── Debug
│       └── netcoreapp1.0
│           ├── dotnetcoreapp.deps.json
│           ├── dotnetcoreapp.dll
│           ├── dotnetcoreapp.pdb
│           ├── dotnetcoreapp.runtimeconfig.dev.json
│           └── dotnetcoreapp.runtimeconfig.json
├── obj
│   └── Debug
│       └── netcoreapp1.0
│           ├── dotnet-compile-csc.rsp
│           ├── dotnet-compile.assemblyinfo.cs
│           └── dotnet-compile.rsp
├── project.json
└── project.lock.json

用dotnet命令 Web项目,再用vs code进行debug

mkdir dotnetcoreapp
cd dotnetcoreapp
dotnet new -t Web
dotnet restore
dotnet run

Project dotnetcoreapp  will be compiled because expected outputs are missing
Compiling dotnetcoreapp for 

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

Time elapsed 00:00:03.9491092
 

info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]
      User profile is available. Using '/Users/cjt908/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
Hosting environment: Production
Content root path: /Users/cjt908/Desktop/dotnetcoreapp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
运行图
  1. 用vs code 打开项目
    当我们第一次打开的时候,会出现缺少debug文件,点击yes即可。


    缺少配置文件提示

    此时会会生成一个隐藏的文件夹,里面有两个配置文件。


    配置文件
  2. 修改HomeController里的Index方法,然后断点调试。
    提示分两种,一种是直接启动web调试,另外一种是附加进程调试,跟visual studio里面的类似。


    调试模式
断点调试和监控变量

用Project Rider创建一个netcore项目

  1. 去JetBrains官网填写一些个人信息,然后订阅一下,就会收到一份邮件,里面有相关下载的链接,下载好之后进行安装。
  2. 创建项目


    软件的首页

  3. 2.png
创建好以后的界面
  1. 在Main方法里输出Hello World,并且编译运行。
编译

但是会报错,
Unable to perform Build: Select toolset to perform build

但是我们通过命令去运行的话,是可以正常编译的。

这是因为目前Rider不能基于global.json和project.json识别项目。
我们需要MSBuild或者XBuild来编译项目。
注:因为微软弃用global.json和project.json,所以这个计划应该不会再继续执行。

最简单的解决方案,就是下载mono,安装包里自带里MSBuild,不怕麻烦的话也可以去github下载编译。

执行效果图 项目结构图

用visual studio 2015

这个难度最低,和平时操作无多大区别。

引用

Core

Copyright © 2019- yule263.com 版权所有 湘ICP备2023023988号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务