From 2be87c9c976c0cd21b66ea12ae406a559aacaac0 Mon Sep 17 00:00:00 2001 From: Maksym Koshovyi Date: Thu, 17 Mar 2022 00:54:16 +0200 Subject: [PATCH] [Group 3] Enable nullable annotations for `Microsoft.Extensions.Hosting.Abstractions` (#65403) * First pass (src) * Update Microsoft.Extensions.Hosting.Abstractions.cs * Update HostBuilderContext * Values of Properties cannot be null --- .../ref/Microsoft.Extensions.Hosting.Abstractions.cs | 10 +++++----- .../Microsoft.Extensions.Hosting.Abstractions.csproj | 1 + .../src/BackgroundService.cs | 8 ++++---- .../src/HostBuilderContext.cs | 4 ++-- .../src/HostingAbstractionsHostExtensions.cs | 8 ++++---- .../src/IHostBuilder.cs | 4 ++-- .../src/IHostEnvironment.cs | 2 +- .../src/IHostingEnvironment.cs | 2 +- .../Microsoft.Extensions.Hosting.Abstractions.csproj | 1 + 9 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs index a7b1d3f7d73f4..269c4a145289c 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs @@ -17,7 +17,7 @@ namespace Microsoft.Extensions.Hosting public abstract partial class BackgroundService : Microsoft.Extensions.Hosting.IHostedService, System.IDisposable { protected BackgroundService() { } - public virtual System.Threading.Tasks.Task ExecuteTask { get { throw null; } } + public virtual System.Threading.Tasks.Task? ExecuteTask { get { throw null; } } public virtual void Dispose() { } protected abstract System.Threading.Tasks.Task ExecuteAsync(System.Threading.CancellationToken stoppingToken); public virtual System.Threading.Tasks.Task StartAsync(System.Threading.CancellationToken cancellationToken) { throw null; } @@ -106,8 +106,8 @@ public partial interface IHostBuilder Microsoft.Extensions.Hosting.IHostBuilder ConfigureContainer(System.Action configureDelegate); Microsoft.Extensions.Hosting.IHostBuilder ConfigureHostConfiguration(System.Action configureDelegate); Microsoft.Extensions.Hosting.IHostBuilder ConfigureServices(System.Action configureDelegate); - Microsoft.Extensions.Hosting.IHostBuilder UseServiceProviderFactory(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory factory); - Microsoft.Extensions.Hosting.IHostBuilder UseServiceProviderFactory(System.Func> factory); + Microsoft.Extensions.Hosting.IHostBuilder UseServiceProviderFactory(Microsoft.Extensions.DependencyInjection.IServiceProviderFactory factory) where TContainerBuilder : notnull; + Microsoft.Extensions.Hosting.IHostBuilder UseServiceProviderFactory(System.Func> factory) where TContainerBuilder : notnull; } public partial interface IHostedService { @@ -116,7 +116,7 @@ public partial interface IHostedService } public partial interface IHostEnvironment { - string ApplicationName { get; set; } + string? ApplicationName { get; set; } Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get; set; } string ContentRootPath { get; set; } string EnvironmentName { get; set; } @@ -124,7 +124,7 @@ public partial interface IHostEnvironment [System.ObsoleteAttribute("IHostingEnvironment has been deprecated. Use Microsoft.Extensions.Hosting.IHostEnvironment instead.")] public partial interface IHostingEnvironment { - string ApplicationName { get; set; } + string? ApplicationName { get; set; } Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get; set; } string ContentRootPath { get; set; } string EnvironmentName { get; set; } diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.csproj b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.csproj index 9658bb63e1ad0..26a0480aa8fca 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.csproj @@ -1,6 +1,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.1;netstandard2.0;$(NetFrameworkMinimum) + enable $(NoWarn);CS0618 diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs index f7802124ea60e..3837a695279d7 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs @@ -12,8 +12,8 @@ namespace Microsoft.Extensions.Hosting /// public abstract class BackgroundService : IHostedService, IDisposable { - private Task _executeTask; - private CancellationTokenSource _stoppingCts; + private Task? _executeTask; + private CancellationTokenSource? _stoppingCts; /// /// Gets the Task that executes the background operation. @@ -21,7 +21,7 @@ public abstract class BackgroundService : IHostedService, IDisposable /// /// Will return if the background operation hasn't started. /// - public virtual Task ExecuteTask => _executeTask; + public virtual Task? ExecuteTask => _executeTask; /// /// This method is called when the starts. The implementation should return a task that represents @@ -69,7 +69,7 @@ public virtual async Task StopAsync(CancellationToken cancellationToken) try { // Signal cancellation to the executing method - _stoppingCts.Cancel(); + _stoppingCts!.Cancel(); } finally { diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostBuilderContext.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostBuilderContext.cs index 94ae0c004fd65..6a8d228947c3a 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostBuilderContext.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostBuilderContext.cs @@ -19,12 +19,12 @@ public HostBuilderContext(IDictionary properties!!) /// /// The initialized by the . /// - public IHostEnvironment HostingEnvironment { get; set; } + public IHostEnvironment HostingEnvironment { get; set; } = null!; /// /// The containing the merged configuration of the application and the . /// - public IConfiguration Configuration { get; set; } + public IConfiguration Configuration { get; set; } = null!; /// /// A central location for sharing state between components during the host building process. diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs index bc4e4be407b6c..81484d28223ad 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs @@ -86,18 +86,18 @@ public static async Task RunAsync(this IHost host, CancellationToken token = def /// The that represents the asynchronous operation. public static async Task WaitForShutdownAsync(this IHost host, CancellationToken token = default) { - IHostApplicationLifetime applicationLifetime = host.Services.GetService(); + IHostApplicationLifetime applicationLifetime = host.Services.GetRequiredService(); token.Register(state => { - ((IHostApplicationLifetime)state).StopApplication(); + ((IHostApplicationLifetime)state!).StopApplication(); }, applicationLifetime); - var waitForStop = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var waitForStop = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); applicationLifetime.ApplicationStopping.Register(obj => { - var tcs = (TaskCompletionSource)obj; + var tcs = (TaskCompletionSource)obj!; tcs.TrySetResult(null); }, waitForStop); diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostBuilder.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostBuilder.cs index 09bb738f46766..7c08dd33014c1 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostBuilder.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostBuilder.cs @@ -51,14 +51,14 @@ public interface IHostBuilder /// The type of builder. /// The factory to register. /// The same instance of the for chaining. - IHostBuilder UseServiceProviderFactory(IServiceProviderFactory factory); + IHostBuilder UseServiceProviderFactory(IServiceProviderFactory factory) where TContainerBuilder : notnull; /// /// Overrides the factory used to create the service provider. /// /// The type of builder. /// The same instance of the for chaining. - IHostBuilder UseServiceProviderFactory(Func> factory); + IHostBuilder UseServiceProviderFactory(Func> factory) where TContainerBuilder : notnull; /// /// Enables configuring the instantiated dependency container. This can be called multiple times and diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs index 6342dc6616597..90c1ae296198d 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs @@ -21,7 +21,7 @@ public interface IHostEnvironment /// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing /// the application entry point. /// - string ApplicationName { get; set; } + string? ApplicationName { get; set; } /// /// Gets or sets the absolute path to the directory that contains the application content files. diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs index af94ab7c1b59c..abba619c76d2e 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs @@ -26,7 +26,7 @@ public interface IHostingEnvironment /// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing /// the application entry point. /// - string ApplicationName { get; set; } + string? ApplicationName { get; set; } /// /// Gets or sets the absolute path to the directory that contains the application content files. diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/Microsoft.Extensions.Hosting.Abstractions.csproj b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/Microsoft.Extensions.Hosting.Abstractions.csproj index cec24fe692941..32a553ddab274 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/Microsoft.Extensions.Hosting.Abstractions.csproj +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/Microsoft.Extensions.Hosting.Abstractions.csproj @@ -2,6 +2,7 @@ $(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.1;netstandard2.0;$(NetFrameworkMinimum) + enable Microsoft.Extensions.Hosting true