Middleware in Asp.Net Core

of 22/22
Welcome to Middleware in Asp.Net Core
  • date post

    13-Jan-2017
  • Category

    Technology

  • view

    241
  • download

    0

Embed Size (px)

Transcript of Middleware in Asp.Net Core

Intro to Azure Webjobs

Welcome to Middleware in Asp.Net Core

Shahriar Hossain

Microsoft MVP

Sr. Software Engineer

Author, Microsoft Silverlight for Windows Phone

Tech blogger

Microsoft Technical Community Speaker

TechEd 2013 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/2016 11:53 AM2

Middleware in Asp.Net Core

AgendaHandling request in WebApiBuilt in message handler in WebApiWriting Custom handler Introducing lightweight ASP.NET Core OverviewGetting startedMiddleware

9/27/20164 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

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

MVCWeb APIWeb PagesRazorHTML HelpersHTML HelpersControllersControllersActionsActionsFiltersFiltersModel bindingModel bindingDIDIASP.NET framework

ASP.NET frameworks - similar, but different 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/20166

.NET future innovation .NET CORENew open source and cross-platform .NET runtime andlibrarystackHigh performance, including native compilationNew set of command-line tools.NET Core CLIfor compilingand publishing appsSupports app local, shared framework, and DockerdeploymentASP.NET Core builds on top of .NET CoreSingle framework for web pages, services, and microservicesIntroduces concept of middleware pipeline, enabling youto inject as little or much functionality as neededFully integrates with CLI tooling and uses the sharedframeworkTakes advantage of .NET Core performance and includes avery high performance web server, built on LibUV

.NET COREASP.NET CORE

The standard library allows us to innovate much more quickly and those innovations are picked up by all the platforms. Tooling innovation affects both app models & standard library7

ASP.NET Core 1.0 Key ValuesChoose your Editors and ToolsOpen Source with ContributionsCross-Platform

OSSSeamless 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

HttpServerHttpRoutingDispatcherHttpControllerDispatcherBuilt-in message handler in WebApi

HttpServergets the request from the host.HttpRoutingDispatcherdispatches the request based on the route.HttpControllerDispatchersends the request to a Web API controller.

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/201612

Writing Custom Handler Three simple steps :Inherit from DelegatingHandlerOverride the send async methodPut it on the pipeline(Register your handler)

varresponse = awaitbase.SendAsync(request, cancellationToken); Debug.WriteLine("Process response handler 1 end");returnresponse;

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/201613

Step 1 & 2 publicclassMessageHandler1: DelegatingHandler{ protectedasyncoverrideTask SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { varresponse =newHttpResponseMessage(HttpStatusCode.OK) { Content =newStringContent(Inside MessageHandler1") }; vartsc =newTaskCompletionSource(); tsc.SetResult(response); returnawait tsc.Task; }}

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/201614

Step 3: Registering Handlerpublic static class WebApiConfig{ public static void Register(HttpConfiguration config) { config.MessageHandlers.Add(new MessageHandler1());

// Code Excerpt }}

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/201615

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

MVC + Web API + Web Pages = ASP.NET Core MVC

ASP.NET Core PipelineResponse

RequestMiddleware

ASP.NET Core Middleware

Static filesRoutingError HandlingMVCBuilt-In Middlewareapp.UseStaticFiles();

app.UseRouter(routes);

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

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

Custom MiddlewareDEMO

Community GroupMicrosoft Azure Bangladesh group: www.facebook.com/groups/microsoft.azure.bd fan page: fb.com/microsoft.azure.bdAsp.Net MVC - Bangladesh www.facebook.com/groups/asp.net.mvc.bd.Netter www.facebook.com/groups/netterNerdCatsSchool www.facebook.com/groups/NerdCatsSchool

Shahriar HossainFacebookFind me athttp://facebook.com/Shahriar.csePersonal Blog

LinkedIn

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

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

2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/201623

Thank You

TechEd 2013 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.9/27/2016 11:53 AM24