DotNet Core pipe communication

Original: the DotNet Core pipe communication

Understand it

Before, we need a clear concept is, Web application, each user's request processes are linear , on the ASP.NET Core program, will correspond to a request pipeline (request pipeline), at the request pipeline , we can dynamically configure business logic corresponding to the various middleware (middleware), so as to achieve a different server may be made in response to requests for different users.

In ASP.NET Core, the pipe programming is the concept of a core and foundation of many of its middleware are by pipeline to the final configuration request pipeline way, so understand there's a pipeline for us to write programming more robust DotNetCore program is very important.

Elaborate line

As it can be seen from the figure that the request is a generally linear process the Kestrel < -Service > After the reception, a series of processes performed by the middleware, in response to -Service , and then presented to the user.

Here is my request conjunction DotNet Core UML diagrams given source (not entirely to).


DotNet Core pipe communication

Analytical illustrated communication pipe

You can see the four most important part is WebHostBuilder, WebHost, Server, HttpApplicaiton. DotNet Core deployed from the beginning of the project to build the host WebHostBuilder.Build().Run();started to build our mainframe host server Kestrel, and start the listener and provide FeatrueCollection to build HttpApplication HttpContext.

默认的会构建DefaultHttpContext,包含Context的Scope,StartTimeStamp等属性,并实现IFeatrueCollection方法,从而提供Service上下文属性,从而运行服务器。如代码所示

  public class HttpListenerServer : IServer
  {
      public HttpListener Listener { get; }
      public HttpListenerServer(string url)
      {
          this.Listener = new HttpListener();
          this.Listener.Prefixes.Add(url ?? "http://localhost:3721/");
      }
      public void Start<TContext>(IHttpApplication<TContext> application)
      {
          this.Listener.Start();
          while (true)
          {
              HttpListenerContext httpListenerContext = this.Listener.GetContext();
   
              HttpListenerContextFeature feature = new HttpListenerContextFeature(httpListenerContext);
              FeatureCollection contextFeatures = new FeatureCollection();
              contextFeatures.Set<IHttpRequestFeature>(feature);
              contextFeatures.Set<IHttpResponseFeature>(feature);
              TContext context = application.CreateContext(contextFeatures);
   
              application.ProcessRequestAsync(context)
                  .ContinueWith(_ => httpListenerContext.Response.Close())
                  .ContinueWith(_ => application.DisposeContext(context, _.Exception));
          }
      }
  }

服务器在接收到上下文后,调用Start方法,并启动StartUpLoader,从而将实现了IApplicationBuilder的中间件,进行委托链式使用。

总之

虽然我们对这个模拟管道做了极大的简化,但是它依然体现了ASP.NET Core管道处理请求的真实流程,而且真实管道的创建方式也与模拟管道基本一致。如果读者朋友们能够对这个模拟管道具有深刻的理解,我相信对真实管道的把握就会变得非常容易。

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11915392.html