Introduction
这部分是关于 .NET Core and the Windows Runtime 的内容。官方文档参照 .Net 指南 和 .Net Core 文档 为主。
Chapter 19: Libraries, Assemblies, Packages, and NuGet
- 此章为项目管理,不涉及编程。
- .NET Core CLI overview
Chapter 20: Dependency Injection
WHAT IS DEPENDENCY INJECTION?
这里先介绍下控制反转:
控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度。其中最常见的方式叫做依赖注入(Dependency Injection,简称DI)
依赖注入是控制反转的一种实现方式
具体什么是控制反转呢?
Class A
中用到了Class B
的对象b
,一般情况下,需要在A
的代码中显式的new
一个B
的对象。
采用依赖注入技术之后,A
的代码只需要定义一个私有的B
对象,不需要直接new
来获得这个对象,而是通过相关的容器控制程序来将B对象在外部new
出来并注入到A类里的引用中。而具体获取的方法、对象被获取时的状态由配置文件(如XML
)来指定。
简单地说,就是当 A
对象依赖 B
对象时,传统的做法时在 A
中 new
一个 B
对象,现在改为:在外部先 new
一个B对象,然后通过 class A
的构造函数传递给 A
对象。
- 在 ASP.NET Core 依赖注入
- 服务与依赖注入简介
- 在 .Net Core 中,可以把除了
StartUp
和Program
之外的类都当作服务。
Using a Service Without Dependency Injection
代码来源于《C#高级编程》
Using Dependency Injection
使用接口或基类抽象化依赖关系实现。
USING THE .NET CORE DI CONTAINER
- 注册服务容器中的依赖关系。 ASP.NET Core 提供了一个内置的服务容器
IServiceProvider
。 服务已在应用的Startup.ConfigureServices
方法中注册。- 将服务注入 到使用它的类的构造函数中。 框架负责创建依赖关系的实例,并在不再需要时对其进行处理。
服务容器的示例:
LIFETIME OF SERVICES
Using Singleton and Transient Services
Registering a service as a singleton always returns the same instance, and registering a service as transient returns a new object every time the service is injected.
Using Scoped Services
Services can also be registered within a scope. This is something inbetween transient and singleton. With singleton, only a single instance is created. Transient creates a new instance every time the service is requested from the container. With scoped, always the same instance is returned from the same scope, but from a different scope a different instance is returned.
INITIALIZATION OF SERVICES USING OPTIONS and USING CONFIGURATION FILES
- ASP.NET Core 中的选项模式
- (13)ASP.NET Core2.2 中的选项模式(Options)
- DI with options example
- DI with configuration files example
CREATING PLATFORM INDEPENDENCE
Dependency injection also can be used to use platform-specific features from platform-independent libraries.
这部分内容是不跨平台的,以后有需要再补。
USING OTHER DI CONTAINERS
Microsoft.Extensions.DependencyInjection
is a simple DI container; many third-party containers offer additional functionality. For example, Autofac allows configuration of the services in a configuration file.
原生的 DI 容器非常轻量,如果需要更复杂的容器,推荐使用 Autofac。
Chapter 21: Tasks and Parallel Programming
《C#高级编程》中并行和异步内容过于详细庞杂,在学习时推荐上手练习 LeetCode 的 Concurrency 和 Codewar 的多线程题目。
OVERVIEW
- .NET 中的并行处理、并发和异步编程
- C# 使用
Task
进行异步编程,Parallel
类进行并行编程。如果想要更多的控制权限,使用Thread
。 - 需要同步机制
synchronization
去防止竞争危害 (race condition) 和死锁 (deadlock)。 - 除了
synchronization
外,使用不可变类型 (immutable types) 也是一种方案。 - 同步基元概述
PARALLEL CLASS
TASKS
Parallel.For用起来方便,但是在实际开发中还是尽量少用,因为它的不可控性太高,有点简单粗暴的感觉,可能带来一些不必要的"麻烦",最好还是使用Task,因为Task的可控性较好。
--- 5天玩转 C# 并行和多线程编程
Chapter 22: Files and Streams
MANAGING THE FILE SYSTEM
Directories or folders? These terms are often used interchangeably. Directory is a classical term for a file system object. A directory contains files and other directories. A folder has its origin with Apple's Lisa and is a GUI object. Often it is associated with an icon to map to a directory.
Using File to Read and Write
- 官方文档使用的是
StreamReader
和StreamWriter
进行读写操作。 - 对于小文件,
File.ReadAllText
和File.WriteAllText
是更便捷的方式,详情参考 File 类
ENUMERATING FILES
WORKING WITH STREAMS
A stream is an object used to transfer data.
USING READERS AND WRITERS
COMPRESSING FILES
Both
DeflateStream
andGZipStream
use the same algorithm for compression (in fact,GZipStream
usesDeflateStream
behind the scenes), butGZipStream
adds a cyclic redundancy check to detect data corruption.Brotli
is a relatively new open-source compression algorithm from Google. The speed ofBrotli
is similar to deflate, but it offers a better compression. Contrary to most other compression algorithms, it uses a dictionary for often-used words for better compression. Nowadays this algorithm is supported by most modern browsers.
Using Brotli
Add the NuGet package
System.IO.Compression.Brotli
and instantiate theBrotliStream
class
Decompression works accordingly using
BrotliStream
:
WATCHING FILE CHANGES
With
FileSystemWatcher
, you can monitor file changes. Events are fired on creating, renaming, deleting, and changing files. This can be used in scenarios where you need to react on file changes — for example, with a server when a file is uploaded, or in a case where a file is cached in memory and the cache needs to be invalidated when the file changes.
WORKING WITH MEMORY MAPPED FILES
COMMUNICATING WITH PIPES
管道本质上就是一个文件,前面的进程以写方式打开文件,后面的进程以读方式打开。这样前面写完后面读,于是就实现了通信。实际上管道的设计也是遵循 UNIX 的“一切皆文件”设计原则的,它本质上就是一个文件。Linux 系统直接把管道实现成了一种文件系统,借助 VFS 给应用程序提供操作接口。
虽然实现形态上是文件,但是管道本身并不占用磁盘或者其他外部存储的空间。在Linux的实现上,它占用的是内存空间。所以,Linux上的管道就是一个操作方式为文件的内存缓冲区。
--- Linux 的进程间通信:管道
USING FILES AND STREAMS WITH THE WINDOWS RUNTIME
Chapter 23: Networking
The two namespaces of most interest for networking are
System.Net
andSystem.Net.Sockets
. TheSystem.Net
namespace is generally concerned with higher-level operations, such as downloading and uploading files, and making web requests using HTTP and other protocols, whereasSystem.Net.Sockets
contains classes to perform lower-level operations. You will find these classes useful when you want to work directly with sockets or protocols, such as TCP/IP.
The HTTP protocol is based on TCP, and thus the HttpXX classes offered an abstraction layer over the TcpXX classes. The TcpXX classes, however, give you more control. You can even get more control than offered by the TcpXX or UdpXX classes with sockets. With sockets, you can use different protocols, not only protocols based on TCP or UDP, and also create your own protocol. What might be even more important is that you can have more control over TCP- or UDP-based protocols.
THE HTTPCLIENT CLASS
这部分是客户端内容
The
HttpClient
class is used to send an HTTP request and receive the response from the request. It is in theSystem.Net.Http
namespace. The classes in theSystem.Net.Http
namespace help make it easy to consume web services for both clients and server.
传统 HttpClient
的基本用法
新版本的 .Net Core 推荐使用 IHttpClientFactory
来创建 HttpClient
。
WORKING WITH THE WEBLISTENER CLASS
我们的 web 程序需要一个宿主,所以 CreateHostBuilder
这个方法就创建了一个 IHostBuilder
. 而且我们还需要 Web Server.
Asp.Net Core 自带了两种 http servers, 一个是 WebListener
, 它只能用于 Windows 系统, 另一个是 kestrel
, 它是跨平台的.
WebListener
是一个只能运行在 Windows 上的 ASP.NET Core web 服务器,基于 Http.Sys
内核模块驱动构建。
Chapter 24: Security
VERIFYING USER INFORMATION
Two fundamental pillars of security are authentication and authorization. Authentication is the process of identifying the user, and authorization occurs afterward to verify that the identified user is allowed to access a specific resource.
这部分内容还是用 IdentityServer 更好。
剩下的内容单靠书本上的理论知识无法充分理解,需要实战演练。