<?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: Emre Kocadere</title>
    <description>The latest articles on DEV Community by Emre Kocadere (@emrekocadere).</description>
    <link>https://dev.to/emrekocadere</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%2F1907331%2F7834d432-68a9-494d-81a6-cedc881f2191.png</url>
      <title>DEV Community: Emre Kocadere</title>
      <link>https://dev.to/emrekocadere</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emrekocadere"/>
    <language>en</language>
    <item>
      <title>C# Implicit Operator Explained with Examples</title>
      <dc:creator>Emre Kocadere</dc:creator>
      <pubDate>Tue, 16 Sep 2025 08:23:43 +0000</pubDate>
      <link>https://dev.to/emrekocadere/implicit-operator-285h</link>
      <guid>https://dev.to/emrekocadere/implicit-operator-285h</guid>
      <description>&lt;p&gt;Normally, when converting between types, you might need to use explicit casting. However, if the conversion is guaranteed to be safe (no data loss or runtime exceptions), you can define an implicit conversion. Once defined, the compiler automatically applies it wherever appropriate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A Practical Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s create a simple Metre class that internally stores a distance value as a double. We want conversion from Metre to double.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Metre
{
    public double Value { get; }

    public Metre(double value)
    {
        Value = value;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Two Ways to Define an Implicit Operator
&lt;/h2&gt;

&lt;p&gt;In C#, you can define an implicit operator in two syntactic styles: the modern expression-bodied form and the traditional block-bodied form. Both work the same way; it’s just a matter of readability and coding style.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class Metre
{
    public double Value { get; }

    public Metre(double value)
    {
        Value = value;
    }

    // Expression-bodied (short, single-line)
    public static implicit operator double(Metre metre) =&amp;gt; metre.Value;           

    // Block-bodied (classic, multi-line)
    public static implicit operator double(Metre metre)
    { 
       return metre.Value;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;double&lt;/strong&gt; → The target type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Metre metre&lt;/strong&gt; → the source type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;metre.Value&lt;/strong&gt; → the actual conversion logic: it pulls out the double value stored in the Metre object.&lt;/p&gt;

&lt;h2&gt;
  
  
  When Does a User-Defined Implicit Operator Run?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;During Assignment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you assign an object to a variable of the target type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Metre m = new Metre(5.0);
double d = m; //implicit operator automatically converts m to double
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2.In Return Statements&lt;/strong&gt;&lt;br&gt;
When returning the object from a method with the target type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Metre m = new Metre(4.0);

double GetValue() 
{
    return m; //implicit operator automatically converts m to double
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.As a Method Argument&lt;/strong&gt;&lt;br&gt;
When passing the object as a parameter to a method expecting the target type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void PrintValue(double value)
{
    Console.WriteLine(value);
}

Metre m = new Metre(7.0);

PrintValue(m); //implicit operator automatically converts m to double
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4.In Expressions&lt;/strong&gt;&lt;br&gt;
When used in mathematical or other operations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Metre m = new Metre(3.0);

double result = m + 2.0; //implicit operator automatically converts m to double
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
    </item>
    <item>
      <title>What is Quartz.Net and its simple implementation</title>
      <dc:creator>Emre Kocadere</dc:creator>
      <pubDate>Tue, 14 Jan 2025 13:55:46 +0000</pubDate>
      <link>https://dev.to/emrekocadere/what-is-quartznet-and-its-simple-implementation-38ni</link>
      <guid>https://dev.to/emrekocadere/what-is-quartznet-and-its-simple-implementation-38ni</guid>
      <description>&lt;p&gt;quartz.net is a c# library that allows specific actions to be run at certain times and periods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Job&lt;/strong&gt;:it is action that to be run&lt;br&gt;
&lt;strong&gt;Trigger&lt;/strong&gt;: it controls when a job runs&lt;br&gt;
&lt;strong&gt;Scheduler&lt;/strong&gt;:responsible for coordinating jobs and triggers&lt;/p&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;For example, you have a service about currency rates. Your service needs to go to another service every 5 minutes and get the rates. You will agree that you cannot call the function every 5 minutes.&lt;/p&gt;

&lt;p&gt;At this point, Quartz.NET comes into play.&lt;/p&gt;

&lt;p&gt;First of all, after get the library from NuGet, you need a class that implements the IJob interface to create a background job.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;IJob&lt;/strong&gt; interface includes a method called &lt;strong&gt;Execute&lt;/strong&gt;. You will write the job you want to run within this method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CurrencyRatesFetcherJob: IJob
{

    public async Task Execute(IJobExecutionContext context)
    {

      // background job..
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddQuartz(configure =&amp;gt;
{

    var jobKey = new JobKey("GetCurrencyRates");
    configure
        .AddJob&amp;lt;CurrencyRatesFetcherJob&amp;gt;(jobKey)
        .AddTrigger(
            trigger =&amp;gt; trigger.ForJob(jobKey).WithSimpleSchedule(
                schedule =&amp;gt; schedule.WithIntervalInMinutes(5).RepeatForever()));

});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration ensures that the code we wrote in Execute runs every 5 minutes.&lt;/p&gt;

&lt;p&gt;By default, Quartz configures all jobs using the &lt;strong&gt;RAMJobStore&lt;/strong&gt;. The locations where Quartz.NET jobs are stored are managed by the &lt;strong&gt;Quartz job store&lt;/strong&gt;.These &lt;strong&gt;job stores&lt;/strong&gt; are used to store the status, schedules, and other information of jobs and triggers.&lt;/p&gt;

&lt;p&gt;In my next article, I will write about using &lt;strong&gt;cron&lt;/strong&gt; for more complex schedules and how to store jobs and triggers somewhere other than RAM.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>quartz</category>
      <category>backend</category>
    </item>
    <item>
      <title>Exploring the Program.cs File: Key Components and Configuration in ASP.NET Web API</title>
      <dc:creator>Emre Kocadere</dc:creator>
      <pubDate>Mon, 19 Aug 2024 22:31:42 +0000</pubDate>
      <link>https://dev.to/emrekocadere/exploring-the-programcs-file-key-components-and-configuration-in-aspnet-web-api-kh4</link>
      <guid>https://dev.to/emrekocadere/exploring-the-programcs-file-key-components-and-configuration-in-aspnet-web-api-kh4</guid>
      <description>&lt;p&gt;The Program.cs is the main class for .NET applications. It starts the application and allows us to configure the application.&lt;br&gt;
It allows us to add things like dependencies, configuration files, and middlewares.&lt;/p&gt;

&lt;p&gt;This is a default Program.cs file for ASP.NET.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();
app.MapControllers();

app.Run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Components.
&lt;/h2&gt;

&lt;p&gt;1-&lt;strong&gt;WebApplication&lt;/strong&gt;:creates the WebApplicationBuilder instance necessary for your application and performs the basic configurations.&lt;/p&gt;

&lt;p&gt;2-&lt;strong&gt;WebApplicationBuilder&lt;/strong&gt;:It is used to start ASP.NET Core applications and to configure the application's settings and services.&lt;br&gt;
You can add dependencies and services using the Services property of the WebApplicationBuilder class.It is used to add services to the dependency injection container. These services provide the objects needed throughout the application.&lt;br&gt;
For Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddDbContext&amp;lt;UserManagementContext&amp;gt;(options =&amp;gt; options.UseNpgsql(connectionString));

builder.Services.AddScoped&amp;lt;GuidanceService&amp;gt;();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In these lines,&lt;br&gt;
&lt;strong&gt;builder.Services.AddDbContext(options =&amp;gt; options.UseNpgsql(connectionString));&lt;/strong&gt; registers the UserManagementContext with the dependency injection container using Npgsql for PostgreSQL. &lt;br&gt;
&lt;strong&gt;builder.Services.AddScoped();&lt;/strong&gt; This line registers the GuidanceService as scoped in the dependency injection container, meaning a new instance is created for each request.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;builder.Services&lt;/strong&gt; represents an instance of IServiceCollection,&lt;br&gt;
&lt;strong&gt;IServiceCollection&lt;/strong&gt; is used to register dependencies (services) in your application.&lt;br&gt;
Its official description is as follows:&lt;br&gt;
"A collection of services for the application to compose. This is useful for adding user-provided or framework-provided services."&lt;/p&gt;

&lt;p&gt;Also, you can add and read configuration files using the WebApplicationBuilder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Configuration.AddJsonFile("ConnectionString.json");

var connectionString = builder.Configuration["ConnectionString"];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code snippet adds the ConnectionString.json file to the configuration and then retrieves the value associated with the "ConnectionString" key from the configuration. &lt;/p&gt;

&lt;p&gt;3-&lt;strong&gt;var app = builder.Build();&lt;/strong&gt;:This line creates a WebApplication instance using all the services configured above. This is a fundamental step for building and starting the application.&lt;/p&gt;

&lt;p&gt;Additionally, you can add middleware using the app object, which is an instance of the WebApplication. For example, middleware like app.UseHttpsRedirection();, app.UseAuthorization();, and app.MapControllers(); has been added here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The application's startup process begins at the builder.Build() line. The application's execution starts at the app.Run() line.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>programming</category>
      <category>csharp</category>
      <category>aspdotnet</category>
    </item>
    <item>
      <title>What is Asp.Net middleware in its simplest definition?</title>
      <dc:creator>Emre Kocadere</dc:creator>
      <pubDate>Wed, 14 Aug 2024 12:54:15 +0000</pubDate>
      <link>https://dev.to/emrekocadere/what-is-aspnet-middleware-in-its-simplest-definition-9gc</link>
      <guid>https://dev.to/emrekocadere/what-is-aspnet-middleware-in-its-simplest-definition-9gc</guid>
      <description>&lt;p&gt;They are structures that make it possible to take customized actions on the Request/Response Pipeline&lt;br&gt;
The request coming to your service does not immediately fall into the action method you wrote. It first passes through the middleware. Here you can change the requests coming to your service, check identity or keep a log record. There are already middleware in asp.net.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0q3zs52qzb9whfv4byjq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0q3zs52qzb9whfv4byjq.png" alt="Image description" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have analyzed the program.cs class of the asp.net project before, you will have seen that there are already midllewares there.&lt;/p&gt;

&lt;p&gt;this is regular program.cs for asp.net web api projects&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the 2nd comment line says, you can configure Http request or response pipeline.&lt;/p&gt;

&lt;p&gt;So the things in the pipeline image above;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.UseHttpsRedirection()&lt;/strong&gt;: This code is used to redirect HTTP requests to HTTPS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.UseAuthorization()&lt;/strong&gt;: This code is used to set up authentication and authorization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.MapControllers()&lt;/strong&gt;: This defines how your API responds to clients and how your controller classes are routed.&lt;/p&gt;

&lt;p&gt;So how to add custom middleware to this pipeline?&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Add Custom Middleware?
&lt;/h2&gt;

&lt;p&gt;Middleware is actually a class and its structure is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CustomMiddleware
{
        private readonly RequestDelegate _requestDelegate;
        public CustomMiddleware(RequestDelegate requestDelegate)
        {
            _requestDelegate = requestDelegate;
        }
        public async Task Invoke(HttpContext context)
        {

        await _requestDelegate.Invoke(context);

        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The RequestDelegate&lt;/strong&gt;: is required to run the next middleware. _requestDelegate.Invoke(context) function sends the http content to the next middleware&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Invoke&lt;/strong&gt;: This is the Method that will be Executed when Middleware is called. It allows us to handle in the Request coming to the application and the Response created.&lt;br&gt;
You can change the response or request in it&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HttpContext&lt;/strong&gt;: It stores the request and response information, such as the properties of request.&lt;/p&gt;

&lt;p&gt;For example, let's write a text to the console for every request coming to the service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       public async Task Invoke(HttpContext context)
        {
        Console.WriteLine("here is CustomMiddleware");

        await _requestDelegate.Invoke(context);

        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we need to add this to the pipeline. For this, we add the middleware to program.cs.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.UseMiddleware&amp;lt;CustomMiddleware&amp;gt;();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When I send any request, a log will be written to the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F90tyxrczlvr0lo2ku53c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F90tyxrczlvr0lo2ku53c.png" alt="Image description" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It doesn't matter which endpoint it goes to. Since every request will pass through middleware, it will be printed to the console with every request.&lt;/p&gt;

&lt;p&gt;Or You can change the content of every response and request coming to your application whenever you want in Invoke method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29nrw76i761succfqs0j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F29nrw76i761succfqs0j.png" alt="Image description" width="800" height="438"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vtnxqr15xcvsfiudty4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2vtnxqr15xcvsfiudty4.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s write a simple middleware that catches exceptions.
&lt;/h2&gt;

&lt;p&gt;for that we need a Controller Class first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class ValuesController : ControllerBase
    {

        [HttpGet]
        public int Get()
        {
            int a = 0;
            int b = 15;
            int c = b / a;
            return c;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created an error inside the controller class.&lt;/p&gt;

&lt;p&gt;If this method works, the response will be as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc06hmbyl1spweqd6fjbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc06hmbyl1spweqd6fjbm.png" alt="Image description" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's write a very simple middleware to prevent our application from crashing and to catch any missed exceptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CustomMiddleware
{
    private readonly RequestDelegate _requestDelegate;
        public CustomMiddleware(RequestDelegate requestDelegate)
        {
            _requestDelegate = requestDelegate;
        }
        public async Task Invoke(HttpContext context)//
        {
            try
            {
                await _requestDelegate.Invoke(context);
            }
            catch(Exception e) {
                context.Response.StatusCode = 500;
                await context.Response.WriteAsJsonAsync(e.Message);

            }

        }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;add it to the pipeline&lt;/p&gt;

&lt;p&gt;&lt;code&gt;app.UseMiddleware&amp;lt;CustomMiddleware&amp;gt;();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After the middleware we wrote, the response will be as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvp7s102x0b2imf37d3r.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvp7s102x0b2imf37d3r.jpg" alt="Image description" width="800" height="98"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now the application continues without crashing.&lt;/p&gt;

&lt;h2&gt;
  
  
  why use middleware?
&lt;/h2&gt;

&lt;p&gt;1.Requests can be passed through the desired controls&lt;/p&gt;

&lt;p&gt;2.Solutions for Exception Handling can be provided&lt;/p&gt;

&lt;p&gt;3.Authorization&lt;/p&gt;

&lt;p&gt;4.Authentication&lt;/p&gt;

&lt;p&gt;5.You can create logging mechanisms on the application.&lt;/p&gt;

</description>
      <category>aspnet</category>
      <category>dotnet</category>
      <category>backend</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
