middleware in asp.net core

Post on 13-Jan-2017

263 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Welcome to Middleware in Asp.Net

Core

Shahriar HossainMicrosoft MVP

Sr. Software Engineer

Author, Microsoft Silverlight for Windows PhoneTech blogger

Microsoft Technical Community Speaker

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

ASP.NET Core

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

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

The concept of Middleware is not new !

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

Handling incoming request in WebApi

1.HttpServer2.HttpRoutingDispatcher3.HttpControllerDispatcher

Built-in message handler in WebApi

Writing Custom Handler

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

handler)

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;    }}

Step 3: Registering Handler

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

// Code Excerpt }}

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

MVC + Web API + Web Pages =

ASP.NET Core MVC

ASP.NET Core Pipeline

ResponseRequest Middleware

ASP.NET Core Middleware

• 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?}");});

Custom Middleware

DEMO

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

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

Thank You

top related