
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What is the usage of DelegatingHandler in Asp.Net webAPI C#?
In a message handler, a series of message handlers are chained together. The first handler receives an HTTP request, does some processing, and gives the request to the next handler. At some point, the response is created and goes back up the chain. This pattern is called a delegating handler.
Along with the built-in Server-side Message Handlers, we can also create our own Server-Side HTTP Message Handlers. To create a custom Server-Side HTTP Message Handler in ASP.NET Web API, we make use of DelegatingHandler. We have to create a class deriving from System.Net.Http.DelegatingHandler. That custom class then should override the SendAsync method.
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken);
The method takes an HttpRequestMessage as input and asynchronously returns an HttpResponseMessage. A typical implementation does the following −
- Process the request message.
- Call base.SendAsync to send the request to the inner handler.
- The inner handler returns a response message. (This step is asynchronous.)
- Process the response and return it to the caller.
Example
public class CustomMessageHandler : DelegatingHandler{ protected async override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken){ Debug.WriteLine("CustomMessageHandler processing the request"); // Calling the inner handler var response = await base.SendAsync(request, cancellationToken); Debug.WriteLine("CustomMessageHandler processing the response"); return response; } }
A delegating handler can also skip the inner handler and directly create the response.
Example
public class CustomMessageHandler: DelegatingHandler{ protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken){ // Create the response var response = new HttpResponseMessage(HttpStatusCode.OK){ Content = new StringContent("Skipping the inner handler") }; // TaskCompletionSource creates a task that does not contain a delegate var taskCompletion = new TaskCompletionSource<HttpResponseMessage>(); taskCompletion.SetResult(response); return taskCompletion.Task; } }
- Related Articles
- What is Content Negotiation in Asp.Net webAPI C#?
- What is parameter binding in C# ASP.NET WebAPI?
- What is the use of Authorize Attribute in C# Asp.Net webAPI?
- What are the advantages of using C# ASP.NET WebAPI?
- What are the different types of filters in C# ASP.NET WebAPI?
- What are built-in message handlers in Asp.Net webAPI C#?
- What are the various return types of a controller action in C# ASP.NET WebAPI?
- How can we test C# Asp.Net WebAPI?
- How to resolve CORS issue in C# ASP.NET WebAPI?
- How to configure C# ASP.NET WebAPI in web.configure file?
- How do we specify MIME type in Asp.Net WebAPI C#?
- How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
- How to do versioning with accept header in C# ASP.NET WebAPI?
- How to add custom message handlers to the pipeline in Asp.Net webAPI C#?
- How to do Web API versioning with URI in C# ASP.NET WebAPI?
