Download - Middleware in Asp.Net Core

Transcript
Page 1: Middleware in Asp.Net Core

Welcome to Middleware in Asp.Net

Core

Page 2: Middleware in Asp.Net Core

Shahriar HossainMicrosoft MVP

Sr. Software Engineer

Author, Microsoft Silverlight for Windows PhoneTech blogger

Microsoft Technical Community Speaker

Page 3: Middleware in Asp.Net Core

Middleware in Asp.Net Core

Page 4: Middleware in Asp.Net Core

Agenda Handling request in WebApi• Built in message handler in WebApi• Writing Custom handler

Introducing lightweight ASP.NET Core • Overview• Getting started• Middleware

Page 5: Middleware in Asp.Net Core

ASP.NET Core

A new open-source and cross-platform framework for building modern cloud-based Web applications using .NET

Page 6: Middleware in Asp.Net Core

ASP.NET Core 1.0 – Key Values

Choose your Editors and Tools

Open Source with Contributions Cross-PlatformOSS

Seamless transition from on-premises to cloud

Faster Development CycleTotally Modular

Fast

Page 7: Middleware in Asp.Net Core

The concept of Middleware is not new !

Page 8: Middleware in Asp.Net Core

How will you -• Modify request headers ?• Add a response header to responses ?• Validate requests before they reach the controller ?• Log the incoming requests and the outgoing responses ?

Handling incoming request in WebApi

Page 9: Middleware in Asp.Net Core

Handling incoming request in WebApi

Page 10: Middleware in Asp.Net Core

1.HttpServer2.HttpRoutingDispatcher3.HttpControllerDispatcher

Built-in message handler in WebApi

Page 11: Middleware in Asp.Net Core

Writing Custom Handler

Three simple steps :1.Inherit from DelegatingHandler2.Override the send async method3.Put it on the pipeline(Register your

handler)

Page 12: Middleware in Asp.Net Core

Step 1 & 2 public class MessageHandler1 : DelegatingHandler{     protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)    { var response = new HttpResponseMessage(HttpStatusCode.OK)    {     Content = new StringContent(“Inside MessageHandler1")    };         var tsc = new TaskCompletionSource<HttpResponseMessage>();         tsc.SetResult(response);         return await tsc.Task;    }}

Page 13: Middleware in Asp.Net Core

Step 3: Registering Handler

public static class WebApiConfig{ public static void Register(HttpConfiguration config) { config.MessageHandlers.Add(new MessageHandler1());

// Code Excerpt }}

Page 14: Middleware in Asp.Net Core

Calls base.SendAsync() to pass the request to the inner message handler.

Page 15: Middleware in Asp.Net Core

MVC + Web API + Web Pages =

ASP.NET Core MVC

Page 16: Middleware in Asp.Net Core

ASP.NET Core Pipeline

ResponseRequest Middleware

Page 17: Middleware in Asp.Net Core

ASP.NET Core Middleware

Page 18: Middleware in Asp.Net Core

• Static files• Routing• Error Handling• MVC

Built-In Middlewareapp.UseStaticFiles();

app.UseRouter(routes);

app.UseExceptionHandler("/Home/Error");

app.UseMvc(routes =>{ routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}");});

Page 19: Middleware in Asp.Net Core

Custom Middleware

DEMO

Page 20: Middleware in Asp.Net Core

Community Group• Microsoft Azure Bangladesh• group: www.facebook.com/groups/microsoft.azure.bd• fan page: fb.com/microsoft.azure.bd• Asp.Net MVC - Bangladesh• www.facebook.com/groups/asp.net.mvc.bd• .Netter• www.facebook.com/groups/netter

• NerdCatsSchool • www.facebook.com/groups/NerdCatsSchool

Page 21: Middleware in Asp.Net Core

Shahriar Hossain

Facebook

Find me athttp://facebook.com/Shahriar.cse

Personal Blog

LinkedIn

Find me athttp://bd.linkedin.com/in/shahriarhossain

Find amazing .Net stuffs at http://LearnWithShahriar.wordpress.com

Page 22: Middleware in Asp.Net Core

Thank You