Clean up #if hell a bit more (#2450)

* Clean up #if hell a bit more

* Add missing null checks
This commit is contained in:
Sebastian Göls
2021-11-10 20:36:17 +01:00
committed by GitHub
parent 0850a261cb
commit 7019445b84
2 changed files with 18 additions and 7 deletions

View File

@@ -354,14 +354,7 @@ namespace ArchiSteamFarm.IPC {
mvc.AddApiExplorer();
#endif
#if NETFRAMEWORK
// Add JSON formatters that will be used as default ones if no specific formatters are asked for
mvc.AddJsonFormatters();
mvc.AddJsonOptions(
#else
mvc.AddNewtonsoftJson(
#endif
static options => {
// Fix default contract resolver to use original names and not a camel case
options.SerializerSettings.ContractResolver = new DefaultContractResolver();

View File

@@ -21,6 +21,7 @@
#if NETFRAMEWORK
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
#endif
using System;
@@ -42,6 +43,23 @@ namespace ArchiSteamFarm.IPC {
return services.AddMvcCore();
}
internal static IMvcCoreBuilder AddNewtonsoftJson(this IMvcCoreBuilder mvc, Action<MvcJsonOptions> setupAction) {
if (mvc == null) {
throw new ArgumentNullException(nameof(mvc));
}
if (setupAction == null) {
throw new ArgumentNullException(nameof(setupAction));
}
// Add JSON formatters that will be used as default ones if no specific formatters are asked for
mvc.AddJsonFormatters();
mvc.AddJsonOptions(setupAction);
return mvc;
}
internal static IServiceCollection AddRequestLocalization(this IServiceCollection services, Action<RequestLocalizationOptions> action) {
if (services == null) {
throw new ArgumentNullException(nameof(services));