From d689d4a98fc614cddca090792b96d303353e01aa Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:31:55 +1100 Subject: [PATCH 1/4] Fix some rider warnings --- .../src/VersionPage/VersionPageMiddleware.cs | 14 ++++---------- .../Authentication/src/InMemoryUserSessionStore.cs | 3 +-- src/Repositories/MySql/src/MySqlUtils.cs | 2 +- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/src/AspNetCore/AspNetCore/src/VersionPage/VersionPageMiddleware.cs b/src/AspNetCore/AspNetCore/src/VersionPage/VersionPageMiddleware.cs index 717eb3b..385ce38 100644 --- a/src/AspNetCore/AspNetCore/src/VersionPage/VersionPageMiddleware.cs +++ b/src/AspNetCore/AspNetCore/src/VersionPage/VersionPageMiddleware.cs @@ -14,14 +14,9 @@ public sealed class VersionPageMiddleware public VersionPageMiddleware(RequestDelegate next, ApplicationInformation applicationInformation, IOptions options) { - if (next == null) - throw new ArgumentNullException(nameof(next)); - - if (applicationInformation == null) - throw new ArgumentNullException(nameof(applicationInformation)); - - if (options == null) - throw new ArgumentNullException(nameof(options)); + ArgumentNullException.ThrowIfNull(next); + ArgumentNullException.ThrowIfNull(applicationInformation); + ArgumentNullException.ThrowIfNull(options); _next = next; _applicationInformation = applicationInformation; @@ -30,8 +25,7 @@ public VersionPageMiddleware(RequestDelegate next, ApplicationInformation applic public async Task InvokeAsync(HttpContext httpContext) { - if (httpContext == null) - throw new ArgumentNullException(nameof(httpContext)); + ArgumentNullException.ThrowIfNull(httpContext); httpContext.Response.StatusCode = 200; diff --git a/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs b/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs index db9ff54..34f9d19 100644 --- a/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs +++ b/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs @@ -9,8 +9,7 @@ public class InMemoryUserSessionStore : IUserSessionStore { - private readonly ConcurrentDictionary _sessions = - new ConcurrentDictionary(); + private readonly ConcurrentDictionary _sessions = new(); public Task GetAsync(string key, CancellationToken token = default) { diff --git a/src/Repositories/MySql/src/MySqlUtils.cs b/src/Repositories/MySql/src/MySqlUtils.cs index 482f099..c3d72e4 100644 --- a/src/Repositories/MySql/src/MySqlUtils.cs +++ b/src/Repositories/MySql/src/MySqlUtils.cs @@ -13,7 +13,7 @@ public static bool IsFailoverException(MySqlException mySqlException) { return mySqlException.Number == (int) MySqlErrorCode.UnableToConnectToHost || mySqlException.Number == (int) MySqlErrorCode.OptionPreventsStatement || - mySqlException.Number == 0 && mySqlException.HResult == -2147467259; + mySqlException is { Number: 0, HResult: -2147467259 }; } } } From 437498e8816685535258fee3f3904bdd39725f0f Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:39:58 +1100 Subject: [PATCH 2/4] ArgumentNullException.ThrowIfNull --- .../DefaultHeaders/ApplicationBuilderExtensions.cs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/AspNetCore/AspNetCore/src/Middleware/DefaultHeaders/ApplicationBuilderExtensions.cs b/src/AspNetCore/AspNetCore/src/Middleware/DefaultHeaders/ApplicationBuilderExtensions.cs index 3d12204..40cbe78 100644 --- a/src/AspNetCore/AspNetCore/src/Middleware/DefaultHeaders/ApplicationBuilderExtensions.cs +++ b/src/AspNetCore/AspNetCore/src/Middleware/DefaultHeaders/ApplicationBuilderExtensions.cs @@ -18,17 +18,10 @@ public static class ApplicationBuilderExtensions public static void UseDefaultHeaders(this IApplicationBuilder app, DefaultHeadersMiddlewareOptions options) { - if (app == null) - { - throw new ArgumentNullException(nameof(app)); - } - - if (options == null) - { - throw new ArgumentNullException(nameof(options)); - } + ArgumentNullException.ThrowIfNull(app); + ArgumentNullException.ThrowIfNull(options); app.UseMiddleware(Options.Create(options)); } } -} \ No newline at end of file +} From 5a61c56942a9803030413379261a5ba124afc9a6 Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:40:11 +1100 Subject: [PATCH 3/4] Use static compiled regex for KebabCaseParameterTransformer --- .../AspNetCore/src/Routing/KebabCaseParameterTransformer.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/AspNetCore/AspNetCore/src/Routing/KebabCaseParameterTransformer.cs b/src/AspNetCore/AspNetCore/src/Routing/KebabCaseParameterTransformer.cs index 0608992..d363f42 100644 --- a/src/AspNetCore/AspNetCore/src/Routing/KebabCaseParameterTransformer.cs +++ b/src/AspNetCore/AspNetCore/src/Routing/KebabCaseParameterTransformer.cs @@ -8,6 +8,8 @@ namespace ClickView.GoodStuff.AspNetCore.Routing; /// public class KebabCaseParameterTransformer : IOutboundParameterTransformer { + private static readonly Regex ReplacerRegex = new("([a-z])([A-Z])", RegexOptions.Compiled); + /// public string? TransformOutbound(object? value) { @@ -17,6 +19,6 @@ public class KebabCaseParameterTransformer : IOutboundParameterTransformer return null; // Slugify value - return Regex.Replace(str, "([a-z])([A-Z])", "$1-$2"); + return ReplacerRegex.Replace(str, "$1-$2"); } } From f2d1e042413902e4f2091823296c9b4ef9332584 Mon Sep 17 00:00:00 2001 From: MrSmoke <709976+MrSmoke@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:54:11 +1100 Subject: [PATCH 4/4] More ArgumentNullException updates --- .../ServiceCollectionExtensions.cs | 3 +-- .../src/InMemoryUserSessionStore.cs | 19 +++++----------- .../Infrastructure/UserSessionTicketStore.cs | 3 +-- .../src/ServiceCollectionExtensions.cs | 7 ++---- .../src/BuilderExtensions.cs | 7 ++---- .../src/RedisUserSessionStore.cs | 22 ++++++------------- 6 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/AspNetCore/AspNetCore/src/Authorization/ServiceCollectionExtensions.cs b/src/AspNetCore/AspNetCore/src/Authorization/ServiceCollectionExtensions.cs index 0cfea43..6dfe9aa 100644 --- a/src/AspNetCore/AspNetCore/src/Authorization/ServiceCollectionExtensions.cs +++ b/src/AspNetCore/AspNetCore/src/Authorization/ServiceCollectionExtensions.cs @@ -16,8 +16,7 @@ public static class ServiceCollectionExtensions /// public static void AddAjaxServices(this IServiceCollection services) { - if (services == null) - throw new ArgumentNullException(nameof(services)); + ArgumentNullException.ThrowIfNull(services); // Register the 401/403 handler services.AddTransient(); diff --git a/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs b/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs index 34f9d19..3a18dc1 100644 --- a/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs +++ b/src/AspNetCore/Authentication/Authentication/src/InMemoryUserSessionStore.cs @@ -13,8 +13,7 @@ public class InMemoryUserSessionStore : IUserSessionStore public Task GetAsync(string key, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); token.ThrowIfCancellationRequested(); @@ -35,8 +34,7 @@ public class InMemoryUserSessionStore : IUserSessionStore public Task AddAsync(UserSession session, CancellationToken token = default) { - if (session == null) - throw new ArgumentNullException(nameof(session)); + ArgumentNullException.ThrowIfNull(session); token.ThrowIfCancellationRequested(); @@ -47,11 +45,8 @@ public Task AddAsync(UserSession session, CancellationToken token = default) public Task UpdateAsync(string key, UserSession session, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); - - if (session == null) - throw new ArgumentNullException(nameof(session)); + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(session); token.ThrowIfCancellationRequested(); @@ -62,8 +57,7 @@ public Task UpdateAsync(string key, UserSession session, CancellationToken token public Task DeleteAsync(string key, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); token.ThrowIfCancellationRequested(); @@ -74,8 +68,7 @@ public Task DeleteAsync(string key, CancellationToken token = default) public Task DeleteBySessionIdAsync(string sessionId, CancellationToken token = default) { - if (sessionId == null) - throw new ArgumentNullException(nameof(sessionId)); + ArgumentNullException.ThrowIfNull(sessionId); token.ThrowIfCancellationRequested(); diff --git a/src/AspNetCore/Authentication/Authentication/src/Infrastructure/UserSessionTicketStore.cs b/src/AspNetCore/Authentication/Authentication/src/Infrastructure/UserSessionTicketStore.cs index 2b1f38b..aec0bc0 100644 --- a/src/AspNetCore/Authentication/Authentication/src/Infrastructure/UserSessionTicketStore.cs +++ b/src/AspNetCore/Authentication/Authentication/src/Infrastructure/UserSessionTicketStore.cs @@ -77,8 +77,7 @@ private UserSession CreateUserSession(string key, AuthenticationTicket ticket) if (string.IsNullOrWhiteSpace(key)) throw new InvalidOperationException("Invalid key"); - if (ticket == null) - throw new ArgumentNullException(nameof(ticket)); + ArgumentNullException.ThrowIfNull(ticket); var subject = ticket.Principal.FindFirst(JwtClaimTypes.Subject)?.Value; var sessionId = ticket.Principal.FindFirst(JwtClaimTypes.SessionId)?.Value; diff --git a/src/AspNetCore/Authentication/Authentication/src/ServiceCollectionExtensions.cs b/src/AspNetCore/Authentication/Authentication/src/ServiceCollectionExtensions.cs index 6249d43..25e1b7e 100644 --- a/src/AspNetCore/Authentication/Authentication/src/ServiceCollectionExtensions.cs +++ b/src/AspNetCore/Authentication/Authentication/src/ServiceCollectionExtensions.cs @@ -23,11 +23,8 @@ public static OpenIdConnectSessionHandlerBuilder AddOpenIdConnectSessionHandler( this IServiceCollection services, Action configure) { - if (services == null) - throw new ArgumentNullException(nameof(services)); - - if (configure == null) - throw new ArgumentNullException(nameof(configure)); + ArgumentNullException.ThrowIfNull(services); + ArgumentNullException.ThrowIfNull(configure); services.AddOptions() .Validate(o => diff --git a/src/AspNetCore/Authentication/StackExchangeRedis/src/BuilderExtensions.cs b/src/AspNetCore/Authentication/StackExchangeRedis/src/BuilderExtensions.cs index f48760f..599c1e0 100644 --- a/src/AspNetCore/Authentication/StackExchangeRedis/src/BuilderExtensions.cs +++ b/src/AspNetCore/Authentication/StackExchangeRedis/src/BuilderExtensions.cs @@ -16,11 +16,8 @@ public static OpenIdConnectSessionHandlerBuilder AddStackExchangeRedisUserSessio this OpenIdConnectSessionHandlerBuilder builder, Action configure) { - if (builder == null) - throw new ArgumentNullException(nameof(builder)); - - if (configure == null) - throw new ArgumentNullException(nameof(configure)); + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(configure); builder.Services.AddOptions() .Validate(o => diff --git a/src/AspNetCore/Authentication/StackExchangeRedis/src/RedisUserSessionStore.cs b/src/AspNetCore/Authentication/StackExchangeRedis/src/RedisUserSessionStore.cs index 0165fef..d86dc3d 100644 --- a/src/AspNetCore/Authentication/StackExchangeRedis/src/RedisUserSessionStore.cs +++ b/src/AspNetCore/Authentication/StackExchangeRedis/src/RedisUserSessionStore.cs @@ -14,12 +14,11 @@ public class RedisUserSessionStore : IUserSessionStore public RedisUserSessionStore(IOptions cacheOptions) { - if (cacheOptions == null) throw new ArgumentNullException(nameof(cacheOptions)); + ArgumentNullException.ThrowIfNull(cacheOptions); var options = cacheOptions.Value; if (options == null) throw new ArgumentException("Options cannot be null"); - if (options.Connection == null) throw new ArgumentException("Connection cannot be null"); _database = options.Connection.GetDatabase(); @@ -32,8 +31,7 @@ public RedisUserSessionStore(IOptions cacheOptions public async Task AddAsync(UserSession session, CancellationToken token = default) { - if (session == null) - throw new ArgumentNullException(nameof(session)); + ArgumentNullException.ThrowIfNull(session); token.ThrowIfCancellationRequested(); @@ -47,11 +45,8 @@ public async Task AddAsync(UserSession session, CancellationToken token = defaul public async Task UpdateAsync(string key, UserSession session, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); - - if (session == null) - throw new ArgumentNullException(nameof(session)); + ArgumentNullException.ThrowIfNull(key); + ArgumentNullException.ThrowIfNull(session); token.ThrowIfCancellationRequested(); @@ -70,8 +65,7 @@ public async Task UpdateAsync(string key, UserSession session, CancellationToken public async Task DeleteAsync(string key, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); token.ThrowIfCancellationRequested(); @@ -87,8 +81,7 @@ public async Task DeleteAsync(string key, CancellationToken token = default) public async Task DeleteBySessionIdAsync(string sessionId, CancellationToken token = default) { - if (sessionId == null) - throw new ArgumentNullException(nameof(sessionId)); + ArgumentNullException.ThrowIfNull(sessionId); token.ThrowIfCancellationRequested(); @@ -108,8 +101,7 @@ public async Task DeleteBySessionIdAsync(string sessionId, CancellationToken tok private async Task GetInternalAsync(string key, CancellationToken token = default) { - if (key == null) - throw new ArgumentNullException(nameof(key)); + ArgumentNullException.ThrowIfNull(key); token.ThrowIfCancellationRequested();