Skip to main content

Posts

Showing posts with the label Dotnet5

Usage Of CancellationToken In Asp.Net Core Applications

When To Use CancellationToken?: In a web application request abortion or orphan, requests are quite common. On users disconnected by network interruption or navigating between multiple pages before proper response or closing of the browser, tabs make the request aborted or orphan. An orphan request can't deliver a response to the client, but it will execute all steps(like database calls, HTTP calls, etc) at the server. Complete execution of an orphan request at the server might not be a problem generally if at all requests need to work on time taking a job at the server in those cases might be nice to terminate the execution immediately. So CancellationToken can be used to terminate a request execution at the server immediately once the request is aborted or orphan. Here we are going to see some sample code snippets about implementing a CancellationToken for Entity FrameworkCore, Dapper ORM, and HttpClient calls in Asp.NetCore MVC application. Note: The sample codes I will show in

Blazor WebAssembly File Upload Using MudBlazor UI Components

In this article, we are going to implement a Blazor WebAssembly application file upload using MudBlazor UI components. Create A Sample Blazor WebAssembly Application: Let's create a sample Blazor WebAssembly application to accomplish our demo on file uploading. Initial MudBlazor Setup: Install the 'MudBlazor' library package. Add Mudblazor namespace into the '_Imports.razor'. _Imports.razor: @using MudBlazor Add the below CSS files inside of the head tag in 'index.html'. wwwroot/index.html: <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" /> <link href="_content/MudBlazor/MudBlazor.min.css" rel="stylesheet" /> Now comment the 'bootstrap.min.css' and '{your_applicationname}.styles.css' links in the head tag. Add MudBlazor javascript file in 'index.html' just above the closing body tag. wwwroot/index.html: <script

A Demo On Hot Chocolate GraphQL Integration In Asp.Net Core Application Using Dapper Micro ORM

In this article, we are going to implement a GraphQL Endpoint by using Pure Code First Techniques in the Asp.Net Core application, and database communication will be accomplished by using Dapper Micro ORM. GraphQL: GraphQL is an open-source data query and manipulation and language for APIs. It is a query language for your API and a server-side runtime for executing queries by using a type system you define for your data. GraphQL can be integrated into any framework like .Net, Java, NestJS, etc and it isn't tied to any specific database or storage engine and is instead backed by your existing code and data. GraphQL main operations are: Query (fetching data) Mutation (saving or updating data) Hot Chocolate GraphQL: Hot Chocolate is the wrapper library of the original.Net GraphQL library. Hot Chocolate takes the complexity away from building a fully-fledged GraphQL server. The hot Chocolate library provides 3 different techniques: Schema First Code First Pure Code First Schema Fi

A Demo On Request Logging Using MediatR IPipelineBehavior In Asp.Net Core Application

Click here  for step by step implementation of the CQRS MediatR design pattern. In this article, we are going to do request logging into an Asp.Net application Core application build on the CQRS MediatR design pattern. The application request logging helps to track the time taken per request. The MediatR provides a pipeline interface that is 'IPipelineBehavior' that helps to implement our logging logic. IPipelineBehavior: The 'IPipelineBehavior' exactly works like Asp.Net Core middleware, but its starting execution and ending execution happens within the 'IMediator'. So the 'IMediator.send()' is usually used to invoke the 'Query' or 'Command' handlers. So if we implement 'IPipelineBehavior' then begin logic inside of its start executes then invokes 'Query' or 'Command' handlers, later again go through 'IPipelineBehavior' and executes the end logic. Create A .Net5 Web API Sample Application: In this

Model Validation In Asp.Net Core Application Using Fluent Validation Library

In this article, we will explore the usage of the Fluent Validation library in the Asp.Net Core application. Fluent Validation Library: Fluent Validation is a popular .Net library for enabling validation rules. It has a lot of features like in-built validation rules that are most commonly used in any application and also has flexible support for creating custom rules. Implementing Fluent validation rules is a cleaner approach than using the 'DataAnnotation'(.Net default implementation for applying validation rules). It can be automatically injected into .NetCore model validations without any explicit implementation. How To Implement Fluent Validation To A Model: To apply fluent validation to the model, we have to create a separate validator class. The validator class must inherit from 'FluentValidation.AbstractValidator<TModel>'. Inside of the constructor need to apply required validation for the properties of 'TModel'. public class Student { public