What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?

WBOY
Release: 2023-09-06 16:01:09
forward
778 people have browsed it

IApplicationBuilder.Use() 和 IApplicationBuilder.Run() C# Asp.net Core 之间有什么区别?

We can configure the middleware in the Configure method of the Startup class, using IApplicationBuilder instance.

Run() is an extension method on the IApplicationBuilder instance, which adds a terminal

Add middleware to the application's request pipeline.

The Run method is an extension method of IApplicationBuilder and accepts a

Parameters of RequestDelegate.

Run method signature

public static void Run(this IApplicationBuilder app, RequestDelegate handler)
Copy after login

RequestDelegate signature

public delegate Task RequestDelegate(HttpContext context);
Copy after login

Example

Chinese translation is:

Example

public class Startup{
   public Startup(){
   }
   public void Configure(IApplicationBuilder app, IHostingEnvironment env,
   ILoggerFactory loggerFactory){
      //configure middleware using IApplicationBuilder here..
      app.Run(async (context) =>{
         await context.Response.WriteAsync("Hello World!");
      });
      // other code removed for clarity..
   }
}
Copy after login

The above MyMiddleware function is not asynchronous, so it will block the thread until it completes execution. So by using async and Wait to improve performance and scalability.

public class Startup{
   public Startup(){
   }
   public void Configure(IApplicationBuilder app, IHostingEnvironment env){
      app.Run(MyMiddleware);
   }
   private async Task MyMiddleware(HttpContext context){
      await context.Response.WriteAsync("Hello World! ");
   }
}
Copy after login

Configuring multiple middleware using Run()

The following code will always execute the first Run method and will never reach

The second Run method

public void Configure(IApplicationBuilder app, IHostingEnvironment env){
   app.Run(async (context) =>{
      await context.Response.WriteAsync("1st Middleware");
   });
   // the following will never be executed
   app.Run(async (context) =>{
      await context.Response.WriteAsync(" 2nd Middleware");
   });
}
Copy after login

USE

To configure multiple middleware, use the Use() extension method. It is similar to Run() method, except that it contains the next parameter to call the next middleware Sequence

public void Configure(IApplicationBuilder app, IHostingEnvironment env){
   app.Use(async (context, next) =>{
      await context.Response.WriteAsync("1st Middleware!");
      await next();
   });
   app.Run(async (context) =>{
      await context.Response.WriteAsync("2nd Middleware");
   });
}
Copy after login

The above is the detailed content of What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template