Allegro.Extensions.AspNetCore 1.4.0

Prefix Reserved
dotnet add package Allegro.Extensions.AspNetCore --version 1.4.0                
NuGet\Install-Package Allegro.Extensions.AspNetCore -Version 1.4.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Allegro.Extensions.AspNetCore" Version="1.4.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Allegro.Extensions.AspNetCore --version 1.4.0                
#r "nuget: Allegro.Extensions.AspNetCore, 1.4.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Allegro.Extensions.AspNetCore as a Cake Addin
#addin nuget:?package=Allegro.Extensions.AspNetCore&version=1.4.0

// Install Allegro.Extensions.AspNetCore as a Cake Tool
#tool nuget:?package=Allegro.Extensions.AspNetCore&version=1.4.0                

Allegro.Extensions.AspNetCore

This library contains useful extensions and utilities for ASP.NET Core based projects.

SkipOnProd

This attribute allows to easily disable specific actions or controllers on production environment. It uses Environment env variable to determine if the action should be enabled.

Remember to configure this feature in Startup.cs:

services
    .AddControllers()
    .AddSkipOnProd(_env); // pass IHostEnvironment _env

Then just add the SkipOnProd attribute to the controller:

[ApiController]
[Route("api/test")]
[SkipOnProd]
public class TestController : ControllerBase

ErrorHandling

To standardize way of handling errors (exception or/and request validation) and specially response body to have same format across service/system we introduce extension package to support this requirement.

This extension contain dedicated Error Handling Middleware and custom Invalid Model State Response Factory (for handling request model validation)

ErrorHandlingMiddleware

To enable this middleware we need to at least configure services in Startup.cs:

services
    .AddAllegroErrorHandlingMiddleware(
        logError: error => ...,
        logWarning: warning => ...
        )

and configure application in Startup.cs:

app
    .UseAllegroErrorHandlingMiddleware()

To be more independent from logging approach we need to pass action how log should be stored.

This will enable default error handling on all not handled exceptions and produce response with status code 500 in standardize format:

{
  "errors": [
    {
      "code": "UnhandledException",
      "message": "Unhandled Exception",
      "userMessage": null,
      "path": null,
      "details": null
    }
  ]
}

Error log action will be executed.

Additionally if System.ComponentModel.DataAnnotations.ValidationException is thrown in code middleware will handle it by default with status code 400 and body:

{
  "errors": [
    {
      "code": "ValidationException",
      "message": "Data validation error",
      "userMessage": null,
      "path": null,
      "details": null
    }
  ]
}

where message is built from validation result if provided. Warning log action will be executed in this case.

Handle custom exception with ErrorHandlingConfigurationBuilder

When we need to handle exception in more custom way and control status code and response body we are able to build configuration with ErrorHandlingConfigurationBuilder.

services
    .AddAllegroErrorHandlingMiddleware(
        logError: error => ...,
        logWarning: warning => ...,
        builder => builder.WithCustomHandler(...)...
        )

To check possible approaches please refer to examples in Demo app in ErrorHandlingController.

Additional instrumentation with ErrorHandlingConfigurationBuilder

To be able to enrich logs, for example with some context information we are able to register custom instrumentation like:

services
    .AddAllegroErrorHandlingMiddleware(
        logError: error => ...,
        logWarning: warning => ...,
        builder => builder.WithAdditionalInstrumentation(...)...
        )

To check possible approaches please refer to examples in Demo app in ErrorHandlingController.

Handling model state error response

To be able to control response of model state response (Data Annotations attributes in request models) package introduce configurable custom InvalidModelStateResponseFactory.

To use it you need to configure services in Startup.cs:

services
    .AddControllers()
    .AddAllegroModelStateValidationHandling();

This will enable custom handling of request validation based on data annotations and produces standardize error resposne with status code 400 in format:

{
  "errors": [
    {
      "code": "Invalid",
      "message": "The Id field is required.",
      "userMessage": null,
      "path": null,
      "details": null
    },
    {
      "code": "Invalid",
      "message": "The Id2 field is required.",
      "userMessage": null,
      "path": null,
      "details": null
    }
  ]
}

For each model entry error error item will be returned with code and validation message.

Custom model state error response handling with ErrorHandlingConfigurationBuilder

When for some reason we want to handle response for action in custom way and control response code or body we can configure custom handlign with ErrorHandlingConfigurationBuilder in Startup.cs:

services
    .AddControllers()
    .AddAllegroModelStateValidationHandling(builder => builder.WithCustomModelStateValidationErrorHandler(...));

To check possible approaches please refer to examples in Demo app in ErrorHandlingController.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.0 219 1/12/2024
1.3.0 2,222 10/14/2022