dotnet / dotnet/aspnetcore

APP_POOL_ID environment variable is null when Preload = True

Open
#59,213 2 comments 0 reactions 0 assignees View on GitHub
area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

As highlighted here https://github.com/dotnet/aspnetcore/issues/47093#issuecomment-1472344840, when running an ASP.NET CORE application (.NET 6 and higher) in IIS with "Preload = true" (Application Initialization module), the **APP_POOL_ID** environment variable is null.

Setting "Preload = false" in IIS everything works fine, the **APP_POOL_ID** environment variable is correctly set.

This is a significant problem in some scenarios, because the application is not aware of its Application Pool, which is often a discriminator for configurations loading.

In my case, for example, multiple Application Pools and WebSites are sharing/based on the same application folder and files.
Every web site loads its own configuration based on the name of the Application Pool that hosts it, like this:

```cs
var appBuilder = WebApplication.CreateBuilder(args);
var configurationBuilder = appBuilder.Configuration;

// Add a JSON file based on the APP_POOL_ID
var appPoolId = Environment.GetEnvironmentVariable("APP_POOL_ID");
var fileName = $"appsettings.AppPoolId_{appPoolId}.json";
configurationBuilder = configurationBuilder.AddJsonFile(fileName, false);

// Build the configuration before running the application
var configuration = configurationBuilder.Build();

// Use the configuration to conditionally include services and components
DoSomethingWithTheConfigurationLikeConditionallyIncludingServicesAndComponents(configuration);

// Build and run the application
var app = appBuilder.Build();
app.Run();
```

I tried waiting for the variable to be populated (since the original post seemed to suggest that this problem is somehow time-related), but it never happens.

```cs
var appPoolId = Environment.GetEnvironmentVariable("APP_POOL_ID");
logger.LogInformation("App Pool Id = {AppPoolId}", appPoolId);

if (string.IsNullOrWhiteSpace(appPoolId))
{
do
{
logger.LogError("App Pool Id is NOT VALID. This is NOT allowed in {EnvironmentName}", appBuilder.Environment.EnvironmentName);
appPoolId = Environment.GetEnvironmentVariable("APP_POOL_ID");

if (string.IsNullOrWhiteSpace(appPoolId)) // still unresolved ?
{
// wait a bit, maybe the environment variables will be set correctly
Thread.Sleep(1000);
}
else
{
break; // App Pool ID resolved, exit the loop and continue with the application initialization
}

} while (true); // loop unti a valid App Pool Id is resolved
}
```

I also couldn't find any documentation about this behaviour, or anything related.
Is it a bug in IIS ? In ASP.NET CORE ?
Is there a known workaround, or a proper solution to this problem ?

Any suggestion will be greatly appreciated :)

### Expected Behavior

I expect the environment variable **APP_POOL_ID** to be correctly set, even when "Preload = true" (Application Initialization module)

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

.NET 6 or higher

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.