Skip to content

Commit bb96535

Browse files
authored
Add nullable annotations to M.A.Hosting and DefaultBuilder (#24571)
* Turn on nullability annotations for public types in M.A.Hosting * Turn on nullability annotations for Microsoft.AspNetCore and Microsoft.AspNetCore.Server.Abstractions Contributes to #5680
1 parent e2d2aaa commit bb96535

19 files changed

+62
-35
lines changed

src/DefaultBuilder/src/GenericHostBuilderExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static class GenericHostBuilderExtensions
2525
/// <returns>The <see cref="IHostBuilder"/> for chaining.</returns>
2626
public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder, Action<IWebHostBuilder> configure)
2727
{
28+
if (configure is null)
29+
{
30+
throw new ArgumentNullException(nameof(configure));
31+
}
32+
2833
return builder.ConfigureWebHost(webHostBuilder =>
2934
{
3035
WebHost.ConfigureWebDefaults(webHostBuilder);

src/DefaultBuilder/src/Microsoft.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<Description>Microsoft.AspNetCore</Description>
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<IsPackable>false</IsPackable>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<ItemGroup>

src/DefaultBuilder/src/WebHost.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public static IWebHost Start(RequestDelegate app) =>
4040
/// <param name="url">The URL the hosted application will listen on.</param>
4141
/// <param name="app">A delegate that handles requests to the application.</param>
4242
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
43-
public static IWebHost Start(string url, RequestDelegate app)
43+
public static IWebHost Start(string? url, RequestDelegate app)
4444
{
45-
var startupAssemblyName = app.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
45+
var startupAssemblyName = app.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
4646
return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName);
4747
}
4848

@@ -62,9 +62,9 @@ public static IWebHost Start(Action<IRouteBuilder> routeBuilder) =>
6262
/// <param name="url">The URL the hosted application will listen on.</param>
6363
/// <param name="routeBuilder">A delegate that configures the router for handling requests to the application.</param>
6464
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
65-
public static IWebHost Start(string url, Action<IRouteBuilder> routeBuilder)
65+
public static IWebHost Start(string? url, Action<IRouteBuilder> routeBuilder)
6666
{
67-
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
67+
var startupAssemblyName = routeBuilder.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
6868
return StartWith(url, services => services.AddRouting(), appBuilder => appBuilder.UseRouter(routeBuilder), applicationName: startupAssemblyName);
6969
}
7070

@@ -84,10 +84,10 @@ public static IWebHost StartWith(Action<IApplicationBuilder> app) =>
8484
/// <param name="url">The URL the hosted application will listen on.</param>
8585
/// <param name="app">The delegate that configures the <see cref="IApplicationBuilder"/>.</param>
8686
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
87-
public static IWebHost StartWith(string url, Action<IApplicationBuilder> app) =>
87+
public static IWebHost StartWith(string? url, Action<IApplicationBuilder> app) =>
8888
StartWith(url: url, configureServices: null, app: app, applicationName: null);
8989

90-
private static IWebHost StartWith(string url, Action<IServiceCollection> configureServices, Action<IApplicationBuilder> app, string applicationName)
90+
private static IWebHost StartWith(string? url, Action<IServiceCollection>? configureServices, Action<IApplicationBuilder> app, string? applicationName)
9191
{
9292
var builder = CreateDefaultBuilder();
9393

@@ -153,7 +153,7 @@ public static IWebHostBuilder CreateDefaultBuilder() =>
153153
/// </remarks>
154154
/// <param name="args">The command line args.</param>
155155
/// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
156-
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
156+
public static IWebHostBuilder CreateDefaultBuilder(string[]? args)
157157
{
158158
var builder = new WebHostBuilder();
159159

src/Hosting/Hosting/src/Builder/ApplicationBuilderFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using Microsoft.AspNetCore.Builder;
68
using Microsoft.AspNetCore.Http.Features;

src/Hosting/Hosting/src/Http/DefaultHttpContextFactory.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Diagnostics;
68
using System.Runtime.CompilerServices;
@@ -12,7 +14,7 @@ namespace Microsoft.AspNetCore.Http
1214
{
1315
public class DefaultHttpContextFactory : IHttpContextFactory
1416
{
15-
private readonly IHttpContextAccessor _httpContextAccessor;
17+
private readonly IHttpContextAccessor? _httpContextAccessor;
1618
private readonly FormOptions _formOptions;
1719
private readonly IServiceScopeFactory _serviceScopeFactory;
1820

@@ -26,7 +28,7 @@ public DefaultHttpContextFactory(IServiceProvider serviceProvider)
2628
_serviceScopeFactory = serviceProvider.GetRequiredService<IServiceScopeFactory>();
2729
}
2830

29-
internal IHttpContextAccessor HttpContextAccessor => _httpContextAccessor;
31+
internal IHttpContextAccessor? HttpContextAccessor => _httpContextAccessor;
3032

3133
public HttpContext Create(IFeatureCollection featureCollection)
3234
{

src/Hosting/Hosting/src/Microsoft.AspNetCore.Hosting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<GenerateDocumentationFile>true</GenerateDocumentationFile>
99
<PackageTags>aspnetcore;hosting</PackageTags>
1010
<IsPackable>false</IsPackable>
11+
<LangVersion>9.0</LangVersion>
1112
</PropertyGroup>
1213

1314
<ItemGroup>

src/Hosting/Hosting/src/Server/Features/ServerAddressesFeature.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System.Collections.Generic;
57

68
namespace Microsoft.AspNetCore.Hosting.Server.Features

src/Hosting/Hosting/src/Startup/DelegateStartup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using Microsoft.AspNetCore.Builder;
68
using Microsoft.Extensions.DependencyInjection;
@@ -18,4 +20,4 @@ public DelegateStartup(IServiceProviderFactory<IServiceCollection> factory, Acti
1820

1921
public override void Configure(IApplicationBuilder app) => _configureApp(app);
2022
}
21-
}
23+
}

src/Hosting/Hosting/src/StaticWebAssets/StaticWebAssetsLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
#nullable enable
5-
using System.Collections.Generic;
5+
66
using System.IO;
77
using System.Linq;
88
using System.Reflection;

src/Hosting/Hosting/src/WebHostBuilder.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
#nullable enable
5+
46
using System;
57
using System.Collections.Generic;
68
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
710
using System.IO;
811
using System.Linq;
912
using System.Reflection;
@@ -24,13 +27,13 @@ namespace Microsoft.AspNetCore.Hosting
2427
public class WebHostBuilder : IWebHostBuilder
2528
{
2629
private readonly HostingEnvironment _hostingEnvironment;
27-
private Action<WebHostBuilderContext, IServiceCollection> _configureServices;
30+
private readonly IConfiguration _config;
31+
private readonly WebHostBuilderContext _context;
2832

29-
private IConfiguration _config;
30-
private WebHostOptions _options;
31-
private WebHostBuilderContext _context;
33+
private WebHostOptions? _options;
3234
private bool _webHostBuilt;
33-
private Action<WebHostBuilderContext, IConfigurationBuilder> _configureAppConfigurationBuilder;
35+
private Action<WebHostBuilderContext, IServiceCollection>? _configureServices;
36+
private Action<WebHostBuilderContext, IConfigurationBuilder>? _configureAppConfigurationBuilder;
3437

3538
/// <summary>
3639
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
@@ -78,7 +81,7 @@ public string GetSetting(string key)
7881
/// <param name="key">The key of the setting to add or replace.</param>
7982
/// <param name="value">The value of the setting to add or replace.</param>
8083
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
81-
public IWebHostBuilder UseSetting(string key, string value)
84+
public IWebHostBuilder UseSetting(string key, string? value)
8285
{
8386
_config[key] = value;
8487
return this;
@@ -212,7 +215,8 @@ IServiceProvider GetProviderFromFactory(IServiceCollection collection)
212215
}
213216
}
214217

215-
private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
218+
[MemberNotNull(nameof(_options))]
219+
private IServiceCollection BuildCommonServices(out AggregateException? hostingStartupErrors)
216220
{
217221
hostingStartupErrors = null;
218222

@@ -231,7 +235,7 @@ private IServiceCollection BuildCommonServices(out AggregateException hostingSta
231235

232236
foreach (var attribute in assembly.GetCustomAttributes<HostingStartupAttribute>())
233237
{
234-
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType);
238+
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType)!;
235239
hostingStartup.Configure(this);
236240
}
237241
}
@@ -330,8 +334,8 @@ private void AddApplicationServices(IServiceCollection services, IServiceProvide
330334
// NOTE: This code overrides original services lifetime. Instances would always be singleton in
331335
// application container.
332336
var listener = hostingServiceProvider.GetService<DiagnosticListener>();
333-
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener));
334-
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener));
337+
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener!));
338+
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener!));
335339
}
336340

337341
private string ResolveContentRootPath(string contentRootPath, string basePath)

0 commit comments

Comments
 (0)