Class HttpExceptionDescriptorResponseHandlerDecoratorExtensions
- Namespace
- Cuemon.AspNetCore.Diagnostics
- Assembly
- Cuemon.AspNetCore.dll
Extension methods for the HttpExceptionDescriptorResponseHandler class hidden behind the IDecorator<T> interface.
public static class HttpExceptionDescriptorResponseHandlerDecoratorExtensions
- Inheritance
-
HttpExceptionDescriptorResponseHandlerDecoratorExtensions
Examples
HttpExceptionDescriptorResponseHandlerDecoratorExtensions provides extension methods on Decorator.Enclose for registering additional response handlers that control how HTTP exception descriptors are serialized and returned. This example creates an initial HttpExceptionDescriptorResponseHandler for application/json with a 500 Internal Server Error status, wraps a List<HttpExceptionDescriptorResponseHandler> containing it, and calls AddResponseHandler with an options delegate specifying a custom content type, ContentFactory, and StatusCodeFactory. After execution, the list contains both handlers configured for different response scenarios in the error pipeline.
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Cuemon;
using Cuemon.AspNetCore.Diagnostics;
namespace MyApp.Examples;
public class HttpExceptionDescriptorResponseHandlerDecoratorExtensionsExample
{
public void Demonstrate()
{
var handler = new HttpExceptionDescriptorResponseHandler(
new MediaTypeHeaderValue("application/json"),
ed => new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(ed.Message)
});
var list = new List<HttpExceptionDescriptorResponseHandler> { handler };
Decorator.Enclose(list).AddResponseHandler(o =>
{
o.ContentType = new MediaTypeHeaderValue("application/json");
o.ContentFactory = ed => new StringContent(ed.Message);
o.StatusCodeFactory = ed => HttpStatusCode.InternalServerError;
});
}
}
Methods
AddResponseHandler(IDecorator<ICollection<HttpExceptionDescriptorResponseHandler>>, Action<HttpExceptionDescriptorResponseHandlerOptions>)
Adds an HttpExceptionDescriptorResponseHandler to the underlying list of HttpExceptionDescriptorResponseHandler from the specified the decorator.
public static ICollection<HttpExceptionDescriptorResponseHandler> AddResponseHandler(this IDecorator<ICollection<HttpExceptionDescriptorResponseHandler>> decorator, Action<HttpExceptionDescriptorResponseHandlerOptions> setup)
Parameters
decoratorIDecorator<ICollection<HttpExceptionDescriptorResponseHandler>>The IDecorator<T> to extend.
setupAction<HttpExceptionDescriptorResponseHandlerOptions>The HttpExceptionDescriptorResponseHandlerOptions that needs to be configured.
Returns
- ICollection<HttpExceptionDescriptorResponseHandler>
A reference to IDecorator.Inner of
decoratorso that additional calls can be chained.
Exceptions
- ArgumentNullException
decoratorcannot be null - or - IDecorator.Inner property ofdecoratorcannot be null - or -setupcannot be null.- ArgumentException
setupfailed to configure an instance of HttpExceptionDescriptorResponseHandlerOptions in a valid state.