Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/DefaultBuilder/src/GenericHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public static class GenericHostBuilderExtensions
/// <returns>The <see cref="IHostBuilder"/> for chaining.</returns>
public static IHostBuilder ConfigureWebHostDefaults(this IHostBuilder builder, Action<IWebHostBuilder> configure)
{
if (configure is null)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I bother with this? This would have null-ref (so would have using a null IHostBuilder).

{
throw new ArgumentNullException(nameof(configure));
}

return builder.ConfigureWebHost(webHostBuilder =>
{
WebHost.ConfigureWebDefaults(webHostBuilder);
Expand Down
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/Microsoft.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<Description>Microsoft.AspNetCore</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions src/DefaultBuilder/src/WebHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public static IWebHost Start(RequestDelegate app) =>
/// <param name="url">The URL the hosted application will listen on.</param>
/// <param name="app">A delegate that handles requests to the application.</param>
/// <returns>A started <see cref="IWebHost"/> that hosts the application.</returns>
public static IWebHost Start(string url, RequestDelegate app)
public static IWebHost Start(string? url, RequestDelegate app)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These shouldn't have been made nullable. I know you did that for chaining, but it would be better to override the warning internally.

{
var startupAssemblyName = app.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = app.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;
return StartWith(url: url, configureServices: null, app: appBuilder => appBuilder.Run(app), applicationName: startupAssemblyName);
}

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

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

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

Expand Down Expand Up @@ -153,7 +153,7 @@ public static IWebHostBuilder CreateDefaultBuilder() =>
/// </remarks>
/// <param name="args">The command line args.</param>
/// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
public static IWebHostBuilder CreateDefaultBuilder(string[]? args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also only nullable for chaining. Suppress the warning in the caller instead.

{
var builder = new WebHostBuilder();

Expand Down
2 changes: 2 additions & 0 deletions src/Hosting/Hosting/src/Builder/ApplicationBuilderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we no longer have the ref project, we can turn on nullability on individual files. Convenient!


using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Features;
Expand Down
6 changes: 4 additions & 2 deletions src/Hosting/Hosting/src/Http/DefaultHttpContextFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand All @@ -12,7 +14,7 @@ namespace Microsoft.AspNetCore.Http
{
public class DefaultHttpContextFactory : IHttpContextFactory
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IHttpContextAccessor? _httpContextAccessor;
private readonly FormOptions _formOptions;
private readonly IServiceScopeFactory _serviceScopeFactory;

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

internal IHttpContextAccessor HttpContextAccessor => _httpContextAccessor;
internal IHttpContextAccessor? HttpContextAccessor => _httpContextAccessor;

public HttpContext Create(IFeatureCollection featureCollection)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;hosting</PackageTags>
<IsPackable>false</IsPackable>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System.Collections.Generic;

namespace Microsoft.AspNetCore.Hosting.Server.Features
Expand Down
4 changes: 3 additions & 1 deletion src/Hosting/Hosting/src/Startup/DelegateStartup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -18,4 +20,4 @@ public DelegateStartup(IServiceProviderFactory<IServiceCollection> factory, Acti

public override void Configure(IApplicationBuilder app) => _configureApp(app);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable
using System.Collections.Generic;

using System.IO;
using System.Linq;
using System.Reflection;
Expand Down
24 changes: 14 additions & 10 deletions src/Hosting/Hosting/src/WebHostBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -24,13 +27,13 @@ namespace Microsoft.AspNetCore.Hosting
public class WebHostBuilder : IWebHostBuilder
{
private readonly HostingEnvironment _hostingEnvironment;
private Action<WebHostBuilderContext, IServiceCollection> _configureServices;
private readonly IConfiguration _config;
private readonly WebHostBuilderContext _context;

private IConfiguration _config;
private WebHostOptions _options;
private WebHostBuilderContext _context;
private WebHostOptions? _options;
private bool _webHostBuilt;
private Action<WebHostBuilderContext, IConfigurationBuilder> _configureAppConfigurationBuilder;
private Action<WebHostBuilderContext, IServiceCollection>? _configureServices;
private Action<WebHostBuilderContext, IConfigurationBuilder>? _configureAppConfigurationBuilder;

/// <summary>
/// Initializes a new instance of the <see cref="WebHostBuilder"/> class.
Expand Down Expand Up @@ -78,7 +81,7 @@ public string GetSetting(string key)
/// <param name="key">The key of the setting to add or replace.</param>
/// <param name="value">The value of the setting to add or replace.</param>
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public IWebHostBuilder UseSetting(string key, string value)
public IWebHostBuilder UseSetting(string key, string? value)
{
_config[key] = value;
return this;
Expand Down Expand Up @@ -212,7 +215,8 @@ IServiceProvider GetProviderFromFactory(IServiceCollection collection)
}
}

private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
[MemberNotNull(nameof(_options))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It says that the field is non-null at the end of this method call.

private IServiceCollection BuildCommonServices(out AggregateException? hostingStartupErrors)
{
hostingStartupErrors = null;

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

foreach (var attribute in assembly.GetCustomAttributes<HostingStartupAttribute>())
{
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType);
var hostingStartup = (IHostingStartup)Activator.CreateInstance(attribute.HostingStartupType)!;
hostingStartup.Configure(this);
}
}
Expand Down Expand Up @@ -330,8 +334,8 @@ private void AddApplicationServices(IServiceCollection services, IServiceProvide
// NOTE: This code overrides original services lifetime. Instances would always be singleton in
// application container.
var listener = hostingServiceProvider.GetService<DiagnosticListener>();
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener));
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener));
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticListener), listener!));
services.Replace(ServiceDescriptor.Singleton(typeof(DiagnosticSource), listener!));
}

private string ResolveContentRootPath(string contentRootPath, string basePath)
Expand Down
8 changes: 5 additions & 3 deletions src/Hosting/Hosting/src/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
Expand All @@ -25,7 +27,7 @@ public static class WebHostBuilderExtensions
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<IApplicationBuilder> configureApp)
{
return hostBuilder.Configure((_, app) => configureApp(app), configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name);
return hostBuilder.Configure((_, app) => configureApp(app), configureApp.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name!);
}

/// <summary>
Expand All @@ -36,7 +38,7 @@ public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action
/// <returns>The <see cref="IWebHostBuilder"/>.</returns>
public static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, IApplicationBuilder> configureApp)
{
return hostBuilder.Configure(configureApp, configureApp.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name);
return hostBuilder.Configure(configureApp, configureApp.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name!);
}

private static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Action<WebHostBuilderContext, IApplicationBuilder> configureApp, string startupAssemblyName)
Expand Down Expand Up @@ -77,7 +79,7 @@ private static IWebHostBuilder Configure(this IWebHostBuilder hostBuilder, Actio
throw new ArgumentNullException(nameof(startupFactory));
}

var startupAssemblyName = startupFactory.GetMethodInfo().DeclaringType.GetTypeInfo().Assembly.GetName().Name;
var startupAssemblyName = startupFactory.GetMethodInfo().DeclaringType!.GetTypeInfo().Assembly.GetName().Name;

hostBuilder.UseSetting(WebHostDefaults.ApplicationKey, startupAssemblyName);

Expand Down
14 changes: 8 additions & 6 deletions src/Hosting/Hosting/src/WebHostExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#nullable enable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -103,7 +105,7 @@ public static async Task RunAsync(this IWebHost host, CancellationToken token =
}
}

private static async Task RunAsync(this IWebHost host, CancellationToken token, string startupMessage)
private static async Task RunAsync(this IWebHost host, CancellationToken token, string? startupMessage)
{
try
{
Expand All @@ -114,8 +116,8 @@ private static async Task RunAsync(this IWebHost host, CancellationToken token,

if (!options.SuppressStatusMessages)
{
Console.WriteLine($"Hosting environment: {hostingEnvironment.EnvironmentName}");
Console.WriteLine($"Content root path: {hostingEnvironment.ContentRootPath}");
Console.WriteLine($"Hosting environment: {hostingEnvironment?.EnvironmentName}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can this be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RunAsync does a GetService:

var hostingEnvironment = host.Services.GetService<IHostEnvironment>();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, bug 😄

Console.WriteLine($"Content root path: {hostingEnvironment?.ContentRootPath}");


var serverAddresses = host.ServerFeatures.Get<IServerAddressesFeature>()?.Addresses;
Expand Down Expand Up @@ -150,18 +152,18 @@ private static async Task RunAsync(this IWebHost host, CancellationToken token,

private static async Task WaitForTokenShutdownAsync(this IWebHost host, CancellationToken token)
{
var applicationLifetime = host.Services.GetService<IHostApplicationLifetime>();
var applicationLifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();

token.Register(state =>
{
((IHostApplicationLifetime)state).StopApplication();
((IHostApplicationLifetime)state!).StopApplication();
},
applicationLifetime);

var waitForStop = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
applicationLifetime.ApplicationStopping.Register(obj =>
{
var tcs = (TaskCompletionSource)obj;
var tcs = (TaskCompletionSource)obj!;
tcs.TrySetResult();
}, waitForStop);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Microsoft.AspNetCore.Hosting.Server.Abstractions
/// its <typeparamref name="TContext"/> between requests.
/// </summary>
/// <typeparam name="TContext">The <see cref="IHttpApplication{TContext}"/> Host context</typeparam>
public interface IHostContextContainer<TContext>
public interface IHostContextContainer<TContext> where TContext : notnull
{
TContext HostContext { get; set; }
}
Expand Down
2 changes: 1 addition & 1 deletion src/Hosting/Server.Abstractions/src/IHttpApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Hosting.Server
/// Represents an application.
/// </summary>
/// <typeparam name="TContext">The context associated with the application.</typeparam>
public interface IHttpApplication<TContext>
public interface IHttpApplication<TContext> where TContext : notnull
{
/// <summary>
/// Create a TContext given a collection of HTTP features.
Expand Down
2 changes: 1 addition & 1 deletion src/Hosting/Server.Abstractions/src/IServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface IServer : IDisposable
/// <param name="application">An instance of <see cref="IHttpApplication{TContext}"/>.</param>
/// <typeparam name="TContext">The context associated with the application.</typeparam>
/// <param name="cancellationToken">Indicates if the server startup should be aborted.</param>
Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken);
Task StartAsync<TContext>(IHttpApplication<TContext> application, CancellationToken cancellationToken) where TContext : notnull;

/// <summary>
/// Stop processing requests and shut down the server, gracefully if possible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface IServerIntegratedAuth
/// <summary>
/// The name of the authentication scheme for the server authentication handler.
/// </summary>
string AuthenticationScheme { get; }
string? AuthenticationScheme { get; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be null. Where did you see otherwise?

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore;hosting</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class ServerIntegratedAuth : IServerIntegratedAuth
/// <summary>
/// The name of the authentication scheme for the server authentication handler.
/// </summary>
public string AuthenticationScheme { get; set; }
public string? AuthenticationScheme { get; set; }
}
}
3 changes: 3 additions & 0 deletions src/Hosting/build.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@ECHO OFF
SET RepoRoot=%~dp0..\..
%RepoRoot%\build.cmd -projects %~dp0**\*.*proj %*