Implementation Of Global Exception Handling Using .NET Core 6 Web API

In this article, we will learn about global exception handling implementation using .NET Core 6 Web API step by step.

Agenda

  • Introduction
  • Implementation of Global Exception Handling

Prerequisites:

  • Visual Studio 2022
  • .NET Core 6 SDK
  • Understanding of C# Programming
  • Basic understanding of Object-Oriented Programming
  • Understanding of .NET Core APIs

Introduction

  • Exception Handling is one of the most important features we need to take care of while designing applications and handling runtime exceptions.
  • If we do not handle exceptions properly, they may impact your application and get terminated automatically.
  • There are many ways to implement exception handling during the software development phase, and from which we only use a few, like try-catch, to handle exceptions while our application is in running mode.
  • But, when we use this method, we need to write more redundant code throughout our application, which reduces the performance of the application and increases the time and space complexity unnecessarily.
  • Because of this, we use global exception handling through middleware to catch runtime errors efficiently as per our requirement.

Implementation of Global Exception Handling

Step 1

Create a new .NET Core Web API Project.

Step 2

Configure your project.

Step 3

Provide some additional information related to the project.

­­Step 4

Install the following NuGet Packages:

Step 5

Final Project Structure:

Step 6

Create a new Product class inside model.

namespace GlobalExceptionHandling.Model {
    public class Product {
        public int ProductId {
            get;
            set;
        }
        public string ProductName {
            get;
            set;
        }
        public string ProductDescription {
            get;
            set;
        }
        public int ProductPrice {
            get;
            set;
        }
        public int ProductStock {
            get;
            set;
        }
    }
}

Step 7

Next, create a new DbContextClass inside Data.

using GlobalExceptionHandling.Model;
using Microsoft.EntityFrameworkCore;
namespace GlobalExceptionHandling.Data {
    public class DbContextClass: DbContext {
        protected readonly IConfiguration Configuration;
        public DbContextClass(IConfiguration configuration) {
            Configuration = configuration;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder options) {
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
        }
        public DbSet < Product > Products {
            get;
            set;
        }
    }
}

Step 8

After that, create an IProductService interface inside the services.

using GlobalExceptionHandling.Model;
namespace GlobalExceptionHandling.Services {
    public interface IProductService {
        public Task < IEnumerable < Product >> GetProductList();
        public Task < Product > GetProductById(int id);
        public Task < Product > AddProduct(Product product);
        public Task < Product > UpdateProduct(Product product);
        public Task < bool > DeleteProduct(int Id);
    }
}

Step 9

Next, create ProductService with interface implementation inside services.

using GlobalExceptionHandling.Data;
using GlobalExceptionHandling.Model;
using Microsoft.EntityFrameworkCore;
namespace GlobalExceptionHandling.Services {
    public class ProductService: IProductService {
        private readonly DbContextClass _dbContext;
        public ProductService(DbContextClass dbContext) {
            _dbContext = dbContext;
        }
        public async Task < IEnumerable < Product >> GetProductList() {
            return await _dbContext.Products.ToListAsync();
        }
        public async Task < Product > GetProductById(int id) {
            return await _dbContext.Products.Where(x => x.ProductId == id).FirstOrDefaultAsync();
        }
        public async Task < Product > AddProduct(Product product) {
            var result = _dbContext.Products.Add(product);
            await _dbContext.SaveChangesAsync();
            return result.Entity;
        }
        public async Task < Product > UpdateProduct(Product product) {
            var result = _dbContext.Products.Update(product);
            await _dbContext.SaveChangesAsync();
            return result.Entity;
        }
        public async Task < bool > DeleteProduct(int Id) {
            var filteredData = _dbContext.Products.Where(x => x.ProductId == Id).FirstOrDefault();
            var result = _dbContext.Remove(filteredData);
            await _dbContext.SaveChangesAsync();
            return result != null ? true : false;
        }
    }
}

Step 10

Register a few services inside the Program class related to product and database services.

using GlobalExceptionHandling.Data;
using GlobalExceptionHandling.Services;
using GlobalExceptionHandling.Utility;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped < IProductService, ProductService > ();
builder.Services.AddDbContext < DbContextClass > ();
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();

Step 11

Add a database connection string inside the appsetting.json file.

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
        "DefaultConnection": "Data Source=DESKTOP;Initial Catalog=GlobalExceptionHandling;User Id=sa;Password=*****@1;"
    }
}

Step 12 - Create Product Controller

Here, we throw an exception inside get product by id and filter method at lines number 35 and 63. You can use any exception that you want to handle.

using GlobalExceptionHandling.Exceptions;
using GlobalExceptionHandling.Model;
using GlobalExceptionHandling.Services;
using Microsoft.AspNetCore.Mvc;
using NotImplementedException = GlobalExceptionHandling.Exceptions.NotImplementedException;
namespace GlobalExceptionHandling.Controllers {
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController: ControllerBase {
        private readonly IProductService productService;
        private ILogger < ProductController > _logger;
        public ProductController(IProductService _productService, ILogger < ProductController > logger) {
                productService = _productService;
                _logger = logger;
            }
            [HttpGet("productlist")]
        public Task < IEnumerable < Product >> ProductList() {
                var productList = productService.GetProductList();
                return productList;
            }
            [HttpGet("getproductbyid")]
        public Task < Product > GetProductById(int Id) {
                _logger.LogInformation($ "Fetch Product with ID: {Id} from the database");
                var product = productService.GetProductById(Id);
                if (product.Result == null) {
                    throw new NotFoundException($ "Product ID {Id} not found.");
                }
                _logger.LogInformation($ "Returning product with ID: {product.Result.ProductId}.");
                return product;
            }
            [HttpPost("addproduct")]
        public Task < Product > AddProduct(Product product) {
                return productService.AddProduct(product);
            }
            [HttpPut("updateproduct")]
        public Task < Product > UpdateProduct(Product product) {
                return productService.UpdateProduct(product);
            }
            [HttpDelete("deleteproduct")]
        public Task < bool > DeleteProduct(int Id) {
                return productService.DeleteProduct(Id);
            }
            [HttpGet("filterproduct")]
        public Task < List < Product >> FilterProduct(int Id) {
            throw new NotImplementedException("Not Implemented Exception!");
        }
    }
}

Step 13

Execute database migration scripts inside the package manager console.

add-migration “Initial”

update-database

Step 14

Create multiple exception classes inside the Exception.

BadRequestException

namespace GlobalExceptionHandling.Exceptions {
    public class BadRequestException: Exception {
        public BadRequestException(string message): base(message) {}
    }
}

KeyNotFoundException

namespace GlobalExceptionHandling.Exceptions {
    public class KeyNotFoundException: Exception {
        public KeyNotFoundException(string message): base(message) {}
    }
}

NotFoundException

namespace GlobalExceptionHandling.Exceptions {
    public class NotFoundException: Exception {
        public NotFoundException(string message): base(message) {}
    }
}

NotImplementedException

namespace GlobalExceptionHandling.Exceptions {
    public class NotImplementedException: Exception {
        public NotImplementedException(string message): base(message) {}
    }
}

UnauthorizedAccessException

namespace GlobalExceptionHandling.Exceptions {
    public class UnauthorizedAccessException: Exception {
        public UnauthorizedAccessException(string message): base(message) {}
    }
}

Step 15

Next, create a GlobalErrorHandlingMiddleware inside utility to handle exceptions.

using GlobalExceptionHandling.Exceptions;
using System.Net;
using System.Text.Json;
using KeyNotFoundException = GlobalExceptionHandling.Exceptions.KeyNotFoundException;
using NotImplementedException = GlobalExceptionHandling.Exceptions.NotImplementedException;
using UnauthorizedAccessException = GlobalExceptionHandling.Exceptions.UnauthorizedAccessException;
namespace GlobalExceptionHandling.Utility {
    public class GlobalErrorHandlingMiddleware {
        private readonly RequestDelegate _next;
        public GlobalErrorHandlingMiddleware(RequestDelegate next) {
            _next = next;
        }
        public async Task Invoke(HttpContext context) {
            try {
                await _next(context);
            } catch (Exception ex) {
                await HandleExceptionAsync(context, ex);
            }
        }
        private static Task HandleExceptionAsync(HttpContext context, Exception exception) {
            HttpStatusCode status;
            var stackTrace = String.Empty;
            string message;
            var exceptionType = exception.GetType();
            if (exceptionType == typeof(BadRequestException)) {
                message = exception.Message;
                status = HttpStatusCode.BadRequest;
                stackTrace = exception.StackTrace;
            } else if (exceptionType == typeof(NotFoundException)) {
                message = exception.Message;
                status = HttpStatusCode.NotFound;
                stackTrace = exception.StackTrace;
            } else if (exceptionType == typeof(NotImplementedException)) {
                status = HttpStatusCode.NotImplemented;
                message = exception.Message;
                stackTrace = exception.StackTrace;
            } else if (exceptionType == typeof(UnauthorizedAccessException)) {
                status = HttpStatusCode.Unauthorized;
                message = exception.Message;
                stackTrace = exception.StackTrace;
            } else if (exceptionType == typeof(KeyNotFoundException)) {
                status = HttpStatusCode.Unauthorized;
                message = exception.Message;
                stackTrace = exception.StackTrace;
            } else {
                status = HttpStatusCode.InternalServerError;
                message = exception.Message;
                stackTrace = exception.StackTrace;
            }
            var exceptionResult = JsonSerializer.Serialize(new {
                error = message, stackTrace
            });
            context.Response.ContentType = "application/json";
            context.Response.StatusCode = (int) status;
            return context.Response.WriteAsync(exceptionResult);
        }
    }
}
  • The use of global error handling middleware is to handle exceptions. It is a type of custom middleware. So, it provides more flexibility to handle the request and response pipeline, which is provided by .NET Core.
  • In the above class, you can see that we used RequestDelegate through dependency injection inside constructor and _next parameter, which is used to process the incoming HTTP request. Below that, the Invoke method is a process that executes the next middleware and processes the request.
  • If the request is not processed properly and has some issue, then control goes to catch and call or the HandleExceptionAsync method to handle the exception and configure stack trace.

Step 16

Configure your middleware inside the program class.

using GlobalExceptionHandling.Data;
using GlobalExceptionHandling.Services;
using GlobalExceptionHandling.Utility;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped < IProductService, ProductService > ();
builder.Services.AddDbContext < DbContextClass > ();
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();
}
//configure exception middleware
app.UseMiddleware(typeof(GlobalErrorHandlingMiddleware));
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

Step 17

Finally, run your application.

Step 18

In the below images, you see exceptions encountered when passing invalid product id, or sometimes call unimplemented methods. I used only these two examples, but there are many scenarios in which you use and handle the exception as per your needs.

Conclusion

In this article, we discussed the introduction of exception handling and practical implementation of that using custom middleware step-by-step.

Happy coding!


Similar Articles