Skip to content

Commit 2c407b6

Browse files
committed
Remove unused pre-compilation conditions
The conditions were used for pre-.NET 8 which are not supported anymore.
1 parent 51a60d4 commit 2c407b6

File tree

5 files changed

+2
-83
lines changed

5 files changed

+2
-83
lines changed

samples/WebApi.Minimal/Program.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,9 @@
99

1010
var builder = WebApplication.CreateBuilder(args);
1111
#if CHANGE_PROPERTY_NAME_POLICY
12-
#if NET8_0_OR_GREATER
1312
builder.Services.ConfigureHttpJsonOptions(options => {
1413
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseUpper;
1514
});
16-
#else
17-
builder.Services.Configure<JsonOptions>(options => {
18-
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
19-
});
20-
#endif
2115
#endif
2216
builder.AddExceptionMapper(builder =>
2317
{
@@ -74,9 +68,7 @@
7468
"---[Others]---",
7569
"/fallback",
7670
"/a-url-that-does-not-exist",
77-
#if NET7_0_OR_GREATER
7871
"/fluent-validation?name=&description=&range=0",
79-
#endif
8072
});
8173
app.MapGet("/BadRequestException", context => throw new BadRequestException());
8274
app.MapGet("/ConflictException", context => throw new ConflictException());
@@ -96,7 +88,6 @@
9688
app.MapGet("/MyUnauthorizedException", context => throw new MyUnauthorizedException(Random.Shared.Next(100) % 2 == 0 ? "John" : "Jane"));
9789

9890
app.MapGet("/fallback", context => throw new Exception("An error that gets handled by the fallback handler."));
99-
#if NET7_0_OR_GREATER
10091
app.MapGet("/fluent-validation", ([AsParameters] Entity entity) =>
10192
{
10293
var validator = new EntityValidator();
@@ -107,7 +98,6 @@
10798
}
10899
return TypedResults.Ok(entity);
109100
});
110-
#endif
111101
app.Run();
112102

113103

src/ForEvolve.ExceptionMapper/DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,16 @@ public static WebApplicationBuilder AddExceptionMapper(this WebApplicationBuilde
5353

5454
private static void AddSerializationHandler(this IServiceCollection services, IConfiguration configuration)
5555
{
56-
#if NET7_0_OR_GREATER
5756
services.ConfigureHttpJsonOptions(options => {
5857
options.SerializerOptions.DictionaryKeyPolicy = options.SerializerOptions.PropertyNamingPolicy;
5958
});
60-
#endif
6159
services
6260
.AddOptions<ProblemDetailsSerializationOptions>()
6361
.Bind(configuration.GetSection("ExceptionMapper:ProblemDetailsSerialization"))
6462
.ValidateOnStart()
6563
;
6664
services.AddSingleton(sp => sp.GetRequiredService<IOptions<ProblemDetailsSerializationOptions>>().Value);
67-
#if NET7_0_OR_GREATER
6865
services.AddProblemDetails();
69-
#endif
70-
// Workaround: binding a local copy of the DefaultProblemDetailsFactory because the .NET class is internal.
71-
// Moreover, the only way to add the class is by calling the AddMvcCore method, which add way more services.
72-
// So until we can add the DefaultProblemDetailsFactory
7366
services.TryAddSingleton<ProblemDetailsFactory, DefaultProblemDetailsFactory>();
7467
services.TryAddSingleton<IExceptionSerializer, ProblemDetailsSerializationHandler>();
7568
}

src/ForEvolve.ExceptionMapper/Serialization/DefaultProblemDetailsFactory.cs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
#if !NET9_0_OR_GREATER
1+
// Load this only for .NET 8.
2+
#if !NET9_0_OR_GREATER
23
// Licensed to the .NET Foundation under one or more agreements.
34
// The .NET Foundation licenses this file to you under the MIT license.
45

@@ -102,42 +103,3 @@ private void ApplyProblemDetailsDefaults(HttpContext httpContext, ProblemDetails
102103
}
103104
}
104105
#endif
105-
#if NET6_0
106-
public class ProblemDetailsOptions
107-
{
108-
/// <summary>
109-
/// The operation that customizes the current <see cref="Mvc.ProblemDetails"/> instance.
110-
/// </summary>
111-
public Action<ProblemDetailsContext>? CustomizeProblemDetails { get; set; }
112-
}
113-
114-
public class ProblemDetailsContext
115-
{
116-
private ProblemDetails? _problemDetails;
117-
118-
/// <summary>
119-
/// The <see cref="HttpContext"/> associated with the current request being processed by the filter.
120-
/// </summary>
121-
public HttpContext? HttpContext { get; init; }
122-
123-
/// <summary>
124-
/// A collection of additional arbitrary metadata associated with the current request endpoint.
125-
/// </summary>
126-
public EndpointMetadataCollection? AdditionalMetadata { get; init; }
127-
128-
/// <summary>
129-
/// An instance of <see cref="ProblemDetails"/> that will be
130-
/// used during the response payload generation.
131-
/// </summary>
132-
public ProblemDetails ProblemDetails
133-
{
134-
get => _problemDetails ??= new ProblemDetails();
135-
set => _problemDetails = value;
136-
}
137-
138-
/// <summary>
139-
/// The exception causing the problem or <c>null</c> if no exception information is available.
140-
/// </summary>
141-
public Exception? Exception { get; init; }
142-
}
143-
#endif

src/ForEvolve.ExceptionMapper/Serialization/Json/ProblemDetailsSerializationHandler.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,11 @@ public class ProblemDetailsSerializationHandler : IExceptionSerializer
1717
private readonly ProblemDetailsFactory _problemDetailsFactory;
1818
private readonly IHostEnvironment _hostEnvironment;
1919
private readonly ProblemDetailsSerializationOptions _options;
20-
#if NET7_0_OR_GREATER
2120
private readonly IProblemDetailsService _problemDetailsService;
22-
#endif
2321
private readonly JsonSerializerOptions _jsonSerializerOptions;
2422

2523
public ProblemDetailsSerializationHandler(
26-
#if NET7_0_OR_GREATER
2724
IProblemDetailsService problemDetailsService,
28-
#endif
2925
ProblemDetailsFactory problemDetailsFactory,
3026
IHostEnvironment hostEnvironment,
3127
ProblemDetailsSerializationOptions options,
@@ -34,9 +30,7 @@ public ProblemDetailsSerializationHandler(
3430
_problemDetailsFactory = problemDetailsFactory ?? throw new ArgumentNullException(nameof(problemDetailsFactory));
3531
_hostEnvironment = hostEnvironment ?? throw new ArgumentNullException(nameof(hostEnvironment));
3632
_options = options ?? throw new ArgumentNullException(nameof(options));
37-
#if NET7_0_OR_GREATER
3833
_problemDetailsService = problemDetailsService ?? throw new ArgumentNullException(nameof(problemDetailsService));
39-
#endif
4034
_jsonSerializerOptions = jsonOptions.Value.SerializerOptions ?? throw new ArgumentNullException(nameof(jsonOptions));
4135
}
4236

@@ -113,27 +107,13 @@ public async Task ExecuteAsync(ExceptionHandlingContext ctx)
113107
}
114108

115109
// Output the problem details
116-
#if NET7_0_OR_GREATER
117110
var problemDetailsContext = new ProblemDetailsContext
118111
{
119112
HttpContext = ctx.HttpContext,
120-
#if NET8_0_OR_GREATER
121113
Exception = ctx.Error,
122-
#endif
123114
ProblemDetails = problemDetails,
124115
};
125116
await _problemDetailsService.WriteAsync(problemDetailsContext);
126-
#else
127-
#pragma warning disable CS0618 // Type or member is obsolete
128-
ctx.HttpContext.Response.ContentType = _options.ContentType;
129-
await JsonSerializer.SerializeAsync(
130-
ctx.HttpContext.Response.Body,
131-
problemDetails,
132-
_jsonSerializerOptions,
133-
cancellationToken: ctx.HttpContext.RequestAborted
134-
);
135-
#pragma warning restore CS0618 // Type or member is obsolete
136-
#endif
137117
}
138118

139119
private string FormatName(string name)

src/ForEvolve.ExceptionMapper/Serialization/Json/ProblemDetailsSerializationOptions.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,4 @@ public class ProblemDetailsSerializationOptions
66
{
77
public bool SerializeExceptions { get; set; } = true;
88
public Func<ExceptionHandlingContext, bool> DisplayDebugInformation { get; set; } = (ExceptionHandlingContext ctx) => false;
9-
#if NET6_0
10-
private const string _obsoleteMessage = "This property was removed when targeting .NET 7+. The library now leverages the `IProblemDetailsService` interface instead.";
11-
12-
[Obsolete(_obsoleteMessage)]
13-
public string ContentType { get; set; } = "application/problem+json; charset=utf-8";
14-
#endif
159
}

0 commit comments

Comments
 (0)