初识.Net Core(1)

重要信息: 本文为初次学习.Net Core时所作,其中有很多描述或有欠缺,后续会进行持续改进。

                   本文中有一个问题未能解决: 使用C#7.0新语法,在命名空间前Using  StaticClass 然后在方法中直接调用静态方法(本文中出现错误)

.NET Core 出现的原因

1.去重(更加轻量级)

众所周知.Net Framework一直是向前兼容(即兼容更早的版本),例如NetFramework5.0会兼容.Net Framework4.0.......Net Core的出现解决了.Net FrameWork繁重的问题,把一些不必要的封装进行消减,.Net Core是一套全新的框架

2.跨平台性

 .Net Core不仅能够在Windows下运行,也能在linux下运行。

我们知道在.Net能够运行是因为CLR,而在.Net FrameWork中就包含CLR。微软公司为.NET Core也开发了一套能够在Linux系统运行的CLR

.NET Core环境

开发工具: Visual Studio 2017 

安装程序:.NET Core SDK  = 使用.NET Core开发应用.NET Core Runtime SDK+CLI(Software Development Kit/Command Line Interface) 工具

安装程序:dotnet-hosting-2.2.0-preview3-35497-win IIS转发请求,可以理解为.Net Core Runtime坏境。(如果没安装,在部署至IIS时无法访问站点)

.NET Core.NET Framework

基于.NET Core.NET Framework开发的区别(Web项目为例)

1:对于IIS的诉求不同

   asp.net--网站托管在IIS--IIS负责监听-转发请求--响应客户端

  .net core--控制台--CreateWebHostBuilder(内置了服务器)--启动了服务器--负责监听-转发请求--响应客户端(IIS只能做反向代理,不再做监听)

KestrelServer 跨平台的服务器

2.内部需要做的事情不同

  Asp.Net不负责请求的监听-转发-响应(IIS),封装了管道处理模型,只写业务处理逻辑

  Asp.NetCore  是个控制台,请求监听-转发-响应都是自己完成的,包括管道模型也是自定义,

这里不再像以前那样,什么都封装好了,开发者什么也不知道封装的少,东西就少,这也就是

我们为什么会说.NET Core相对而言是轻量级的

C#6.0新语法

        #region 自动属性初始化(Auto-property initializers)
        public string Name { get; set; } = "summit";
        public int Age { get; set; } = 22;
        public DateTime BirthDay { get; set; } = DateTime.Now.AddYears(-20);
        public IList<int> AgeList
        {
            get;
            set;
        } = new List<int> { 10, 20, 30, 40, 50 };
        #endregion

        public void Show(People people)
        {
          
            #region 字符串嵌入值(String interpolation)
            //C#6.0
            System.Console.WriteLine($"年龄:{this.Age}  生日:{this.BirthDay}");
            System.Console.WriteLine($"{(this.Age<20 ?"小鲜肉":"老腊肉")}");//开始支持嵌套表达式等
            #endregion


            #region 导入静态类(Using Static)
            //6.0之前我们调用静态类中的方法时:Class.Method();
            //6.0调用静态类中的方法:Using Class,使用时Method()
            #endregion


            #region 空值运算符(Null-conditional operators)
            int? iValue = 10;
            System.Console.WriteLine(iValue?.ToString());//不需要判断是否为空
            string name = null;
            System.Console.WriteLine(name?.ToString());
            #endregion

            #region 对象初始化器(Index Initializers)
            IDictionary<int, string> dictOld = new Dictionary<int, string>()
            {
                { 1,"first"},
                { 2,"second"}
            };

            IDictionary<int, string> dictNew = new Dictionary<int, string>()
            {
                [4] = "first",
                [5] = "second"
            };

            #endregion

            #region 异常过滤器(Exception filters)
            int exceptionValue = 10;
            try
            {
                Int32.Parse("s");
            }
            catch (Exception e) when (exceptionValue > 1)//满足条件才进入catch
            {
                System.Console.WriteLine("catch");
                //return;
            }
            #endregion

            #region nameof表达式 (nameof expressions)
            System.Console.WriteLine(nameof(people)); //获取peopleTest这个字符串
            #endregion

            #region 在cath和finally语句块里使用await(Await in catch and finally blocks)

            #endregion

        }

        public class People
        {
            public int Id { get; set; }
            public string Name { get; set; }

            public static async Task Get()
            {
                await Task.Run(() =>
                {

                    Thread.Sleep(5000);
                   System.Console.WriteLine("People.async.Get");
                });
                System.Console.WriteLine("People.async.Get after");
            }
        }

C#7.0新语法

        public void Show()
        {
            #region out参数
            {
                this.DoNoting(out int x, out int y);

                this.DoNoting(out var l, out var m);

            }
            //Console.WriteLine(x + y);
            #endregion

            #region 模式
            this.PrintStars(null);
            this.PrintStars(3);

            this.Switch(null);
            this.Switch("RichardRichard");
            this.Switch("Richard");
            #endregion

            #region 元组
            {
                var result = this.LookupName(1);
                System.Console.WriteLine(result.Item1);
                System.Console.WriteLine(result.Item2);
                System.Console.WriteLine(result.Item3);

            }
            {
                var result = this.LookupNameByName(1);
                System.Console.WriteLine(result.first);
                System.Console.WriteLine(result.middle);
                System.Console.WriteLine(result.last);

                System.Console.WriteLine(result.Item1);
                System.Console.WriteLine(result.Item2);
                System.Console.WriteLine(result.Item3);
            }
            #endregion

            #region 局部函数
            {
                Add(3);
                int Add(int k)//闭合范围内的参数和局部变量在局部函数的内部是可用的,就如同它们在 lambda 表达式中一样。
                {
                    return 3 + k;
                }
            }
            #endregion

            #region 数字分隔号
            long big = 100_000;
            #endregion

        }

        /// <summary>
        /// System.ValueTuple 需要安装这个nuget包
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private (string, string, string) LookupName(long id) // tuple return type
        {
            return ("first", "middle", "last");
        }

        private (string first, string middle, string last) LookupNameByName(long id) // tuple return type
        {
            return ("first", "middle", "last");
            //return (first: "first", middle: "middle", last: "last");
        }

        /// <summary>
        /// 具有模式的 IS 表达式
        /// </summary>
        /// <param name="o"></param>
        public void PrintStars(object o)
        {
            if (o is null) return;     // 常量模式 "null"
            if (!(o is int i)) return; // 类型模式 定义了一个变量 "int i"
            System.Console.WriteLine(new string('*', i));
        }

        /// <summary>
        /// 可以设定任何类型的 Switch 语句(不只是原始类型)
        /// 模式可以用在 case 语句中
        /// Case 语句可以有特殊的条件
        /// </summary>
        /// <param name="text"></param>
        private void Switch(string text)
        {
            int k = 100;
            switch (text)
            {
                case "RichardRichard" when k > 10:
                   System.Console.WriteLine("RichardRichard");
                    break;
                case "Richard" when text.Length < 10:
                    System.Console.WriteLine("Richard");
                    break;
                case string s when s.Length > 7://模式
                    System.Console.WriteLine(s);
                    break;
                default:
                    System.Console.WriteLine("default");
                    break;
                case null:
                    System.Console.WriteLine("null");
                    break;
            }
        }

        private void DoNoting(out int x, out int y)
        {
            x = 1;
            y = 2;
        }

Core部署至IIS

基于CoreWeb项目部署至IIS具体步骤

注意:一定要记得安装dotnet-hosting-2.2.6-win,它的作用是Core的运行时环境,如果不安装,部署时无法运行

 

 

 

应用程序池的选择和基于Framework有所差异,在基于Framework中这里是选择.Net CLR的版本,而基于Core则选择的是无托管代码。原因:

Core是跨平台的,就两者本身而言,它们的运行时环境就不一样。Framework需要IIS所做的事情,Core都不需了(自己做)Core的代码不再

托管在IIS上,对于Core而言IIS只是一个代理,只是做一个转发而已。

猜你喜欢

转载自www.cnblogs.com/JohnTang/p/11356856.html
今日推荐