find and solve || findandsolve.com
Please wait.....

What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run()


I can configure middleware in the Configure method of the Startup.cs class using IApplicationBuilder instance.Run() is an extension method on IApplicationBuilder instance which adds a terminal middleware to the application's request pipeline.The Run method is an extension method on IApplicationBuilder and accepts a parameter of RequestDelegate.

IApplicationBuilder.Use() method : Used to create a simple middleware which can be "chained" to other functions over the pipeline. Takes two arguments: RequestDelegate and HttpContext. It is chained by calling the next().Some features of IApplicationBuilder.Use() like as given below.

  • It is used under configure method of Startup class.
  • It is used to add middleware delegate to the application request pipeline.
  • It will call the next middleware in the pipeline.

IApplicationBuilder.Run() method : "short-circuit" the request pipeline for a given condition. Takes a single argument of HttpContext and can be used to write on the Response.Some features of IApplicationBuilder.Run() like as given below.

  • It is used under configure method of Startup class.
  • It is used to add middleware delegate to the application request pipeline.
  • It will not call the next middleware in the pipeline. System will stop adding middleware after this method.

 Run method

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

RequestDelegate

public delegate Task RequestDelegate(HttpContext context);
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 Example");
      });     
   }
}

In the given above example  asynchronous and so will block the thread till the time it completes the execution. So, make it asynchronous by using async and await to improve performance and scalability

public class Startup{
   public Startup(){
   }
   public void Configure(IApplicationBuilder app, IHostingEnvironment env){
      app.Run(DemoMiddleware);
   }    private async Task DemoMiddleware(HttpContext context){
      await context.Response.WriteAsync("DemoMiddleware Exmple");
   }
}

How to Configure Multiple Middleware using Run()?

In the given blow 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("First Middleware Example");
   });
   // the following will never be executed
   app.Run(async (context) =>{
      await context.Response.WriteAsync("Second Middleware Example");
   });
}

Use method

To configure multiple middleware, use Use() extension method. It is similar to Run() method except that it includes next parameter to invoke next middleware in the sequence

public void Configure(IApplicationBuilder app, IHostingEnvironment env){
   app.Use(async (context, next) =>{
      await context.Response.WriteAsync("First Middleware!");
      await next();
   });
   app.Run(async (context) =>{
      await context.Response.WriteAsync("Second Middleware");
   });
}


Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials

Comments



Report Response