<?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>Forem: Sanket Naik</title>
    <description>The latest articles on Forem by Sanket Naik (@sankdeveloper).</description>
    <link>https://forem.com/sankdeveloper</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%2F1121295%2Fe8c34d97-f3de-4f81-b2db-79ccc4130030.png</url>
      <title>Forem: Sanket Naik</title>
      <link>https://forem.com/sankdeveloper</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sankdeveloper"/>
    <language>en</language>
    <item>
      <title>Trying To Solve The Confusion of Choice Between gRPC vs REST🕵</title>
      <dc:creator>Sanket Naik</dc:creator>
      <pubDate>Sat, 22 Jul 2023 13:19:28 +0000</pubDate>
      <link>https://forem.com/sankdeveloper/trying-to-solve-the-confusion-of-choice-between-grpc-vs-rest-4hii</link>
      <guid>https://forem.com/sankdeveloper/trying-to-solve-the-confusion-of-choice-between-grpc-vs-rest-4hii</guid>
      <description>&lt;h3&gt;
  
  
  Introduction:
&lt;/h3&gt;

&lt;p&gt;Recently I came across the discussion of choosing between gRPC or REST Service. So thought to summarize my research and opinion in a form of blog post, so that it will be helpful for one who are interested in this topic.&lt;/p&gt;

&lt;p&gt;I saw there is some confusion among us as a Developer to choose one over another. So, let's get some dive into this topic. Hope this will give you some insights to give a fresh thought.&lt;/p&gt;

&lt;p&gt;There is a lot to discuss about this topic. However, I have tried to keep this blog as concise as possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  RPC vs REST:📢
&lt;/h3&gt;

&lt;p&gt;Yes, it's not a spell mistake!&lt;br&gt;
Before diving into the comparison, I think it is important to go through some fundamental difference between &lt;strong&gt;RPC&lt;/strong&gt; and &lt;strong&gt;REST&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RPC:&lt;/strong&gt;&lt;br&gt;
I think &lt;strong&gt;RPC&lt;/strong&gt; was there even before REST. REST came into picture in after evolving era of internet over HTTP protocol.&lt;br&gt;
PC APIs &lt;strong&gt;allows&lt;/strong&gt; developers &lt;strong&gt;to call remote functions in external servers&lt;/strong&gt; as if they were local to their software.(&lt;strong&gt;action oriented in short&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;Examples of RPC:&lt;br&gt;
A client app &lt;strong&gt;“calling a remote API method”&lt;/strong&gt; to perform &lt;strong&gt;“actions”&lt;/strong&gt; like processing payment, sending messages, or retrieving data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REST:&lt;/strong&gt;&lt;br&gt;
Where in &lt;strong&gt;“REpresentational State Transfer”&lt;/strong&gt; is an architectural style that focuses on CRUD(Get/Post/Put/Delete) &lt;strong&gt;“specifically for the Resource”&lt;/strong&gt; (e.g., books, users) (&lt;strong&gt;Resource oriented in short&lt;/strong&gt;)&lt;/p&gt;

&lt;p&gt;Example of REST:&lt;br&gt;
A mobile app fetching product information by making an HTTP GET request to a RESTful API endpoint, enabling CRUD operations (Create, Read, Update, Delete) on resources like user profiles, posts, or photos.&lt;/p&gt;

&lt;h3&gt;
  
  
  Then Why REST replaced RPC?
&lt;/h3&gt;

&lt;p&gt;REST didn’t replace RPC, but it grown as a popular alternative for building web services and APIs for various reasons. Both REST and RPC are different architectural styles with their own strengths and use cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  gRPC:📢
&lt;/h3&gt;

&lt;p&gt;Originally, the &lt;strong&gt;motivation&lt;/strong&gt; for Google behind gRPC was to connect the large number of microservices running &lt;strong&gt;on same or different data centers&lt;/strong&gt;.&lt;br&gt;
As the &lt;strong&gt;payload size&lt;/strong&gt; of gRPC vs REST is much &lt;strong&gt;optimized&lt;/strong&gt; in size, it is somewhere 5 to 8 times faster than REST data transferred over network.&lt;br&gt;
So they just picked the &lt;strong&gt;power&lt;/strong&gt; of &lt;strong&gt;HTTP/2&lt;/strong&gt; and &lt;strong&gt;protobuff&lt;/strong&gt; on top of RPC. &lt;a href="https://grpc.io/blog/principles/" rel="noopener noreferrer"&gt;ref&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One of the key feature of gRPC is protobuf .proto file(nothing but just a contract for me between two communicator code components)&lt;br&gt;
This file and protobuff compiler is so mature, then it generates a direct client implementation using &lt;code&gt;protoc&lt;/code&gt;compiler. &lt;a href="https://github.com/protocolbuffers/protobuf" rel="noopener noreferrer"&gt;ref&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  gRPC weaknesses:
&lt;/h3&gt;

&lt;p&gt;Every technology comes with their own power and downside; hence it is important to focus on their use and Use cases.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Limited &lt;strong&gt;browser support&lt;/strong&gt;: since gRPC uses HTTP/2 protocol, not all modern browsers support very well.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not&lt;/strong&gt; human-readable &lt;strong&gt;format&lt;/strong&gt; of message: I read some articles heilighting this as weakness. But in my openion, why should we care about this ? as far as sender and reciver understands it very well, I dont see any problem with this.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited&lt;/strong&gt; number of &lt;strong&gt;error codes&lt;/strong&gt;: gRPC has less number of error codes if we compare with REST. &lt;a href="https://grpc.github.io/grpc/core/md_doc_statuscodes.html" rel="noopener noreferrer"&gt;ref&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Challenge and Considerations When Using gRPC in Microservices
&lt;/h3&gt;

&lt;p&gt;I think, it makes sense to use gRPC in microservice(internal layer) of architecture but &lt;strong&gt;carefully&lt;/strong&gt;!&lt;br&gt;
Why am I emphasizing on ‘carefully’? First let's see the &lt;strong&gt;problem&lt;/strong&gt; referring &lt;strong&gt;below&lt;/strong&gt; scenario,&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AaZt1BfMCiusjJrk8lHoaBA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2AaZt1BfMCiusjJrk8lHoaBA.png" alt="Addressing problem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Client (referring to a service caller) calls for an action to gRPC_service_1.&lt;br&gt;
gRPC_service_1 service internally &lt;strong&gt;“maybe”&lt;/strong&gt;(we don’t know as a caller) calling gRPC_service_2, and gRPC_service_2 might call other services internally.&lt;/p&gt;

&lt;p&gt;But there is one challenge with this design!&lt;br&gt;
What if, there is a &lt;strong&gt;strong business workflow&lt;/strong&gt; involved/associated in this hole cycle and somehow gRPC_service_3 fails? As a result, it will cause some inconsistency within services, and gRPC_service_1 does not know which of the components failed in the chain (rather I would say failure in business workflow).&lt;/p&gt;

&lt;h3&gt;
  
  
  My Thoughts of using gRPC effectively in web API development✍
&lt;/h3&gt;

&lt;p&gt;I am proposing above &lt;strong&gt;high-level architecture&lt;/strong&gt; of where I think gRPC makes more sense, and why.&lt;br&gt;
&lt;strong&gt;I considered below&lt;/strong&gt; points while proposing this high-level architecture.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Most of the browsers doesn’t support HTTP/2 hence gRPC very well.&lt;/li&gt;
&lt;li&gt;Architecture should be workable with both the scenario:
&lt;strong&gt;a)&lt;/strong&gt; if having strong business workflow, and hence better to decouple the modules
&lt;strong&gt;b)&lt;/strong&gt; if do not have strong business workflow and flow ok with failure in chain.&lt;/li&gt;
&lt;li&gt;Architecture should be flexible enough for shifting to gRPC seamlessly from REST, without any change in internal modules.&lt;/li&gt;
&lt;li&gt;Should be scalable over time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's Architect,&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A6TtbgQp9dO79ttrbXs405w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2000%2F1%2A6TtbgQp9dO79ttrbXs405w.png" alt="Proposed Architecture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Okay, some developers may be having some diabetes over these components. Be flexible enough to change any of the above modules based on your needs or Use case.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Web Interface Layer:&lt;/strong&gt;&lt;br&gt;
🔹&lt;strong&gt;Gateway Service:&lt;/strong&gt; &lt;br&gt;
&lt;strong&gt;If&lt;/strong&gt; you are using this layer as your REST gateway service and your internal services are hidden from the external user, it's a big win to tackle the browser compatibility problem for WebUI clients.&lt;br&gt;
Most of the Gateway services supports to configure Upstream as REST protocol and Downstream as gRPC and vice versa. By this way you do not need to worry about switching from gRPC to REST for your external clients without changing your internal structure. Isn’t it Great!!!&lt;/p&gt;

&lt;p&gt;🔹&lt;strong&gt;BFF Service:&lt;/strong&gt;&lt;br&gt;
This is one of another Usecase where you make your view composition by using underlying core services. &lt;br&gt;
Aggregating your logic using gRPC and sending back data to your clients as per your choice or protocol (e.g. REST/WebSocket) makes your decision flexible enough.&lt;/p&gt;

&lt;p&gt;🔹&lt;strong&gt;Aggregator Service:&lt;/strong&gt;&lt;br&gt;
This service is very much similar to the BFF, but the core difference is, BFF is built with frontend needs in mind, and responsibility of Aggregator service is to summarize the data by contacting two or more services behind the scenes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Business Workflow?” block:&lt;/strong&gt;&lt;br&gt;
I am not sure; what term this block we should call to. You can suggest a better name, but you get the point!&lt;br&gt;
This block is little bit tricky while implementation and taking decisions.&lt;/p&gt;

&lt;p&gt;If there is no business workflow involved, then I think it's okay to have direct call to another service to retrieve data. You can use any resiliency framework internally though to deal with temporary service down delay.&lt;/p&gt;

&lt;p&gt;But if, there is business workflow involved then you can &lt;strong&gt;consider&lt;/strong&gt; &lt;strong&gt;messaging queue&lt;/strong&gt; to make your flows/system &lt;strong&gt;decoupled&lt;/strong&gt; and &lt;strong&gt;flexible&lt;/strong&gt; enough in nature. Using messaging queue brings up its own power and strength though.&lt;/p&gt;

&lt;h3&gt;
  
  
  Okay, then shall I ‘move’ from REST to gRPC now?
&lt;/h3&gt;

&lt;p&gt;The answer I would suggest: If you are starting a project/service newly (newly, I meant the reference is not exists yet anywhere), you can give a thought to have service as gRPC.&lt;br&gt;
But there is no concrete reason that you must have it until and unless there is any need.&lt;/p&gt;

&lt;h3&gt;
  
  
  References for more detail📗
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://aws.amazon.com/compare/the-difference-between-rpc-and-rest/" rel="noopener noreferrer"&gt;RPC vs REST — Difference Between API Architectures — AWS (amazon.com)&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://learn.microsoft.com/en-us/aspnet/core/grpc/performance?view=aspnetcore-7.0" rel="noopener noreferrer"&gt;Performance best practices with gRPC | Microsoft Learn&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://grpc.github.io/grpc/core/md_doc_statuscodes.html" rel="noopener noreferrer"&gt;GRPC Core: Status codes and their use in gRPC&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>grpc</category>
      <category>architecture</category>
      <category>cleancode</category>
    </item>
    <item>
      <title>Efficient Way of Writing Controlled Global Exceptions in .NET Core🕵</title>
      <dc:creator>Sanket Naik</dc:creator>
      <pubDate>Mon, 17 Jul 2023 14:02:15 +0000</pubDate>
      <link>https://forem.com/sankdeveloper/efficient-way-of-writing-controlled-global-exceptions-in-net-core-5ca3</link>
      <guid>https://forem.com/sankdeveloper/efficient-way-of-writing-controlled-global-exceptions-in-net-core-5ca3</guid>
      <description>&lt;p&gt;&lt;strong&gt;Introduction 📢&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Hey folks!!!&lt;/strong&gt;&lt;br&gt;
How are you all!!! Here I am with the new blog “Efficient Way of Writing Controlled Global Exceptions in.NET Core”.&lt;/p&gt;

&lt;p&gt;In this &lt;strong&gt;exciting&lt;/strong&gt; blog titled “Efficient Way of Writing Controlled Global Exceptions in.NET Core” I’ll share valuable insights on effectively managing exceptions in a &lt;strong&gt;controlled&lt;/strong&gt; manner, resulting in a positive impact on your application. This article offers unique perspectives that you won’t find elsewhere. So, get ready to dive into some interesting learnings!&lt;/p&gt;

&lt;p&gt;Make sure to pay attention to the specific use case I’ll discuss later in the article below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why you need custom loggings&lt;/strong&gt;🤔&lt;br&gt;
There are numerous reasons why you may desire these features, but for the sake of this article’s focus and objective, I won’t delve into all of them here. However, I will provide a list of some common reasons:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Customizing logging &lt;strong&gt;behavior&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Tracking &lt;strong&gt;additional&lt;/strong&gt; contextual information.&lt;/li&gt;
&lt;li&gt;Logging to &lt;strong&gt;multiple destinations&lt;/strong&gt; (&lt;em&gt;e.g., displaying informational logs in the console while storing only failed requests in the LoggingDB&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;Improving logging &lt;strong&gt;readability&lt;/strong&gt; for future analysis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integrating&lt;/strong&gt; and customizing behavior for multiple destinations (&lt;em&gt;e.g., storing informational logs in different databases and error logs in separate databases&lt;/em&gt;).&lt;/li&gt;
&lt;li&gt;Presenting a &lt;strong&gt;well-formatted&lt;/strong&gt; error structure to API users or consumers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Any other way to write global level logs other than Middleware way?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Yes&lt;/strong&gt;, you can implement &lt;code&gt;IExceptionFilter&lt;/code&gt;. However, it's essential to understand the &lt;strong&gt;fundamental&lt;/strong&gt; difference between &lt;code&gt;IExceptionFilter&lt;/code&gt; and &lt;code&gt;CustomExceptionHandlerMiddleware&lt;/code&gt;in terms of their execution within the request pipeline. Perhaps I will write another article that provides a detailed comparison and helps you choose between the two.&lt;/p&gt;

&lt;p&gt;For now, the diagram below will give you a general understanding of when each component comes into play:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UfUkDB8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AlUI8-4LM9GCThQzC85jvEA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UfUkDB8Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AlUI8-4LM9GCThQzC85jvEA.png" alt="CustomExceptionMiddleware Vs ExceptionFilter" width="447" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;‘UseCase’ for the approach that I use✍:&lt;/strong&gt;&lt;br&gt;
I have multiple microservices as a part of my big codebase. These multiple web API services will have almost same code for &lt;code&gt;GlobalExceptionHandlerMiddleware&lt;/code&gt;, which basically handles unhandled exceptions at the request level.&lt;/p&gt;

&lt;p&gt;However, I need to be &lt;strong&gt;consistent&lt;/strong&gt; in terms of standard &lt;strong&gt;HTTP response codes&lt;/strong&gt; so that I don’t violate &lt;strong&gt;DRY&lt;/strong&gt; principles.&lt;/p&gt;

&lt;p&gt;For the most commonly encountered exceptions, the best practices follow the following HTTP codes.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--21EbI2WW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A6ChdeV79fzgJCbBhxx4jXA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--21EbI2WW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2A6ChdeV79fzgJCbBhxx4jXA.png" alt="" width="508" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's architect this scenario into code 🏹:&lt;/strong&gt;&lt;br&gt;
Yah! so, the idea is &lt;strong&gt;to abstract&lt;/strong&gt; the most used exceptions along with their http code in a separate shared library.&lt;br&gt;
Below, we have Three exception types in our &lt;strong&gt;shared library&lt;/strong&gt; &lt;code&gt;DomainException&lt;/code&gt;,&lt;code&gt;ApplicationException&lt;/code&gt;and &lt;code&gt;InfrastructureException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And the consumers will inherit from below &lt;strong&gt;Three&lt;/strong&gt; base exceptions.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LzwEOHiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AKNY6mdUu-Rid5dy1cN62zw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LzwEOHiF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AKNY6mdUu-Rid5dy1cN62zw.png" alt="High level idea" width="571" height="406"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Write &lt;code&gt;DomainException&lt;/code&gt; base&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DomainException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;DomainException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;DomainException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;DomainException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write &lt;code&gt;ApplicationException&lt;/code&gt; base&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;ApplicationException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;ApplicationException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;ApplicationException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write &lt;code&gt;InfrastructureException&lt;/code&gt;base&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Serializable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InfrastructureException&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;InfrastructureException&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;InfrastructureException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

        &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nf"&gt;InfrastructureException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write Middleware implementation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// GlobalExceptionHandlerMiddleware.cs&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GlobalExceptionHandlerMiddleware&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;InvokeAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;_next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;ConfigureAwait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DomainException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BadRequest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApplicationException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InternalServerError&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InfrastructureException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ServiceUnavailable&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ArgumentException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BadRequest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InternalServerError&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;HandleException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;logRequestBody&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Log into Infra/Console/File etc&lt;/span&gt;
        &lt;span class="nf"&gt;WriteResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;//Modify and return request context&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;WriteResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpContext&lt;/span&gt; &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HttpStatusCode&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StatusCode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;statusCode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;responseModel&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;..........&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;httpContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;JsonSerializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;responseModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_seralizeSetting&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UTF8&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;    &lt;span class="c1"&gt;//Expose an extension method from your shared library&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;IApplicationBuilder&lt;/span&gt; &lt;span class="nf"&gt;UseGlobalExceptionHandling&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="n"&gt;IApplicationBuilder&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;UseMiddleware&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;GlobalExceptionHandlerMiddleware&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Finally, Make sure your code line &lt;code&gt;app.UseGlobalExceptionHandeling()&lt;/code&gt; as your first middleware in request pipeline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enjoy using Exceptions from consumer code:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3rhNBFsP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AYmNPceUK65bRh_87pg0UQw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3rhNBFsP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2000/1%2AYmNPceUK65bRh_87pg0UQw.png" alt="This is your Client/Consumer code" width="679" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;📚&lt;br&gt;
I’m excited to share this blog titled “Efficient Way of Writing Controlled Global Exceptions in .NET Core”! In this article, I’ll provide valuable insights on how you can effectively manage exceptions in a controlled manner, ensuring a positive impact on your application.&lt;/p&gt;

&lt;p&gt;We saw below points:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We were onto mission on: valuable &lt;strong&gt;insights&lt;/strong&gt; on how you can effectively manage exceptions in a controlled manner.&lt;/li&gt;
&lt;li&gt;We saw the ‘&lt;strong&gt;&lt;em&gt;why&lt;/em&gt;&lt;/strong&gt;’ part of ‘&lt;strong&gt;&lt;em&gt;need of custom logging&lt;/em&gt;&lt;/strong&gt;’.&lt;/li&gt;
&lt;li&gt;Then we saw the &lt;strong&gt;UseCase&lt;/strong&gt; of above approach I had written.&lt;/li&gt;
&lt;li&gt;We saw the &lt;strong&gt;HTTP codes&lt;/strong&gt; for general types of exceptions.&lt;/li&gt;
&lt;li&gt;Then we also went through the &lt;strong&gt;implementation&lt;/strong&gt; idea and the actual &lt;strong&gt;hands-on code&lt;/strong&gt; by making our hands dirty.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Above is just example in my perticular use case. You can have your own/different &lt;em&gt;ExceptionsTypes&lt;/em&gt; that most suits in your context.🙌&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>cleancode</category>
      <category>architecture</category>
      <category>netcoremiddleware</category>
      <category>bestprectices</category>
    </item>
  </channel>
</rss>
