Skip to content

Commit 9d9b1e4

Browse files
authoredFeb 8, 2025··
fix: added new analyzers and multiple fixes (#937)
1 parent a70f7e0 commit 9d9b1e4

15 files changed

+50
-84
lines changed
 

‎src/Directory.Build.props

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@
1818
<NoWarn>CS1591</NoWarn>
1919
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
2020
<RestoreLockedMode Condition="'$(ContinuousIntegrationBuild)' == 'true'">true</RestoreLockedMode>
21-
</PropertyGroup>
21+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
22+
</PropertyGroup>
23+
24+
<PropertyGroup Label="Analysis rules">
25+
<AnalysisLevel>latest-Recommended</AnalysisLevel>
26+
<NoWarn>$(NoWarn);CA1848</NoWarn> <!-- For improved performance, use the LoggerMessage delegates -->
27+
<NoWarn>$(NoWarn);CA2201</NoWarn> <!-- Exception type System.Exception is not sufficiently specific -->
28+
<NoWarn>$(NoWarn);CA2208</NoWarn> <!--TryAddAsync passes 'xxx' as the paramName argument to a ArgumentNullException constructor-->
29+
</PropertyGroup>
2230

2331
<ItemGroup>
24-
<None Include="../../finbuckle-128x128.png" Pack="true" PackagePath="/"/>
25-
<None Include="../../README.md" Pack="true" PackagePath="/"/>
32+
<None Include="../../finbuckle-128x128.png" Pack="true" PackagePath="/" />
33+
<None Include="../../README.md" Pack="true" PackagePath="/" />
2634
</ItemGroup>
2735
</Project>

‎src/Finbuckle.MultiTenant.AspNetCore/Extensions/MultiTenantBuilderExtensions.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ public static MultiTenantBuilder<TTenantInfo> WithPerTenantAuthenticationConvent
7373
options.Events.OnValidatePrincipal = async context =>
7474
{
7575
// Skip if bypass set (e.g. ClaimsStrategy in effect)
76-
if (context.HttpContext.Items.Keys.Contains(
77-
$"{Constants.TenantToken}__bypass_validate_principal__"))
76+
if (context.HttpContext.Items.ContainsKey($"{Constants.TenantToken}__bypass_validate_principal__"))
7877
return;
7978

8079
var currentTenant = context.HttpContext.GetMultiTenantContext<TTenantInfo>().TenantInfo?.Identifier;
@@ -372,8 +371,7 @@ private static void BypassSessionPrincipalValidation<TTenantInfo>(
372371
options.Events.OnValidatePrincipal = async context =>
373372
{
374373
// Skip if bypass set (e.g. ClaimStrategy in effect)
375-
if (context.HttpContext.Items.Keys.Contains(
376-
$"{Constants.TenantToken}__bypass_validate_principal__"))
374+
if (context.HttpContext.Items.ContainsKey($"{Constants.TenantToken}__bypass_validate_principal__"))
377375
return;
378376

379377
await origOnValidatePrincipal(context);

‎src/Finbuckle.MultiTenant.AspNetCore/Internal/MultiTenantAuthenticationSchemeProvider.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public MultiTenantAuthenticationSchemeProvider(IAuthenticationSchemeProvider inn
138138

139139
if (scheme == null)
140140
{
141-
scheme = _schemes.ContainsKey(name) ? _schemes[name] : null;
141+
scheme = _schemes.TryGetValue(name, out AuthenticationScheme? value) ? value : null;
142142
}
143143

144144
return scheme;
@@ -189,9 +189,8 @@ public virtual void RemoveScheme(string name)
189189
}
190190
lock (_lock)
191191
{
192-
if (_schemes.ContainsKey(name))
192+
if (_schemes.TryGetValue(name, out AuthenticationScheme? scheme))
193193
{
194-
var scheme = _schemes[name];
195194
_requestHandlers.Remove(scheme);
196195
_schemes.Remove(name);
197196
}

‎src/Finbuckle.MultiTenant.AspNetCore/Internal/MultiTenantAuthenticationService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private static void AddTenantIdentifierToProperties(HttpContext context, ref Aut
3232
if (multiTenantContext?.TenantInfo != null)
3333
{
3434
properties ??= new AuthenticationProperties();
35-
if(!properties.Items.Keys.Contains(Constants.TenantToken))
35+
if(!properties.Items.ContainsKey(Constants.TenantToken))
3636
properties.Items.Add(Constants.TenantToken, multiTenantContext.TenantInfo.Identifier);
3737
}
3838
}

‎src/Finbuckle.MultiTenant.AspNetCore/MultiTenantAuthenticationOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ namespace Finbuckle.MultiTenant.AspNetCore;
55

66
public class MultiTenantAuthenticationOptions
77
{
8-
public bool SkipChallengeIfTenantNotResolved { get; set; } = false;
8+
public bool SkipChallengeIfTenantNotResolved { get; set; }
99
}

‎src/Finbuckle.MultiTenant.AspNetCore/Strategies/HostStrategy.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public HostStrategy(string template)
5353
string singleSegmentPattern = @"[^\.]+";
5454
if (template.Substring(template.Length - 3, 3) == @"\.*")
5555
{
56-
template = template.Substring(0, template.Length - 3) + wildcardSegmentsPattern;
56+
template = string.Concat(template.AsSpan(0, template.Length - 3), wildcardSegmentsPattern);
5757
}
5858

5959
wildcardSegmentsPattern = @"([^\.]+\.)*";

‎src/Finbuckle.MultiTenant.EntityFrameworkCore/Extensions/MultiTenantDbContextExtensions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
2727
Where(e => e.Metadata.IsMultiTenant()).ToList();
2828

2929
// ensure tenant context is valid
30-
if (changedMultiTenantEntities.Any())
30+
if (changedMultiTenantEntities.Count != 0)
3131
{
3232
if (tenantInfo == null)
3333
throw new MultiTenantException("MultiTenant Entity cannot be changed if TenantInfo is null.");
@@ -42,7 +42,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
4242
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
4343
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
4444

45-
if (mismatchedAdded.Any())
45+
if (mismatchedAdded.Count != 0)
4646
{
4747
switch (tenantMismatchMode)
4848
{
@@ -80,7 +80,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
8080
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
8181
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
8282

83-
if (mismatchedModified.Any())
83+
if (mismatchedModified.Count != 0)
8484
{
8585
switch (tenantMismatchMode)
8686
{
@@ -104,7 +104,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
104104
var notSetModified = modifiedMultiTenantEntities.
105105
Where(e => (string?)e.Property("TenantId").CurrentValue == null).ToList();
106106

107-
if (notSetModified.Any())
107+
if (notSetModified.Count != 0)
108108
{
109109
switch (tenantNotSetMode)
110110
{
@@ -129,7 +129,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
129129
Where(e => (string?)e.Property("TenantId").CurrentValue != null &&
130130
(string?)e.Property("TenantId").CurrentValue != tenantInfo.Id).ToList();
131131

132-
if (mismatchedDeleted.Any())
132+
if (mismatchedDeleted.Count != 0)
133133
{
134134
switch (tenantMismatchMode)
135135
{
@@ -150,7 +150,7 @@ public static void EnforceMultiTenant<TContext>(this TContext context) where TCo
150150
var notSetDeleted = deletedMultiTenantEntities.
151151
Where(e => (string?)e.Property("TenantId").CurrentValue == null).ToList();
152152

153-
if (notSetDeleted.Any())
153+
if (notSetDeleted.Count != 0)
154154
{
155155
switch (tenantNotSetMode)
156156
{

‎src/Finbuckle.MultiTenant/DependencyInjection/MultiTenantBuilderExtensions.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ public static MultiTenantBuilder<TTenantInfo> WithInMemoryStore<TTenantInfo>(thi
122122
Action<InMemoryStoreOptions<TTenantInfo>> config)
123123
where TTenantInfo : class, ITenantInfo, new()
124124
{
125-
if (config == null)
126-
{
127-
throw new ArgumentNullException(nameof(config));
128-
}
125+
ArgumentNullException.ThrowIfNull(config);
129126

130127
// ReSharper disable once RedundantTypeArgumentsOfMethod
131128
builder.Services.Configure<InMemoryStoreOptions<TTenantInfo>>(config);
@@ -173,10 +170,7 @@ public static MultiTenantBuilder<TTenantInfo> WithDelegateStrategy<TTenantInfo>(
173170
Func<object, Task<string?>> doStrategy)
174171
where TTenantInfo : class, ITenantInfo, new()
175172
{
176-
if (doStrategy == null)
177-
{
178-
throw new ArgumentNullException(nameof(doStrategy));
179-
}
173+
ArgumentNullException.ThrowIfNull(doStrategy);
180174

181175
return builder.WithStrategy<DelegateStrategy>(ServiceLifetime.Singleton, new object[] { doStrategy });
182176
}

‎src/Finbuckle.MultiTenant/DependencyInjection/OptionsBuilderExtensions.cs

+10-11
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public static OptionsBuilder<TOptions> ConfigurePerTenant<TOptions, TTenantInfo>
1919
where TOptions : class
2020
where TTenantInfo : class, ITenantInfo, new()
2121
{
22-
// TODO use ThrowNull here?
23-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
22+
ArgumentNullException.ThrowIfNull(configureOptions);
2423

2524
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
2625

@@ -44,7 +43,7 @@ public static OptionsBuilder<TOptions> ConfigurePerTenant<TOptions, TDep, TTenan
4443
where TDep : class
4544
where TTenantInfo : class, ITenantInfo, new()
4645
{
47-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
46+
ArgumentNullException.ThrowIfNull(configureOptions);
4847

4948
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
5049

@@ -70,7 +69,7 @@ public static OptionsBuilder<TOptions> ConfigurePerTenant<TOptions, TDep1, TDep2
7069
where TDep2 : class
7170
where TTenantInfo : class, ITenantInfo, new()
7271
{
73-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
72+
ArgumentNullException.ThrowIfNull(configureOptions);
7473

7574
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
7675

@@ -99,7 +98,7 @@ public static OptionsBuilder<TOptions> ConfigurePerTenant<TOptions, TDep1, TDep2
9998
where TDep3 : class
10099
where TTenantInfo : class, ITenantInfo, new()
101100
{
102-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
101+
ArgumentNullException.ThrowIfNull(configureOptions);
103102

104103
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
105104

@@ -130,7 +129,7 @@ public static OptionsBuilder<TOptions> ConfigurePerTenant<TOptions, TDep1, TDep2
130129
where TDep4 : class
131130
where TTenantInfo : class, ITenantInfo, new()
132131
{
133-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
132+
ArgumentNullException.ThrowIfNull(configureOptions);
134133

135134
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
136135

@@ -194,7 +193,7 @@ public static OptionsBuilder<TOptions> PostConfigurePerTenant<TOptions, TTenantI
194193
where TOptions : class
195194
where TTenantInfo : class, ITenantInfo, new()
196195
{
197-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
196+
ArgumentNullException.ThrowIfNull(configureOptions);
198197

199198
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
200199

@@ -218,7 +217,7 @@ public static OptionsBuilder<TOptions> PostConfigurePerTenant<TOptions, TDep, TT
218217
where TDep : class
219218
where TTenantInfo : class, ITenantInfo, new()
220219
{
221-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
220+
ArgumentNullException.ThrowIfNull(configureOptions);
222221

223222
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
224223

@@ -244,7 +243,7 @@ public static OptionsBuilder<TOptions> PostConfigurePerTenant<TOptions, TDep1, T
244243
where TDep2 : class
245244
where TTenantInfo : class, ITenantInfo, new()
246245
{
247-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
246+
ArgumentNullException.ThrowIfNull(configureOptions);
248247

249248
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
250249

@@ -273,7 +272,7 @@ public static OptionsBuilder<TOptions> PostConfigurePerTenant<TOptions, TDep1, T
273272
where TDep3 : class
274273
where TTenantInfo : class, ITenantInfo, new()
275274
{
276-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
275+
ArgumentNullException.ThrowIfNull(configureOptions);
277276

278277
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
279278

@@ -304,7 +303,7 @@ public static OptionsBuilder<TOptions> PostConfigurePerTenant<TOptions, TDep1, T
304303
where TDep4 : class
305304
where TTenantInfo : class, ITenantInfo, new()
306305
{
307-
if (configureOptions == null) throw new ArgumentNullException(nameof(configureOptions));
306+
ArgumentNullException.ThrowIfNull(configureOptions);
308307

309308
FinbuckleServiceCollectionExtensions.ConfigurePerTenantReqs<TOptions>(optionsBuilder.Services);
310309

‎src/Finbuckle.MultiTenant/DependencyInjection/ServiceCollectionExtensions.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public static IServiceCollection PostConfigureAllPerTenant<TOptions, TTenantInfo
257257
internal static void ConfigurePerTenantReqs<TOptions>(IServiceCollection services)
258258
where TOptions : class
259259
{
260-
if (services == null) throw new ArgumentNullException(nameof(services));
260+
ArgumentNullException.ThrowIfNull(services);
261261

262262
// Required infrastructure.
263263
services.AddOptions();
@@ -270,10 +270,8 @@ internal static void ConfigurePerTenantReqs<TOptions>(IServiceCollection service
270270

271271
MultiTenantOptionsManager<TOptions> BuildOptionsManager(IServiceProvider sp)
272272
{
273-
var cache = (IOptionsMonitorCache<TOptions>)ActivatorUtilities.CreateInstance(sp,
274-
typeof(MultiTenantOptionsCache<TOptions>));
275-
return (MultiTenantOptionsManager<TOptions>)
276-
ActivatorUtilities.CreateInstance(sp, typeof(MultiTenantOptionsManager<TOptions>), cache);
273+
IOptionsMonitorCache<TOptions> cache = ActivatorUtilities.CreateInstance<MultiTenantOptionsCache<TOptions>>(sp);
274+
return ActivatorUtilities.CreateInstance<MultiTenantOptionsManager<TOptions>>(sp, cache);
277275
}
278276
}
279277
}

‎src/Finbuckle.MultiTenant/MultiTenantBuilder.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ public MultiTenantBuilder<TTenantInfo> WithStore<TStore>(ServiceLifetime lifetim
5050
Func<IServiceProvider, TStore> factory)
5151
where TStore : IMultiTenantStore<TTenantInfo>
5252
{
53-
if (factory == null)
54-
{
55-
throw new ArgumentNullException(nameof(factory));
56-
}
53+
ArgumentNullException.ThrowIfNull(factory);
5754

5855
// Note: can't use TryAddEnumerable here because ServiceDescriptor.Describe with a factory can't set implementation type.
5956
Services.Add(
@@ -82,10 +79,7 @@ public MultiTenantBuilder<TTenantInfo> WithStrategy<TStrategy>(ServiceLifetime l
8279
Func<IServiceProvider, TStrategy> factory)
8380
where TStrategy : IMultiTenantStrategy
8481
{
85-
if (factory == null)
86-
{
87-
throw new ArgumentNullException(nameof(factory));
88-
}
82+
ArgumentNullException.ThrowIfNull(factory);
8983

9084
// Potential for multiple entries per service is intended.
9185
Services.Add(ServiceDescriptor.Describe(typeof(IMultiTenantStrategy), sp => factory(sp), lifetime));

‎src/Finbuckle.MultiTenant/Options/MultiTenantOptionsCache.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ public void ClearAll()
6767
/// <returns>The existing or new options instance.</returns>
6868
public TOptions GetOrAdd(string? name, Func<TOptions> createOptions)
6969
{
70-
if (createOptions == null)
71-
{
72-
throw new ArgumentNullException(nameof(createOptions));
73-
}
70+
ArgumentNullException.ThrowIfNull(createOptions);
7471

7572
name ??= Microsoft.Extensions.Options.Options.DefaultName;
7673
var tenantId = multiTenantContextAccessor.MultiTenantContext?.TenantInfo?.Id ?? "";

‎src/Finbuckle.MultiTenant/Stores/ConfigurationStore/ConfigurationStore.cs

+3-12
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public ConfigurationStore(IConfiguration configuration) : this(configuration, De
3939
/// <exception cref="MultiTenantException"></exception>
4040
public ConfigurationStore(IConfiguration configuration, string sectionName)
4141
{
42-
if (configuration is null)
43-
{
44-
throw new ArgumentNullException(nameof(configuration));
45-
}
42+
ArgumentNullException.ThrowIfNull(configuration);
4643

4744
if (string.IsNullOrEmpty(sectionName))
4845
{
@@ -88,10 +85,7 @@ public Task<bool> TryAddAsync(TTenantInfo tenantInfo)
8885
/// <inheritdoc />
8986
public async Task<TTenantInfo?> TryGetAsync(string id)
9087
{
91-
if (id is null)
92-
{
93-
throw new ArgumentNullException(nameof(id));
94-
}
88+
ArgumentNullException.ThrowIfNull(id);
9589

9690
return await Task.FromResult(tenantMap?.Where(kv => kv.Value.Id == id).SingleOrDefault().Value);
9791
}
@@ -105,10 +99,7 @@ public async Task<IEnumerable<TTenantInfo>> GetAllAsync()
10599
/// <inheritdoc />
106100
public async Task<TTenantInfo?> TryGetByIdentifierAsync(string identifier)
107101
{
108-
if (identifier is null)
109-
{
110-
throw new ArgumentNullException(nameof(identifier));
111-
}
102+
ArgumentNullException.ThrowIfNull(identifier);
112103

113104
if (tenantMap is null)
114105
{

‎src/Finbuckle.MultiTenant/Stores/HttpRemoteStore/HttpRemoteStore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public HttpRemoteStore(HttpRemoteStoreClient<TTenantInfo> client, string endpoin
3232
_client = client ?? throw new ArgumentNullException(nameof(client));
3333
if (!endpointTemplate.Contains(DefaultEndpointTemplateIdentifierToken))
3434
{
35-
if (endpointTemplate.EndsWith("/"))
35+
if (endpointTemplate.EndsWith('/'))
3636
endpointTemplate += DefaultEndpointTemplateIdentifierToken;
3737
else
3838
endpointTemplate += $"/{DefaultEndpointTemplateIdentifierToken}";

‎src/Finbuckle.MultiTenant/Stores/MultiTenantStoreWrapper.cs

+4-16
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public MultiTenantStoreWrapper(IMultiTenantStore<TTenantInfo> store, ILogger log
3535
/// <inheritdoc />
3636
public async Task<TTenantInfo?> TryGetAsync(string id)
3737
{
38-
if (id == null)
39-
{
40-
throw new ArgumentNullException(nameof(id));
41-
}
38+
ArgumentNullException.ThrowIfNull(id);
4239

4340
TTenantInfo? result = null;
4441

@@ -86,10 +83,7 @@ public async Task<IEnumerable<TTenantInfo>> GetAllAsync()
8683
/// <inheritdoc />
8784
public async Task<TTenantInfo?> TryGetByIdentifierAsync(string identifier)
8885
{
89-
if (identifier == null)
90-
{
91-
throw new ArgumentNullException(nameof(identifier));
92-
}
86+
ArgumentNullException.ThrowIfNull(identifier);
9387

9488
TTenantInfo? result = null;
9589

@@ -186,10 +180,7 @@ public async Task<bool> TryAddAsync(TTenantInfo tenantInfo)
186180
/// <inheritdoc />
187181
public async Task<bool> TryRemoveAsync(string identifier)
188182
{
189-
if (identifier == null)
190-
{
191-
throw new ArgumentNullException(nameof(identifier));
192-
}
183+
ArgumentNullException.ThrowIfNull(identifier);
193184

194185
var result = false;
195186

@@ -218,10 +209,7 @@ public async Task<bool> TryRemoveAsync(string identifier)
218209
/// <inheritdoc />
219210
public async Task<bool> TryUpdateAsync(TTenantInfo tenantInfo)
220211
{
221-
if (tenantInfo == null)
222-
{
223-
throw new ArgumentNullException(nameof(tenantInfo));
224-
}
212+
ArgumentNullException.ThrowIfNull(tenantInfo);
225213

226214
if (tenantInfo.Id == null)
227215
{

0 commit comments

Comments
 (0)
Please sign in to comment.