4: Registration and pipeline services

Video address: https: //www.bilibili.com/video/av38392956/ p = 3?

1: When it comes to the last, we get information Hello Word from the configuration file / configuration source, and now we have another way to write it, write a service called IWelcomeService, and assuming there is such a method getMessage, regardless of which object or service, implements this interface, we can achieve the object or service from the interface where to obtain this information

   1.1: Create an interface

  1.2: Create a class that implements this interface to

  1.3: After configuration, we F5 to run, you will find there is a mistake, he said: Can not find a service that implements IWelcomeService interface, so we need to register a service in ASP.NET Core project, corresponding to the IWelcomeService

  1.4: These two interfaces have been registered over the corresponding service is ASP.NET Core has been registered over the default configuration, but IWelcomeService our own defined interface, no registration, so we need an interface to register for this service

  1.5: This method is used to ConfigureServices registration services, such as the following write

    1.5.1: IWelcomeService the interface, WelcomeService his corresponding implementation classes;

    1.5.2: AddSingleton (Singleton) said in a statement cycle the entire web project where a WelcomeService most instances will appear;

    1.5.3: there are two other ways: AddTransient every time there is another way to class or when requested IWelcomeService, which will establish a new WelcomeService instance;

 

    1.5.4: AddScoped is every Http request, when Web requests, and once he generates a Web request WelcomeService instance, during the Web request, if multiple requests, it uses the same instance

  1.6: We now use AddSingleton, this time to run, have found no wrong


 

2: and the intermediate pipe

  2.1:ASP.NET Core Web应用是如何处理Http请求的?

  2.2:假设有个http请求到达我们的应用之后,如下图有个POST请求,POST/后面是一个Product,创建一个产品,而我们的ASP.NET Core Web应用它就需要处理这个请求,在这个应用里,中间件就是干这个活的;

  2.3:中间件就决定着如何处理这个请求,并且其实这个中间件就是一个对象,每个中间件的角色和功能都不一样,而且每个中间件的功能都局限在特定的领域内,所以每个中间件都很小,只关注这一块功能,所以整个web应用将会使用很多中间件;

  2.4:分析一下下面的图:有个POST请求,首先这个Web应用可能要记录所有的Web请求,把里面的信息记录下来,或者是存到日志文件,日志数据库里,所以我们的应用可以考虑添加一个日志中间件Logger,Logger可以查看所有请求进来的信息,包括请求的路径,查询字符串,Header,Cookies,Access Token,Logger最重要的应该是记录信息,把请求的相关信息都记录下来,然年转给下一个中间件...(Logger可以拒绝这个请求,一旦拒绝请求,这个处理也就停止了)

  2.5:下一个中间件,我们可以考虑一个授权功能的中间件,这个中间件的工作原理-->首先会找一个固定的cookie的值或者是Access Token,如果授权中间件找到了,那么它就允许上面这个请求(POST/Product)在整个管道内继续前行,能到下一个中间件,如果没找到特定的cookie或Token,那么它可以返回一个错误代码401,或者把请求重定向登陆页面,这都是常见做法。假设在我们这里找到了,那么它就把请求带到了下一个中间件里...

  2.6::下一个中间件是一个路由中间件,这个中间件首先会看一下请求的URL(POST/Product),然后它的工作就是弄明白你想要调用哪个类的哪个方法,而这个方法可能返回JSON数据,也可能返回Html页面,而这个路由中间件的职责就是找遍整个应用,来找到那个可以响应这个请求的那个东西,如果路由中间件没有找到任何可以相应请求的东西,那么它就返回Http 404状态码;如果找到了,并且这个方法是返回JSON数据或者Html页面,这时整个管道的流程就开始原路返回,所以说管道是双向设计的;

  2.7:一个请求进入管道之后呢,它会按照程序里添加中间件的顺序依次先后经过这些中间件,如下图就是先Logger,再授权,最后路由;假设路由中间件产生了一个响应,那么响应在管道里就是原路返回,从路由这到授权,最后到Logger,响应就会离开我们的服务器,来到客户端,这些就是ASP.NET Core管道和中间件的本质,我们需要按照特定的顺序,设置不同功能的中间件,来组成我们Web应用的功能...(其他常用的中间件也有很多,包括处理异常的中间件,server静态文件的中间件,还有最重要的Mvc,把请求送到Mvc框架里,就需要使用Mvc中间件)

 

Guess you like

Origin www.cnblogs.com/Codemandyk/p/10907079.html
Recommended