Skip to content

Commit

Permalink
Conditionally add custom IActionResultExecutor to work with minimal APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
khellang committed Aug 8, 2022
1 parent 0ef888c commit 090976f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/ProblemDetails/MinimalApiResultExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if NET6_0_OR_GREATER
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System.Threading.Tasks;

namespace Hellang.Middleware.ProblemDetails
{
internal class MinimalApiResultExecutor : IActionResultExecutor<ObjectResult>
{
public Task ExecuteAsync(ActionContext context, ObjectResult result) =>
Results.Json(result.Value, options: null, "application/problem+json", result.StatusCode)
.ExecuteAsync(context.HttpContext);
}
}
#endif
4 changes: 2 additions & 2 deletions src/ProblemDetails/ProblemDetails.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>6.4.4</Version>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<Version>6.5.0</Version>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<AssemblyName>Hellang.Middleware.ProblemDetails</AssemblyName>
<RootNamespace>Hellang.Middleware.ProblemDetails</RootNamespace>
<Description>Error handling middleware, using RFC7807</Description>
Expand Down
14 changes: 11 additions & 3 deletions src/ProblemDetails/ProblemDetailsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using LibProblemDetailsFactory = Hellang.Middleware.ProblemDetails.ProblemDetailsFactory;

// TODO: Move to Microsoft.Extensions.DependencyInjection
namespace Hellang.Middleware.ProblemDetails
{
public static class ProblemDetailsExtensions
Expand Down Expand Up @@ -31,18 +35,22 @@ public static IServiceCollection AddProblemDetails(this IServiceCollection servi
services.Configure(configure);
}

services.TryAddSingleton<ProblemDetailsFactory>();
services.TryAddSingleton<LibProblemDetailsFactory>();
services.TryAddSingleton<ProblemDetailsMarkerService, ProblemDetailsMarkerService>();
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<ProblemDetailsOptions>, ProblemDetailsOptionsSetup>());

#if NET6_0_OR_GREATER
services.TryAddSingleton<IActionResultExecutor<ObjectResult>, MinimalApiResultExecutor>();
#endif

return services;
}

/// <summary>
/// Adds the <see cref="ProblemDetailsMiddleware"/> to the application pipeline.
/// </summary>
/// <param name="app">The application builder to add the middleware to.</param>
/// <exception cref="InvalidOperationException">If <see cref="AddProblemDetails(Microsoft.Extensions.DependencyInjection.IServiceCollection)"/> hasn't been called.</exception>
/// <exception cref="InvalidOperationException">If <see cref="AddProblemDetails(IServiceCollection)"/> hasn't been called.</exception>
public static IApplicationBuilder UseProblemDetails(this IApplicationBuilder app)
{
var markerService = app.ApplicationServices.GetService<ProblemDetailsMarkerService>();
Expand Down

0 comments on commit 090976f

Please sign in to comment.