<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Huzaifa Asif on Medium]]></title>
        <description><![CDATA[Stories by Huzaifa Asif on Medium]]></description>
        <link>https://medium.com/@huzaifa.asif?source=rss-309cf6a27d59------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Huzaifa Asif on Medium</title>
            <link>https://medium.com/@huzaifa.asif?source=rss-309cf6a27d59------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:55:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@huzaifa.asif/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Create a custom middleware pipeline using .NET Core]]></title>
            <link>https://medium.com/@huzaifa.asif/create-a-custom-middleware-pipeline-using-net-core-c60fde242ad9?source=rss-309cf6a27d59------2</link>
            <guid isPermaLink="false">https://medium.com/p/c60fde242ad9</guid>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[middleware]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <dc:creator><![CDATA[Huzaifa Asif]]></dc:creator>
            <pubDate>Tue, 18 Dec 2018 22:35:05 GMT</pubDate>
            <atom:updated>2018-12-18T22:43:44.896Z</atom:updated>
            <content:encoded><![CDATA[<h3>Create a custom middleware pipeline using .NET Core</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FEV4Vo6fekbxMqc0" /><figcaption>Photo by <a href="https://unsplash.com/@grohsfabian?utm_source=medium&amp;utm_medium=referral">Fabian Grohs</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>What is middleware?</h4><p>Middleware are small C# components that are assembled into an application pipeline to handle requests and responses.</p><p>Each component can choose whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component in the pipeline. Request delegates are used to build this request pipeline, which are then used to handle each incoming HTTP request to your application.</p><h4>Configuring the HTTP pipeline</h4><p>Middleware components are setup in the <strong>Configure </strong>method of the <strong>Startup</strong> class using one or more of the following extension methods on the IApplicationBuilder:</p><ul><li><strong>Use</strong></li><li><strong>Run</strong></li><li><strong>Map</strong></li></ul><pre>public void Configure(IApplicationBuilder app)<br>{<br> app.UseExceptionHandler(“/Home/Error”);<br> app.UseHsts();<br> app.UseHttpsRedirection();<br> app.UseStaticFiles();<br>}</pre><p>Ordering of these middleware components is important as it will be the order in which they are invoked on incoming requests and for outgoing responses it will be called in the reverse order.</p><h4>Create custom middleware</h4><p>Time to create our own middleware and plug it into the application pipeline.</p><blockquote><em>Create the </em><em>CustomLoggerMiddlewareclass that would first log a message when the application is handling incoming request and log another message when returning a response back.</em></blockquote><pre>public class CustomLoggerMiddleware<br>{<br>    private readonly RequestDelegate _next;<br>    private readonly ILogger _logger;</pre><pre>public CustomLoggerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)<br>    {<br>        _next = next;<br>        _logger = loggerFactory.CreateLogger&lt;CustomLoggerMiddleware&gt;();<br>    }</pre><pre>public async Task Invoke(HttpContext context)<br>    {<br>        _logger.LogInformation(&quot;**Handling request: &quot; + context.Request.Path + &quot;**&quot;);<br>        await _next.Invoke(context);<br>        _logger.LogInformation(&quot;**Finished handling request.**&quot;);<br>    }<br>}</pre><blockquote><em>We also need to create an extension method to expose our middleware through </em><strong><em>IApplicationBuilder</em></strong><em>.</em></blockquote><pre>public static class CustomLoggerExtension<br>{<br>    public static IApplicationBuilder UseCustomLogger(this IApplicationBuilder builder)<br>    {<br>        return builder.UseMiddleware&lt;CustomLoggerMiddleware&gt;();<br>    }<br>}</pre><blockquote><em>Call the extension method in the </em><em>Configuremethod in the </em><strong><em>Startup.cs</em></strong><em> class.</em></blockquote><pre>public void Configure(IApplicationBuilder app, IHostingEnvironment env)<br>{<br>    app.UseExceptionHandler(&quot;/Home/Error&quot;);<br>    app.UseHsts();<br>    app.UseHttpsRedirection();</pre><pre>app.UseCustomLogger();<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/637/1*X9mckf6mFAfCFHWG_jBN9g.png" /><figcaption>You should see a similar output from the ASP.NET Core Web Server after starting the web application</figcaption></figure><p>As you can see, the idea behind the middleware pipeline is quite simple, but it’s very powerful and it’s equally as simple to create your own middleware classes.</p><p>That’s all for this article and if you enjoyed it please don’t forget to send me a clap or two!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c60fde242ad9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>