开始DotNetCore之旅

 

下载.NetCore SDK

从Microsoft官方网站下载最新的DotNetCore SDK. 目前最新的版本是2.1

https://www.microsoft.com/net/learn/get-started-with-dotnet-tutorial

下载最新的VSCode, 学习.netCore 为什么要选择VSCode. 轻量,简单的风格令人耳目一新,几乎支持目前所有的编程语言,丰富的扩展,只占用极少的计算机资源。

安装c# 相关扩展

除了最基本的C#扩展,我还额外安装了Powershell的扩展(因每个人需求不同自定义)


认识NetCore

首先需要配置dotnet sdk 的环境变量,有的版本需要配置,有的不需要

在cmd中使用命令 dotnet -h 列出.netCore相关命令 

new              Initialize .NET projects. (初始化一个.netcore 的 工程)
  restore          Restore dependencies specified in the .NET project. (还原.net工程之间的以来)
  run              Compiles and immediately executes a .NET project. (编译工程并且执行)
  build            Builds a .NET project. (编译)
  publish          Publishes a .NET project for deployment (including the runtime). (发布)
  test             Runs unit tests using the test runner specified in the project. (执行测试)
  pack             Creates a NuGet package. (创建 NuGet 包)
  migrate          Migrates a project.json based project to a msbuild based project. (从一个.json定义的项目迁移成传统的基于msbuild的项目)
  clean            Clean build output(s). (清理项目)
  sln              Modify solution (SLN) files. (解决方案相关操作)
  add              Add reference to the project. 
  remove           Remove reference from the project.
  list             List reference in the project.
  nuget            Provides additional NuGet commands. 
  msbuild          Runs Microsoft Build Engine (MSBuild). 
  vstest           Runs Microsoft Test Execution Command Line Tool.

 

接下来使用 dotnet new -h 指令, 查看new 指令的创建类型

Templates                                         Short Name       Language          Tags
--------------------------------------------------------------------------------------------------------
Console Application                               console          [C#], F#, VB      Common/Console
Class library                                     classlib         [C#], F#, VB      Common/Library
Unit Test Project                                 mstest           [C#], F#, VB      Test/MSTest
xUnit Test Project                                xunit            [C#], F#, VB      Test/xUnit
ASP.NET Core Empty                                web              [C#], F#          Web/Empty
ASP.NET Core Web App (Model-View-Controller)      mvc              [C#], F#          Web/MVC
ASP.NET Core Web App                              razor            [C#]              Web/MVC/Razor Pages
ASP.NET Core with Angular                         angular          [C#]              Web/MVC/SPA
ASP.NET Core with React.js                        react            [C#]              Web/MVC/SPA
ASP.NET Core with React.js and Redux              reactredux       [C#]              Web/MVC/SPA
ASP.NET Core Web API                              webapi           [C#], F#          Web/WebAPI
global.json file                                  globaljson                         Config
Nuget Config                                      nugetconfig                        Config
Web Config                                        webconfig                          Config
Solution File                                     sln                                Solution
Razor Page                                        page                               Web/ASP.NET
MVC ViewImports                                   viewimports                        Web/ASP.NET
MVC ViewStart                                     viewstart                          Web/ASP.NET

 

然后使用 dotnet new console {appname}指令创建一个控制台应用程序

dotnet new console -o Hello
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on Hello\Hello.csproj...
  Restoring packages for C:\projects\Git\Code\NetCore\Console\Hello\Hello.csproj...
  Generating MSBuild file C:\projects\Git\Code\NetCore\Console\Hello\obj\Hello.csproj.nuget.g.props.
  Generating MSBuild file C:\projects\Git\Code\NetCore\Console\Hello\obj\Hello.csproj.nuget.g.targets.
  Restore completed in 198.52 ms for C:\projects\Git\Code\NetCore\Console\Hello\Hello.csproj.


Restore succeeded.

 

这里用的工程名为Hello, 生成的目录结构如下

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         8/6/2018   7:50 AM                obj
-a----         8/6/2018   7:50 AM            178 Hello.csproj
-a----         8/6/2018   7:50 AM            187 Program.cs

Hello.csproj

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<OutputType>Exe</OutputType>

<TargetFramework>netcoreapp2.0</TargetFramework>

</PropertyGroup>

</Project>

Program.cs

using System;

namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

编译调试

按下F5 在Evironment选择器中选择 .netCore

然后当前的根目录会创建launch.json, task.json  这里使用默认配置就可以

如果有多个项目,可以在下面的属性中设置启动项目

tasks.json -> tasks->args

     "args": [
                "build",
                "${workspaceFolder}/Hello/Hello.csproj"
            ],

launch.json -> configurations ->program,cwd

 {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/Hello/bin/Debug/netcoreapp2.0/Hello.dll",
            "args": [],
            "cwd": "${workspaceFolder}/Hello",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
        }

重新按下F5启动默认配置执行Debug

Executing task: C:\Program Files\dotnet\dotnet.exe build C:\projects\Git\Code\NetCore\Console/CSharpSixTest/CSharpSixTest.csproj <

Microsoft (R) Build Engine version 15.3.409.57025 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Prime -> C:\projects\Git\Code\NetCore\Console\Prime\bin\Debug\netstandard2.0\Prime.dll
  CSharpSixTest -> C:\projects\Git\Code\NetCore\Console\CSharpSixTest\bin\Debug\netcoreapp2.0\CSharpSixTest.dll

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

Time Elapsed 00:00:01.47

Terminal will be reused by tasks, press any key to close it.

请注意上面的输出第一句命令, dotnet build {.csproj}

如果你的VSCode中没有安装c#扩展,使用上面的的命令编译

或者使用命令直接执行  dotnet run {.csproj}

 

添加类库项目

使用下面的命令床一个类库项目

cd {project root}

new-item MyPrinter -itemtype directory  (创建目录)

cd MyPrinter

dotnet new classlib (创建类库项目)

 

创建完成后的目录结构

Directory: C:\projects\Git\Code\NetCore\Console\MyPrinter

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----         8/6/2018   8:23 AM                obj
-a----         8/6/2018   8:23 AM             86 Class1.cs
-a----         8/6/2018   8:23 AM            145 MyPrinter.csproj

 

在VSCode中可以重命名文件,这里将Class1.cs重命名为Printer.cs

using System;

namespace MyPrinter
{
    public class Printer
    {
        public void Print(string printContent){
            Console.WriteLine("Printer print content : " + printContent);
        }
    }
}

那么如何在Hello项目中引用这个classlib

使用下面的命令 

cd {Startup Project Location}

dotnet add reference {target project path}

PS C:\projects\Git\Code\NetCore\Console\Hello> dotnet add reference ..\MyPrinter\MyPrinter.csproj
Reference `..\MyPrinter\MyPrinter.csproj` added to the project.

Program.cs 

using System;
using MyPrinter;
namespace Hello
{
    class Program
    {
        static void Main(string[] args)
        {
            Printer p =new Printer();
            Console.WriteLine("Hello World!");
            p.Print("First .net core app");
        }
    }
}

执行

Hello World!
Printer print content : First .net core app

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值