First, net core project, Getting together! ! !

  Start using the .net core on recent projects, new projects are more familiar things, now take the time to sort out, start again to build a .net core project. Haha, this relatively veterans, it is estimated will feel pediatrics, all right, when it fills a share summary, I hope to help small partners like a little help.

Preparing the environment:

  In order to develop .net core, Microsoft has made it clear to terminate .net core 2.2, so we have to use .net core3.1 in the actual development of it, in order to better use, so the development environment quickly upgrade up: vs gotta upgraded to vs2019 right, .net core3.1 quickly installed. Haha, that is, in fact, think about these two environments, no nonsense, easy to get on line and begin the code it.

Step 1: Create a .net core project:

  In fact, create a .net core project is very simple, so long as .net project, that is a pediatrician, if just join, Baidu is also a major wave of big waves. Here it is also simple to create, for reference:

Part according to the following selection box red, continue to the next:

Fill in the project name, enter the following interface, you can create a .net core projects selected in accordance with Marina red

Haha, it is not that super simple, it is to have this feeling it! ! !

The directory structure brief .net core projects: The second step

  Is not look familiar feeling, ah I have this feeling right, so long as MVC and webform experience the feeling of a small partner is not more evident. .Net directory structure above is the core of the project, in the actual project development, which is to follow this line and Directory Interface own project code Well, today is not that specific line and code. The following outlines the operational mechanism .net it!

The third step: simple comb .net core project configuration file:

  First .net core configuration files are .json file extension, it consists of two main profiles launchSettings.json and appsettings.json

  launchSettings.json: simply means that the project profile, that is, click Project Properties visualization json data file corresponding interface, which is mainly about the configuration environment variables, and so, it is mainly for debugging needs.

  appsettings.json:就是应用内配置,其实简单的说也就是相当于.net项目中的web.config配置文件,这样说是不是就懂啦,哈哈所以不多说了。

第四步:.net core项目启动方式:

,net core项目的启动大概有如下几种方式:

  1、直接通过vs,以IIS为宿主进行启动(操作简单)

  2、直接通过vs,自宿主启动

  3、通过命令行启动

    命令行启动其实也有两种方式:

    直接通过源码启动:

    进入到源码所在目录:执行如下命令即可:

      dotnet run --urls "http://*9000"

    通过编译文件启动:

    进入到编译后的文件所在目录:执行如下命令即可:

      dotnet /项目名称.dll --urls "http://*8000;https://*8001;"

4、当然是生成环境中,可以通过各种容器启动,比如:docker等等

第五步:.net core启动端口设置:

  通过第四步,不同方式把程序跑起来,你会发现,不同的方式其端口也不经相同,那么是哪儿控制了其端口呢?

  其实.net core可以通过如下4种方式来时设置程序端口:

  环境变量:也就是通过launchSettings.json的如下节点来配置程序端口

  硬编码:也就是直接将端口写在代码中,具体怎么操作,下面再说

  应用配置:也就是通过appsettings.json的如下节点来配置程序端口

    "urls": "http://*:9000"

  命令行:这个就是通过命令行启动时的 -urls后队列的地址

  以上4种方式的优先级是至上而下逐级升高

第六步:.net core的运行机制

  首先梳理一下Program.cs文件的代码逻辑及其功能,有了这个概念后,就清楚了

public static void Main(string[] args)
      {
          /// 创建一个主机(配置信息、主机配置信息)->创建、启动
          CreateHostBuilder(args).Build().Run();
      }
 
      /// <summary>
      /// 创建主机
      /// </summary>
      /// <param name="args"></param>
      /// <returns></returns>
      public static IHostBuilder CreateHostBuilder(string[] args) =>
          /// 主机分两类:一类的默认主机(非web主机)、web主机
 
          /// 创建一个默认主机
          /// 加载主机配置,加载环境变量,加载命令行参数
          /// 加载日志组件、启用IIS集成
          Host.CreateDefaultBuilder(args)
              /// 配置web主机
              /// 将kestrel设置为web主机服务器,并进行一些初始化配置
              /// 加载前缀为aspnetcore的环境变量
              .ConfigureWebHostDefaults(webBuilder =>
              {
                  /// 指定web应用的启动类
                  webBuilder.UseStartup<Startup>();
                  /// 修改主机配置项的地址
                  webBuilder.UseUrls("http://*:7000");
 
                  /// 程序的启动端口可以通过如下4种方式来设置:
                  /// 命令行->应用配置->硬编码->环境变量
                  /// 其中每种设置的优先级是:从左至右逐级递减
 
                  /// 其他各种配置
              });
  }

 

下面在用一个来形象的描述其运行机制

总结:

哈哈,上面也是简单的总结了一下.net core的创建,当然对于老手来说,简直就是小菜,哈哈,没事,就当回顾一下。做.net开发,.net core是一个必然的选择,所以还在做基于.net framework 开发的,最好迁移到.net core上来。

.net core 主要有如下一些特点:

跨平台、跨框架、支持命令行、部署灵活、兼容性强、轻量级、开源、微服务支持

说了半天,下面来一张图总结一下.net core 和 .net framework的关系:

 

明天会继续和大家分享.net core中的依赖注入相关的知识,感觉大家关注与分享!!

 

END
欢迎各位小伙伴关注我的公众号(程序员修炼之旅),里面会分享一些技术类干货,同时也是一个技术沟通交流平台,谢谢大家的支持。

Guess you like

Origin www.cnblogs.com/xiaoXuZhi/p/xyh_netcore_first.html