.net学习笔记

167 阅读2分钟

### .net

    .NET是开发平台, .NET是 .NET Framwork、NET Core、Xamarin/Mono的统称。

    .NET Framwork 2002年

    .net Core 包含.net framwork ,免费、开源、跨平台

 

## .NET 的重要问题

    1. C#是主要开发语言(C#图解教程)

    2. .net Core 并不是.net framwork 的升级版,无法直接升级

    3. .net framwork 的缺点:

        <1> 系统级别的安装,互相影响

        <2> 无法独立部署

        <3> ASP.NET 和 IIS深度耦合

        <4> ASP.NET资源消耗大

        <5> 非云原生

        .net framwork历史包袱:

        <1>带着手铐脚链长大的ASP.NET MVC

        <2>ASP.NET底层不支持很好的的单元测试、依赖注入等

    4. .net Core 的优点

        1)独立安装部署,不互相影响

        2)彻底模块化

        3)没有历史包袱,运行效率高

        4}不依赖IIS

        5)跨平台

        6)符合现代开发理念:依赖注入、单元测试等

image.png

image.png

.NET Standard是一个标准(规范)

image.png

image.png

反编译器:

程序集->>源代码 ILSpy

image.png

如果想让一个类库同时被.net framwork 和 .net Core引用 可以使用.net standard类库

image.png

NET 开发环境

image.png

.NET CLI 命令****

image.png

版本: .net 程序结构

.csproj **文件 **包含exe 版本。。。

.net framwork 的 csproj 内容比较多 会显示所有该项目中的文件

.net core 的csproj 内容简单 会默认添加项目中引用的文件

image.png

程序的发布:

image.png

虚拟机:

  SWL :linux

  SandBox: widows

image.png

NuGet

image.png

image.png

image.png

image.png

异步编程:(不等)

为什么要用异步编程?

image.png

image.png

Async await 简化多线程

image.png

image.png

image.png

image.png

案例1: image.png

案例2: 不推荐使用 image.png

异步委托:

案例3: image.png

Async 、await 原理揭秘:

image.png

结论:

image.png 异步编程 async 背后的线程切换:

image.png Methods:

image.png 线程切换

image.png

image.png 线程切换并不好!!!

异步方法不等于多线程

image.png 操作:

image.png

image.png 结论:

image.png 为什么有的地方没有标记async?

image.png 案例:

image.png async缺点: image.png 优点:写起来简单

image.png 改进案例: image.png 不要使用Sleep:它会阻断当前的线程

image.png

image.png

image.png

异步编程之CancellationToken

image.png

image.png

案例: image.png

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace 取消请求
{
    class Program
    {
        static async Task Main(string[] args)
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            // 超时处理
            // cts.CancelAfter(5000);

            CancellationToken cToken = cts.Token;
            await Download1Async("https://www.youzack.com", 100, cToken);
        }
        static async Task DownloadAsync(string url, int n)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                for(int i = 0; i < n; i++)
                {
                    string html = await httpClient.GetStringAsync(url);
                    Console.WriteLine($"{DateTime.Now}:{html}");
                }
            }
        }
        static async Task Download1Async(string url, int n, CancellationToken cancellationToken)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                for (int i = 0; i < n; i++)
                {
                    string html = await httpClient.GetStringAsync(url);
                    Console.WriteLine($"{DateTime.Now}:{html}");
                    // 2. 取消请求(建议 增加可读性)
                    if (cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("请求被取消");
                        break;
                    }
                    // 1. 直接抛出异常
                    // cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }

        static async Task Download2Async(string url, int n, CancellationToken cancellationToken)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                for (int i = 0; i < n; i++)
                {
                    // GetAsync
                    var resp = await httpClient.GetAsync(url, cancellationToken);
                    string html = await resp.Content.ReadAsStringAsync();
                    Console.WriteLine($"{DateTime.Now}:{html}");
                    // 2. 取消请求(建议 增加可读性)
                    if (cancellationToken.IsCancellationRequested)
                    {
                        Console.WriteLine("请求被取消");
                        break;
                    }
                    // 1. 直接抛出异常
                    // cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }
    }
}

HttpClient 类

命名空间:

System.Net.Http

程序集: System.Net.Http.dll

提供一个类,用于发送 HTTP 请求并从 URI 标识的资源接收 HTTP 响应。

** HttpClient.GetStringAsync** 方法

将 GET 请求发送到指定 URI 并在异步操作中以字符串的形式返回响应正文。

** HttpClient.GetAsync** 方法

以异步操作将 GET 请求发送给指定 URI。

Task类

image.png

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace WhenAll
{
    class Program
    {
        static async Task Main(string[] args)
        {
            /* Task<string> t1 = File.ReadAllTextAsync(@"D:\.Net_Study\a1\1.txt");
            Task<string> t2 = File.ReadAllTextAsync(@"D:\.Net_Study\a1\2.txt");
            Task<string> t3 = File.ReadAllTextAsync(@"D:\.Net_Study\a1\3.txt");

            string[] strs = await Task.WhenAll(t1, t2, t3);

            string s1 = strs[0];
            string s2 = strs[1];
            string s3 = strs[2];

            Console.WriteLine(s1);
            Console.WriteLine(s2);
            Console.WriteLine(s3);*/

            // 拿到文件夹下所有的文件
            string[] files = Directory.GetFiles(@"D:\.Net_Study\a1");
            Task<int>[] countTasks = new Task<int>[files.Length];
            for(int i = 0; i < files.Length; i++)
            {
                string filename = files[i];
                Task<int> t = ReadCharsCount(filename);
                countTasks[i] = t;
            }
            int[] counts = await Task.WhenAll(countTasks);
            int c = counts.Sum();//计算数组的和
            Console.WriteLine(c);
        }

        // 读取char 的个数
        static async Task<int> ReadCharsCount(string filename)
        {
            string s = await File.ReadAllTextAsync(filename);
            return s.Length;
        }
    }
}