<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ashutosh Rajput</title>
    <description>The latest articles on DEV Community by Ashutosh Rajput (@ashu_rajput).</description>
    <link>https://dev.to/ashu_rajput</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1181192%2F61b6ed03-4626-4be3-9d9f-79ba15bdce1c.png</url>
      <title>DEV Community: Ashutosh Rajput</title>
      <link>https://dev.to/ashu_rajput</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashu_rajput"/>
    <language>en</language>
    <item>
      <title>What is a middleware and for what purposes we can use it ?</title>
      <dc:creator>Ashutosh Rajput</dc:creator>
      <pubDate>Tue, 10 Oct 2023 04:52:21 +0000</pubDate>
      <link>https://dev.to/ashu_rajput/what-is-a-middleware-and-for-what-purposes-we-can-use-it--o7i</link>
      <guid>https://dev.to/ashu_rajput/what-is-a-middleware-and-for-what-purposes-we-can-use-it--o7i</guid>
      <description>&lt;p&gt;Middleware is a software assembled into an app pipeline to handle request and response.&lt;/p&gt;

&lt;p&gt;In simple words middleware gives us the facility to add additional logic before and after the HTTP request.&lt;/p&gt;

&lt;p&gt;We can use middlewares for following purposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Caching&lt;/li&gt;
&lt;li&gt;Logging&lt;/li&gt;
&lt;li&gt;Routing&lt;/li&gt;
&lt;li&gt;Static Files&lt;/li&gt;
&lt;li&gt;Rate Limiting &lt;/li&gt;
&lt;li&gt;Compression&lt;/li&gt;
&lt;li&gt;Authorization&lt;/li&gt;
&lt;li&gt;Authentication&lt;/li&gt;
&lt;li&gt;Error Handling&lt;/li&gt;
&lt;li&gt;Content Type Negotiation&lt;/li&gt;
&lt;li&gt;CORS (Cross-Origin Resource Sharing)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We have three ways to create middleware:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Request delegate&lt;/li&gt;
&lt;li&gt;By convention&lt;/li&gt;
&lt;li&gt;Using middleware factory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I already wrote an article about how to create middlewares, you can read it here and if you enjoyed that make sure to subscribe my newsletter for more amazing content ??&lt;br&gt;
&lt;a href="https://dev.to/ashu_rajput/what-is-middleware-and-multiple-ways-to-create-middleware-in-net-core--32li"&gt;https://dev.to/ashu_rajput/what-is-middleware-and-multiple-ways-to-create-middleware-in-net-core--32li&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--awyAgZiE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv4sbdeax9r1o0wl2q51.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--awyAgZiE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bv4sbdeax9r1o0wl2q51.png" alt="how to create middleware in .net" width="483" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What is middleware and multiple ways to create middleware in .NET Core ?</title>
      <dc:creator>Ashutosh Rajput</dc:creator>
      <pubDate>Tue, 10 Oct 2023 04:40:22 +0000</pubDate>
      <link>https://dev.to/ashu_rajput/what-is-middleware-and-multiple-ways-to-create-middleware-in-net-core--32li</link>
      <guid>https://dev.to/ashu_rajput/what-is-middleware-and-multiple-ways-to-create-middleware-in-net-core--32li</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is middleware ?&lt;/strong&gt;&lt;br&gt;
Middleware is a software assembled into an app pipeline to handle request and response.&lt;br&gt;
In simple words middleware gives us the facility to add additional logic before and after the HTTP request.&lt;br&gt;
&lt;strong&gt;How middleware works ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9yxDngVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1vdqw8pfahngjjhwmva.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9yxDngVH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1vdqw8pfahngjjhwmva.png" alt="How middleware works" width="600" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Suppose we have three middlewares in our app then for each middleware we enter two time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First middleware logic is performed then we send it to next middleware&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Secondly when request comes back after executing the next middlewares&lt;br&gt;
&lt;strong&gt;Multiple ways of creating middlewares&lt;/strong&gt;&lt;br&gt;
We have three ways to create middleware in .NET Core&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Request delegate (Most simple)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;By convention&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using middleware factory&lt;br&gt;
&lt;strong&gt;Creating middleware using Request Delegate&lt;/strong&gt;&lt;br&gt;
In this way we can create middleware using &lt;code&gt;app.Use&lt;/code&gt; in our Program.cs&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can add our logic that we want to perform before or after this middleware.&lt;/p&gt;

&lt;p&gt;If you don’t want to call next middleware we can achieve that by not invoking next.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;Program.cs&lt;/code&gt; file would look like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ers0_F6---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/111i8nz8uc36y4ibek9j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ers0_F6---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/111i8nz8uc36y4ibek9j.png" alt="Program file" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To learn more about this technique visit &lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-6.0"&gt;Microsoft Docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating middleware by Convention&lt;/strong&gt;&lt;br&gt;
It has three steps :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a class of your middleware and pass RequestDelegate in its constructor.&lt;/li&gt;
&lt;li&gt;Now add a method responsible for logic before and after the middleware, pass it HttpContext&lt;/li&gt;
&lt;li&gt;Add middleware in Program.cs
After first two step our code would be like :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--17rTsolk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s144nzcsz4036ylsr500.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--17rTsolk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s144nzcsz4036ylsr500.png" alt="Creating middleware by Convention" width="800" height="370"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tell the Program.cs about this middleware by using &lt;code&gt;app.UseMiddleware&amp;lt;name&amp;gt;()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9CuuJFtb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qskldjg46h3nxj6wpjbr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9CuuJFtb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qskldjg46h3nxj6wpjbr.png" alt="Use Middleware" width="720" height="82"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating middleware using Factory&lt;/strong&gt;&lt;br&gt;
This is modified version of conventional way , in this we create our class and inherit it from &lt;code&gt;IMiddleware&lt;/code&gt;. In this way &lt;code&gt;IMiddleware&lt;/code&gt; enforces us to implement &lt;code&gt;InvokeAsync&lt;/code&gt; method. So the code looks like :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KF43h4K3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or2puj3exshvj41leuzy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KF43h4K3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or2puj3exshvj41leuzy.png" alt="Creating middleware using Factory" width="720" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One last step is to informing &lt;code&gt;Program.cs&lt;/code&gt; about this middleware , but this time we can not do it only with &lt;code&gt;app.UseMiddleware&lt;/code&gt; , we have to add it in built-in DI container. We have to register its service as well like this :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xIHKFug---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejefvaf1xdvk4agk8opf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xIHKFug---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejefvaf1xdvk4agk8opf.png" alt="DI container" width="720" height="132"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The factory method is most appreciated by developers because it has less chance of error and it is more clean.&lt;/p&gt;

&lt;p&gt;Explore more about last two approaches from &lt;a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-6.0"&gt;Microsoft Docs&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
