-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Group 3] Enable nullable annotations for Microsoft.Extensions.Hosting.Abstractions
#65403
[Group 3] Enable nullable annotations for Microsoft.Extensions.Hosting.Abstractions
#65403
Conversation
Note regarding the This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change. |
Tagging subscribers to this area: @dotnet/area-extensions-hosting Issue DetailsRelated to #43605 This one is a bit tricky. A lot of abstractions, and hard to know what's null, and what isn't. Questions:
|
src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/BackgroundService.cs
Show resolved
Hide resolved
{ | ||
Properties = properties; | ||
} | ||
|
||
/// <summary> | ||
/// The <see cref="IHostEnvironment" /> initialized by the <see cref="IHost" />. | ||
/// </summary> | ||
public IHostEnvironment HostingEnvironment { get; set; } | ||
public IHostEnvironment? HostingEnvironment { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two properties are always set (when we create the object):
runtime/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs
Lines 213 to 217 in 385fc88
_hostBuilderContext = new HostBuilderContext(Properties) | |
{ | |
HostingEnvironment = _hostingEnvironment, | |
Configuration = _hostConfiguration | |
}; |
It feels wrong to have consumers need to check these for null, when they won't be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PR.
Hm, why they are not part of constructor then?
...libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
Show resolved
Hide resolved
Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get; set; } | ||
string ContentRootPath { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will probably have problems with these two in the Hosting
PR, but we can deal with it then. I think the intention here is that these two properties are not null
.
...libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
Show resolved
Hide resolved
...s/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs
Outdated
Show resolved
Hide resolved
@eerhardt Hi, I'm in Ukraine (currently relatively safe in Lviv), so cannot be as active. I'll try to address the comments when I can, but if I don't respond for tool long or disappear altogether, feel free to commit to my branches. |
Ok - no problem at all, @maxkoshevoi. I'm glad to hear you are safe, as that is the most important thing. We can definitely take over the PR if you don't have time. Thanks again for all your contributions. |
For now, I do have time. It's just a heads up that I will be slower to respond, and may not be able to respond for some periods of time. Libraries that left are not that big, so it should be fine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks for the work here @maxkoshevoi!
@@ -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. | |||
/// </summary> | |||
string ApplicationName { get; set; } | |||
string? ApplicationName { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eerhardt This one seems odd to be nullable, similar to EnvironmentName above, this is set automatically by the host
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the current code, this annotation is correct as there are scenarios where it can be null:
runtime/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs
Lines 202 to 213 in 05cb7f5
var hostingEnvironment = new HostingEnvironment() | |
{ | |
ApplicationName = hostConfiguration[HostDefaults.ApplicationKey], | |
EnvironmentName = hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production, | |
ContentRootPath = ResolveContentRootPath(hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory), | |
}; | |
if (string.IsNullOrEmpty(hostingEnvironment.ApplicationName)) | |
{ | |
// Note GetEntryAssembly returns null for the net4x console test runner. | |
hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name; | |
} |
…ng.Abstractions` (dotnet#65403) * First pass (src) * Update Microsoft.Extensions.Hosting.Abstractions.cs * Update HostBuilderContext * Values of Properties cannot be null
Related to #43605
This one is a bit tricky. A lot of abstractions, and hard to know what's null, and what isn't.
Questions:
GetRequiredService
, sincenull
is not expected here. Yes, I know it's a behavioral change, but it will NRE anyway, this just provides a more crear exceptionruntime/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostingAbstractionsHostExtensions.cs
Line 89 in 6b3ec8e
runtime/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/HostBuilderContext.cs
Line 32 in 6b3ec8e