Skip to content

Commit 9de8e33

Browse files
Use ArgumentNullException.ThrowIfNull in Servers (#43502)
1 parent 5c4112c commit 9de8e33

26 files changed

+58
-232
lines changed

src/Servers/HttpSys/src/HttpSysListener.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ internal sealed partial class HttpSysListener : IDisposable
4343

4444
public HttpSysListener(HttpSysOptions options, ILoggerFactory loggerFactory)
4545
{
46-
if (options == null)
47-
{
48-
throw new ArgumentNullException(nameof(options));
49-
}
50-
if (loggerFactory == null)
51-
{
52-
throw new ArgumentNullException(nameof(loggerFactory));
53-
}
46+
ArgumentNullException.ThrowIfNull(options);
47+
ArgumentNullException.ThrowIfNull(loggerFactory);
5448

5549
if (!HttpApi.Supported)
5650
{

src/Servers/HttpSys/src/MessagePump.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@ internal sealed partial class MessagePump : IServer, IServerDelegationFeature
3030

3131
public MessagePump(IOptions<HttpSysOptions> options, ILoggerFactory loggerFactory, IAuthenticationSchemeProvider authentication)
3232
{
33-
if (options == null)
34-
{
35-
throw new ArgumentNullException(nameof(options));
36-
}
37-
if (loggerFactory == null)
38-
{
39-
throw new ArgumentNullException(nameof(loggerFactory));
40-
}
33+
ArgumentNullException.ThrowIfNull(options);
34+
ArgumentNullException.ThrowIfNull(loggerFactory);
4135
_options = options.Value;
4236
Listener = new HttpSysListener(_options, loggerFactory);
4337
_logger = loggerFactory.CreateLogger<MessagePump>();
@@ -69,10 +63,7 @@ public MessagePump(IOptions<HttpSysOptions> options, ILoggerFactory loggerFactor
6963

7064
public Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) where TContext : notnull
7165
{
72-
if (application == null)
73-
{
74-
throw new ArgumentNullException(nameof(application));
75-
}
66+
ArgumentNullException.ThrowIfNull(application);
7667

7768
var hostingUrlsPresent = _serverAddresses.Addresses.Count > 0;
7869
var serverAddressCopy = _serverAddresses.Addresses.ToList();

src/Servers/HttpSys/src/RequestProcessing/RequestContext.FeatureCollection.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,7 @@ IHeaderDictionary IHttpResponseFeature.Headers
434434

435435
void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
436436
{
437-
if (callback == null)
438-
{
439-
throw new ArgumentNullException(nameof(callback));
440-
}
437+
ArgumentNullException.ThrowIfNull(callback);
441438
if (_onStartingActions == null)
442439
{
443440
throw new InvalidOperationException("Cannot register new callbacks, the response has already started.");
@@ -448,10 +445,7 @@ void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
448445

449446
void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
450447
{
451-
if (callback == null)
452-
{
453-
throw new ArgumentNullException(nameof(callback));
454-
}
448+
ArgumentNullException.ThrowIfNull(callback);
455449
if (_onCompletedActions == null)
456450
{
457451
throw new InvalidOperationException("Cannot register new callbacks, the response has already completed.");

src/Servers/HttpSys/src/RequestProcessing/RequestContext.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,7 @@ protected void SetFatalResponse(int status)
262262

263263
internal unsafe void Delegate(DelegationRule destination)
264264
{
265-
if (destination == null)
266-
{
267-
throw new ArgumentNullException(nameof(destination));
268-
}
265+
ArgumentNullException.ThrowIfNull(destination);
269266
if (Request.HasRequestBodyStarted)
270267
{
271268
throw new InvalidOperationException("This request cannot be delegated, the request body has already started.");

src/Servers/HttpSys/src/RequestProcessing/RequestStream.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ internal void Abort()
9696

9797
private static void ValidateReadBuffer(byte[] buffer, int offset, int size)
9898
{
99-
if (buffer == null)
100-
{
101-
throw new ArgumentNullException(nameof(buffer));
102-
}
99+
ArgumentNullException.ThrowIfNull(buffer);
103100
if ((uint)offset > (uint)buffer.Length)
104101
{
105102
throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);

src/Servers/HttpSys/src/RequestProcessing/ResponseBody.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,7 @@ public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, As
524524

525525
public override void EndWrite(IAsyncResult asyncResult)
526526
{
527-
if (asyncResult == null)
528-
{
529-
throw new ArgumentNullException(nameof(asyncResult));
530-
}
527+
ArgumentNullException.ThrowIfNull(asyncResult);
531528

532529
TaskToApm.End(asyncResult);
533530
}

src/Servers/IIS/IIS/src/Core/IISHttpContext.FeatureCollection.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,7 @@ private async Task CompleteInitializeResponseAwaited(Task initializeTask)
297297
throw new ArgumentException($"{nameof(variableName)} should be non-empty string");
298298
}
299299

300-
if (value == null)
301-
{
302-
throw new ArgumentNullException(nameof(value));
303-
}
300+
ArgumentNullException.ThrowIfNull(value);
304301

305302
// Synchronize access to native methods that might run in parallel with IO loops
306303
lock (_contextLock)

src/Servers/IIS/IIS/src/WebHostBuilderIISExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public static class WebHostBuilderIISExtensions
2222
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
2323
public static IWebHostBuilder UseIIS(this IWebHostBuilder hostBuilder)
2424
{
25-
if (hostBuilder == null)
26-
{
27-
throw new ArgumentNullException(nameof(hostBuilder));
28-
}
25+
ArgumentNullException.ThrowIfNull(hostBuilder);
2926

3027
// Check if in process
3128
if (OperatingSystem.IsWindows() && NativeMethods.IsAspNetCoreModuleLoaded())

src/Servers/IIS/IIS/test/testassets/shared/WebSockets/HandshakeHelpers.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public static string CreateResponseKey(string requestKey)
2424
// this concatenated value to obtain a 20-byte value and base64-encoding"
2525
// https://tools.ietf.org/html/rfc6455#section-4.2.2
2626

27-
if (requestKey == null)
28-
{
29-
throw new ArgumentNullException(nameof(requestKey));
30-
}
27+
ArgumentNullException.ThrowIfNull(requestKey);
3128

3229
string merged = requestKey + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
3330
byte[] mergedBytes = Encoding.UTF8.GetBytes(merged);

src/Servers/IIS/IISIntegration/src/IISMiddleware.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,10 @@ public IISMiddleware(RequestDelegate next,
7373
IAuthenticationSchemeProvider authentication,
7474
IHostApplicationLifetime applicationLifetime)
7575
{
76-
if (next == null)
77-
{
78-
throw new ArgumentNullException(nameof(next));
79-
}
80-
if (loggerFactory == null)
81-
{
82-
throw new ArgumentNullException(nameof(loggerFactory));
83-
}
84-
if (options == null)
85-
{
86-
throw new ArgumentNullException(nameof(options));
87-
}
88-
if (applicationLifetime == null)
89-
{
90-
throw new ArgumentNullException(nameof(applicationLifetime));
91-
}
76+
ArgumentNullException.ThrowIfNull(next);
77+
ArgumentNullException.ThrowIfNull(loggerFactory);
78+
ArgumentNullException.ThrowIfNull(options);
79+
ArgumentNullException.ThrowIfNull(applicationLifetime);
9280
if (string.IsNullOrEmpty(pairingToken))
9381
{
9482
throw new ArgumentException("Missing or empty pairing token.");

src/Servers/IIS/IISIntegration/src/WebHostBuilderIISExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public static class WebHostBuilderIISExtensions
3030
/// <returns></returns>
3131
public static IWebHostBuilder UseIISIntegration(this IWebHostBuilder hostBuilder)
3232
{
33-
if (hostBuilder == null)
34-
{
35-
throw new ArgumentNullException(nameof(hostBuilder));
36-
}
33+
ArgumentNullException.ThrowIfNull(hostBuilder);
3734

3835
// Check if `UseIISIntegration` was called already
3936
if (hostBuilder.GetSetting(nameof(UseIISIntegration)) != null)

src/Servers/IIS/IntegrationTesting.IIS/src/ApplicationDeployerFactory.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,9 @@ public class IISApplicationDeployerFactory
1919
/// <returns></returns>
2020
public static ApplicationDeployer Create(DeploymentParameters deploymentParameters, ILoggerFactory loggerFactory)
2121
{
22-
if (deploymentParameters == null)
23-
{
24-
throw new ArgumentNullException(nameof(deploymentParameters));
25-
}
22+
ArgumentNullException.ThrowIfNull(deploymentParameters);
2623

27-
if (loggerFactory == null)
28-
{
29-
throw new ArgumentNullException(nameof(loggerFactory));
30-
}
24+
ArgumentNullException.ThrowIfNull(loggerFactory);
3125

3226
switch (deploymentParameters.ServerType)
3327
{

src/Servers/Kestrel/Core/src/Internal/Http/HttpRequestStream.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ private async ValueTask<int> ReadAsyncInternal(Memory<byte> destination, Cancell
154154
/// <inheritdoc />
155155
public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
156156
{
157-
if (destination == null)
158-
{
159-
throw new ArgumentNullException(nameof(destination));
160-
}
157+
ArgumentNullException.ThrowIfNull(destination);
161158

162159
if (bufferSize <= 0)
163160
{

src/Servers/Kestrel/Core/src/Internal/Http2/Http2Stream.FeatureCollection.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ IHeaderDictionary IHttpResponseTrailersFeature.Trailers
3737
}
3838
set
3939
{
40-
if (value == null)
41-
{
42-
throw new ArgumentNullException(nameof(value));
43-
}
40+
ArgumentNullException.ThrowIfNull(value);
4441

4542
_userTrailers = value;
4643
}

src/Servers/Kestrel/Core/src/Internal/Http3/Http3Stream.FeatureCollection.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ IHeaderDictionary IHttpResponseTrailersFeature.Trailers
3232
}
3333
set
3434
{
35-
if (value == null)
36-
{
37-
throw new ArgumentNullException(nameof(value));
38-
}
35+
ArgumentNullException.ThrowIfNull(value);
3936

4037
_userTrailers = value;
4138
}

src/Servers/Kestrel/Core/src/Internal/KestrelServerImpl.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ internal KestrelServerImpl(
7272
IEnumerable<IMultiplexedConnectionListenerFactory>? multiplexedFactories,
7373
ServiceContext serviceContext)
7474
{
75-
if (transportFactories == null)
76-
{
77-
throw new ArgumentNullException(nameof(transportFactories));
78-
}
75+
ArgumentNullException.ThrowIfNull(transportFactories);
7976

8077
_transportFactory = transportFactories.LastOrDefault();
8178
_multiplexedTransportFactory = multiplexedFactories?.LastOrDefault();
@@ -98,14 +95,8 @@ internal KestrelServerImpl(
9895

9996
private static ServiceContext CreateServiceContext(IOptions<KestrelServerOptions> options, ILoggerFactory loggerFactory, DiagnosticSource? diagnosticSource)
10097
{
101-
if (options == null)
102-
{
103-
throw new ArgumentNullException(nameof(options));
104-
}
105-
if (loggerFactory == null)
106-
{
107-
throw new ArgumentNullException(nameof(loggerFactory));
108-
}
98+
ArgumentNullException.ThrowIfNull(options);
99+
ArgumentNullException.ThrowIfNull(loggerFactory);
109100

110101
var serverOptions = options.Value ?? new KestrelServerOptions();
111102
var trace = new KestrelTrace(loggerFactory);

src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ public KestrelConfigurationLoader Endpoint(string name, Action<EndpointConfigura
102102
/// </summary>
103103
public KestrelConfigurationLoader Endpoint(IPAddress address, int port, Action<ListenOptions> configure)
104104
{
105-
if (address == null)
106-
{
107-
throw new ArgumentNullException(nameof(address));
108-
}
105+
ArgumentNullException.ThrowIfNull(address);
109106

110107
return Endpoint(new IPEndPoint(address, port), configure);
111108
}
@@ -120,14 +117,8 @@ public KestrelConfigurationLoader Endpoint(IPAddress address, int port, Action<L
120117
/// </summary>
121118
public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint, Action<ListenOptions> configure)
122119
{
123-
if (endPoint == null)
124-
{
125-
throw new ArgumentNullException(nameof(endPoint));
126-
}
127-
if (configure == null)
128-
{
129-
throw new ArgumentNullException(nameof(configure));
130-
}
120+
ArgumentNullException.ThrowIfNull(endPoint);
121+
ArgumentNullException.ThrowIfNull(configure);
131122

132123
EndpointsToAdd.Add(() =>
133124
{
@@ -149,10 +140,7 @@ public KestrelConfigurationLoader Endpoint(IPEndPoint endPoint, Action<ListenOpt
149140
/// </summary>
150141
public KestrelConfigurationLoader LocalhostEndpoint(int port, Action<ListenOptions> configure)
151142
{
152-
if (configure == null)
153-
{
154-
throw new ArgumentNullException(nameof(configure));
155-
}
143+
ArgumentNullException.ThrowIfNull(configure);
156144

157145
EndpointsToAdd.Add(() =>
158146
{
@@ -172,10 +160,7 @@ public KestrelConfigurationLoader LocalhostEndpoint(int port, Action<ListenOptio
172160
/// </summary>
173161
public KestrelConfigurationLoader AnyIPEndpoint(int port, Action<ListenOptions> configure)
174162
{
175-
if (configure == null)
176-
{
177-
throw new ArgumentNullException(nameof(configure));
178-
}
163+
ArgumentNullException.ThrowIfNull(configure);
179164

180165
EndpointsToAdd.Add(() =>
181166
{
@@ -195,18 +180,12 @@ public KestrelConfigurationLoader AnyIPEndpoint(int port, Action<ListenOptions>
195180
/// </summary>
196181
public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action<ListenOptions> configure)
197182
{
198-
if (socketPath == null)
199-
{
200-
throw new ArgumentNullException(nameof(socketPath));
201-
}
183+
ArgumentNullException.ThrowIfNull(socketPath);
202184
if (socketPath.Length == 0 || socketPath[0] != '/')
203185
{
204186
throw new ArgumentException(CoreStrings.UnixSocketPathMustBeAbsolute, nameof(socketPath));
205187
}
206-
if (configure == null)
207-
{
208-
throw new ArgumentNullException(nameof(configure));
209-
}
188+
ArgumentNullException.ThrowIfNull(configure);
210189

211190
EndpointsToAdd.Add(() =>
212191
{
@@ -226,10 +205,7 @@ public KestrelConfigurationLoader UnixSocketEndpoint(string socketPath, Action<L
226205
/// </summary>
227206
public KestrelConfigurationLoader HandleEndpoint(ulong handle, Action<ListenOptions> configure)
228207
{
229-
if (configure == null)
230-
{
231-
throw new ArgumentNullException(nameof(configure));
232-
}
208+
ArgumentNullException.ThrowIfNull(configure);
233209

234210
EndpointsToAdd.Add(() =>
235211
{

0 commit comments

Comments
 (0)