Skip to content

Conversation

@twsouthwick
Copy link
Member

@twsouthwick twsouthwick commented Aug 16, 2023

This refactors the runtime environment to be configured via a user-available options type. By default, if running on IIS, this will be populated, but can easily be configured if needed.

This also adds HostingEnvironment properties that may be expected via the same options.

As part of this, a hosted service is added that will set the options as a static instance so it will be available for the lifetime of the service (since the APIs on HttpRuntime and HostingEnvironment were static). This hosted service will take care of clearing this value once the host is stopped.

Fixes #368
Blocked by dotnet/aspnetcore#43632 for full support

This refactors the runtime environment to be configured via a user-available options type. By default, if running on IIS, this will be populated, but can easily be configured if needed.

This also adds HostingEnvironment properties that may be expected via the same options.

As part of this, a hosted service is added that will set the options as a static instance so it will be available for the lifetime of the service (since the APIs on HttpRuntime and HostingEnvironment were static). This hosted service will take care of clearing this value once the host is stopped.
@twsouthwick twsouthwick requested a review from Tratcher August 16, 2023 22:55
@twsouthwick
Copy link
Member Author

@Tratcher this enables the HostingEnvironment APIs on the adapter side. Once dotnet/aspnetcore#43632 is available, we can hook it up as a separate PR. This enables customizing the values surfaced through the APIs, though, so can be useable without the full IIS hookup.


Task IHostedService.StartAsync(CancellationToken cancellationToken)
{
HostingEnvironmentAccessor.Current = _accessor;
Copy link
Member

Choose a reason for hiding this comment

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

Why not set it directly in the HostingEnvironmentService or HostingEnvironmentAccessor constructors? And null it out in Dispose? You just need a live service container, not a running host, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

The HostingEnvironmentAccessor would need to be activated somehow to set it in its constructor. The goal here was to only have it set while running so it could be removed when not in use. Mostly for unit tests that I set to be run in serial and require that clean up to ensure tests aren't stomping on each other.

I switched it to use an IStartupFilter so it will be auto-activated instead of a hosted service (is there a difference in overhead here?).

@twsouthwick twsouthwick merged commit 17386eb into main Aug 17, 2023
@twsouthwick twsouthwick deleted the tasou/hosting-environment branch August 17, 2023 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ask About System.Web.Hosting.HostingEnvironment

3 participants