Skip to content

Commit 4963b76

Browse files
authored
Add throw helpers to more multi-targeted projects (dotnet#45984)
1 parent 3056186 commit 4963b76

File tree

89 files changed

+675
-1950
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+675
-1950
lines changed

Directory.Build.targets

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@
2828
<!-- Ignore API doc requirements for test assets -->
2929
<NoWarn Condition="'$(IsTestAssetProject)' == 'true' or '$(IsSampleProject)' == 'true' or '$(IsBenchmarkProject)' == 'true' or
3030
'$(IsMicrobenchmarksProject)' == 'true'">$(NoWarn);CS1591</NoWarn>
31-
32-
<!-- Ignore analyzers that recommend APIs introduced in .NET Core when targeting frameworks that lack those APIs
33-
to avoid issues with multitargeting.
34-
Some projects only build DefaultNetCoreTargetFramework when DotNetBuildFromSource is true so also suppress for source build.
35-
-->
36-
<NoWarn Condition="$(TargetFrameworks.Contains('NetFramework')) or $(TargetFrameworks.Contains('netstandard')) or '$(DotNetBuildFromSource)' == 'true'">$(NoWarn);CA1510;CA1511;CA1512;CA1513</NoWarn>
3731
</PropertyGroup>
3832

3933
<PropertyGroup Label="Resx settings">

src/Caching/SqlServer/src/Microsoft.Extensions.Caching.SqlServer.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@
2121
<Reference Include="Microsoft.Data.SqlClient" />
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
26+
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
27+
</ItemGroup>
28+
2429
</Project>

src/Caching/SqlServer/src/SqlServerCache.cs

Lines changed: 13 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Shared;
78
using Microsoft.Extensions.Caching.Distributed;
89
using Microsoft.Extensions.Internal;
910
using Microsoft.Extensions.Options;
@@ -80,10 +81,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
8081
/// <inheritdoc />
8182
public byte[]? Get(string key)
8283
{
83-
if (key == null)
84-
{
85-
throw new ArgumentNullException(nameof(key));
86-
}
84+
ArgumentNullThrowHelper.ThrowIfNull(key);
8785

8886
var value = _dbOperations.GetCacheItem(key);
8987

@@ -95,10 +93,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
9593
/// <inheritdoc />
9694
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken))
9795
{
98-
if (key == null)
99-
{
100-
throw new ArgumentNullException(nameof(key));
101-
}
96+
ArgumentNullThrowHelper.ThrowIfNull(key);
10297

10398
token.ThrowIfCancellationRequested();
10499

@@ -112,10 +107,7 @@ public SqlServerCache(IOptions<SqlServerCacheOptions> options)
112107
/// <inheritdoc />
113108
public void Refresh(string key)
114109
{
115-
if (key == null)
116-
{
117-
throw new ArgumentNullException(nameof(key));
118-
}
110+
ArgumentNullThrowHelper.ThrowIfNull(key);
119111

120112
_dbOperations.RefreshCacheItem(key);
121113

@@ -125,10 +117,7 @@ public void Refresh(string key)
125117
/// <inheritdoc />
126118
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
127119
{
128-
if (key == null)
129-
{
130-
throw new ArgumentNullException(nameof(key));
131-
}
120+
ArgumentNullThrowHelper.ThrowIfNull(key);
132121

133122
token.ThrowIfCancellationRequested();
134123

@@ -140,10 +129,7 @@ public void Refresh(string key)
140129
/// <inheritdoc />
141130
public void Remove(string key)
142131
{
143-
if (key == null)
144-
{
145-
throw new ArgumentNullException(nameof(key));
146-
}
132+
ArgumentNullThrowHelper.ThrowIfNull(key);
147133

148134
_dbOperations.DeleteCacheItem(key);
149135

@@ -153,10 +139,7 @@ public void Remove(string key)
153139
/// <inheritdoc />
154140
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
155141
{
156-
if (key == null)
157-
{
158-
throw new ArgumentNullException(nameof(key));
159-
}
142+
ArgumentNullThrowHelper.ThrowIfNull(key);
160143

161144
token.ThrowIfCancellationRequested();
162145

@@ -168,20 +151,9 @@ public void Remove(string key)
168151
/// <inheritdoc />
169152
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
170153
{
171-
if (key == null)
172-
{
173-
throw new ArgumentNullException(nameof(key));
174-
}
175-
176-
if (value == null)
177-
{
178-
throw new ArgumentNullException(nameof(value));
179-
}
180-
181-
if (options == null)
182-
{
183-
throw new ArgumentNullException(nameof(options));
184-
}
154+
ArgumentNullThrowHelper.ThrowIfNull(key);
155+
ArgumentNullThrowHelper.ThrowIfNull(value);
156+
ArgumentNullThrowHelper.ThrowIfNull(options);
185157

186158
GetOptions(ref options);
187159

@@ -197,20 +169,9 @@ public async Task SetAsync(
197169
DistributedCacheEntryOptions options,
198170
CancellationToken token = default(CancellationToken))
199171
{
200-
if (key == null)
201-
{
202-
throw new ArgumentNullException(nameof(key));
203-
}
204-
205-
if (value == null)
206-
{
207-
throw new ArgumentNullException(nameof(value));
208-
}
209-
210-
if (options == null)
211-
{
212-
throw new ArgumentNullException(nameof(options));
213-
}
172+
ArgumentNullThrowHelper.ThrowIfNull(key);
173+
ArgumentNullThrowHelper.ThrowIfNull(value);
174+
ArgumentNullThrowHelper.ThrowIfNull(options);
214175

215176
token.ThrowIfCancellationRequested();
216177

src/Caching/SqlServer/src/SqlServerCachingServicesExtensions.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using Microsoft.AspNetCore.Shared;
56
using Microsoft.Extensions.Caching.Distributed;
67
using Microsoft.Extensions.Caching.SqlServer;
78

@@ -20,15 +21,8 @@ public static class SqlServerCachingServicesExtensions
2021
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
2122
public static IServiceCollection AddDistributedSqlServerCache(this IServiceCollection services, Action<SqlServerCacheOptions> setupAction)
2223
{
23-
if (services == null)
24-
{
25-
throw new ArgumentNullException(nameof(services));
26-
}
27-
28-
if (setupAction == null)
29-
{
30-
throw new ArgumentNullException(nameof(setupAction));
31-
}
24+
ArgumentNullThrowHelper.ThrowIfNull(services);
25+
ArgumentNullThrowHelper.ThrowIfNull(setupAction);
3226

3327
services.AddOptions();
3428
AddSqlServerCacheServices(services);

src/Caching/StackExchangeRedis/src/Microsoft.Extensions.Caching.StackExchangeRedis.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@
1717
<Reference Include="StackExchange.Redis" />
1818
</ItemGroup>
1919

20+
<ItemGroup>
21+
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ArgumentNullThrowHelper.cs" LinkBase="Shared" />
22+
<Compile Include="$(SharedSourceRoot)ThrowHelpers\ObjectDisposedThrowHelper.cs" LinkBase="Shared" />
23+
<Compile Include="$(SharedSourceRoot)CallerArgument\CallerArgumentExpressionAttribute.cs" LinkBase="Shared" />
24+
</ItemGroup>
25+
2026
</Project>

src/Caching/StackExchangeRedis/src/RedisCache.cs

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics.CodeAnalysis;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using Microsoft.AspNetCore.Shared;
910
using Microsoft.Extensions.Caching.Distributed;
1011
using Microsoft.Extensions.Logging;
1112
using Microsoft.Extensions.Options;
@@ -79,15 +80,8 @@ public RedisCache(IOptions<RedisCacheOptions> optionsAccessor)
7980
/// <param name="logger">The logger.</param>
8081
internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
8182
{
82-
if (optionsAccessor == null)
83-
{
84-
throw new ArgumentNullException(nameof(optionsAccessor));
85-
}
86-
87-
if (logger == null)
88-
{
89-
throw new ArgumentNullException(nameof(logger));
90-
}
83+
ArgumentNullThrowHelper.ThrowIfNull(optionsAccessor);
84+
ArgumentNullThrowHelper.ThrowIfNull(logger);
9185

9286
_options = optionsAccessor.Value;
9387
_logger = logger;
@@ -99,21 +93,15 @@ internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
9993
/// <inheritdoc />
10094
public byte[]? Get(string key)
10195
{
102-
if (key == null)
103-
{
104-
throw new ArgumentNullException(nameof(key));
105-
}
96+
ArgumentNullThrowHelper.ThrowIfNull(key);
10697

10798
return GetAndRefresh(key, getData: true);
10899
}
109100

110101
/// <inheritdoc />
111102
public async Task<byte[]?> GetAsync(string key, CancellationToken token = default(CancellationToken))
112103
{
113-
if (key == null)
114-
{
115-
throw new ArgumentNullException(nameof(key));
116-
}
104+
ArgumentNullThrowHelper.ThrowIfNull(key);
117105

118106
token.ThrowIfCancellationRequested();
119107

@@ -123,20 +111,9 @@ internal RedisCache(IOptions<RedisCacheOptions> optionsAccessor, ILogger logger)
123111
/// <inheritdoc />
124112
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
125113
{
126-
if (key == null)
127-
{
128-
throw new ArgumentNullException(nameof(key));
129-
}
130-
131-
if (value == null)
132-
{
133-
throw new ArgumentNullException(nameof(value));
134-
}
135-
136-
if (options == null)
137-
{
138-
throw new ArgumentNullException(nameof(options));
139-
}
114+
ArgumentNullThrowHelper.ThrowIfNull(key);
115+
ArgumentNullThrowHelper.ThrowIfNull(value);
116+
ArgumentNullThrowHelper.ThrowIfNull(options);
140117

141118
Connect();
142119

@@ -157,20 +134,9 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
157134
/// <inheritdoc />
158135
public async Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default(CancellationToken))
159136
{
160-
if (key == null)
161-
{
162-
throw new ArgumentNullException(nameof(key));
163-
}
164-
165-
if (value == null)
166-
{
167-
throw new ArgumentNullException(nameof(value));
168-
}
169-
170-
if (options == null)
171-
{
172-
throw new ArgumentNullException(nameof(options));
173-
}
137+
ArgumentNullThrowHelper.ThrowIfNull(key);
138+
ArgumentNullThrowHelper.ThrowIfNull(value);
139+
ArgumentNullThrowHelper.ThrowIfNull(options);
174140

175141
token.ThrowIfCancellationRequested();
176142

@@ -194,21 +160,15 @@ public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
194160
/// <inheritdoc />
195161
public void Refresh(string key)
196162
{
197-
if (key == null)
198-
{
199-
throw new ArgumentNullException(nameof(key));
200-
}
163+
ArgumentNullThrowHelper.ThrowIfNull(key);
201164

202165
GetAndRefresh(key, getData: false);
203166
}
204167

205168
/// <inheritdoc />
206169
public async Task RefreshAsync(string key, CancellationToken token = default(CancellationToken))
207170
{
208-
if (key == null)
209-
{
210-
throw new ArgumentNullException(nameof(key));
211-
}
171+
ArgumentNullThrowHelper.ThrowIfNull(key);
212172

213173
token.ThrowIfCancellationRequested();
214174

@@ -343,10 +303,7 @@ private void TryRegisterProfiler()
343303

344304
private byte[]? GetAndRefresh(string key, bool getData)
345305
{
346-
if (key == null)
347-
{
348-
throw new ArgumentNullException(nameof(key));
349-
}
306+
ArgumentNullThrowHelper.ThrowIfNull(key);
350307

351308
Connect();
352309

@@ -379,10 +336,7 @@ private void TryRegisterProfiler()
379336

380337
private async Task<byte[]?> GetAndRefreshAsync(string key, bool getData, CancellationToken token = default(CancellationToken))
381338
{
382-
if (key == null)
383-
{
384-
throw new ArgumentNullException(nameof(key));
385-
}
339+
ArgumentNullThrowHelper.ThrowIfNull(key);
386340

387341
token.ThrowIfCancellationRequested();
388342

@@ -419,10 +373,7 @@ private void TryRegisterProfiler()
419373
/// <inheritdoc />
420374
public void Remove(string key)
421375
{
422-
if (key == null)
423-
{
424-
throw new ArgumentNullException(nameof(key));
425-
}
376+
ArgumentNullThrowHelper.ThrowIfNull(key);
426377

427378
Connect();
428379

@@ -433,10 +384,7 @@ public void Remove(string key)
433384
/// <inheritdoc />
434385
public async Task RemoveAsync(string key, CancellationToken token = default(CancellationToken))
435386
{
436-
if (key == null)
437-
{
438-
throw new ArgumentNullException(nameof(key));
439-
}
387+
ArgumentNullThrowHelper.ThrowIfNull(key);
440388

441389
await ConnectAsync(token).ConfigureAwait(false);
442390
Debug.Assert(_cache is not null);
@@ -463,10 +411,7 @@ private static void MapMetadata(RedisValue[] results, out DateTimeOffset? absolu
463411

464412
private void Refresh(IDatabase cache, string key, DateTimeOffset? absExpr, TimeSpan? sldExpr)
465413
{
466-
if (key == null)
467-
{
468-
throw new ArgumentNullException(nameof(key));
469-
}
414+
ArgumentNullThrowHelper.ThrowIfNull(key);
470415

471416
// Note Refresh has no effect if there is just an absolute expiration (or neither).
472417
if (sldExpr.HasValue)
@@ -488,10 +433,7 @@ private void Refresh(IDatabase cache, string key, DateTimeOffset? absExpr, TimeS
488433

489434
private async Task RefreshAsync(IDatabase cache, string key, DateTimeOffset? absExpr, TimeSpan? sldExpr, CancellationToken token = default(CancellationToken))
490435
{
491-
if (key == null)
492-
{
493-
throw new ArgumentNullException(nameof(key));
494-
}
436+
ArgumentNullThrowHelper.ThrowIfNull(key);
495437

496438
token.ThrowIfCancellationRequested();
497439

@@ -564,9 +506,6 @@ public void Dispose()
564506

565507
private void CheckDisposed()
566508
{
567-
if (_disposed)
568-
{
569-
throw new ObjectDisposedException(this.GetType().FullName);
570-
}
509+
ObjectDisposedThrowHelper.ThrowIf(_disposed, this);
571510
}
572511
}

0 commit comments

Comments
 (0)