Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 6 additions & 4 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
{
"sdk": {
"version": "8.0.100-rc.1.23455.8"
"version": "8.0.100-rc.2.23502.2"
},
"tools": {
"dotnet": "8.0.100-rc.1.23455.8",
"dotnet": "8.0.100-rc.2.23502.2",
"runtimes": {
"dotnet": [
"6.0.4"
"6.0.4",
"7.0.12"
],
"aspnetcore": [
"6.0.4"
"6.0.4",
"7.0.12"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET8_0_OR_GREATER
using System;

namespace Microsoft.AspNetCore.Server.IIS;

/// <summary>
/// This feature provides access to IIS application information. This is <see href="https://github.com/dotnet/aspnetcore/blob/4218bd758012820a955b0185e5b1824168d00c6a/src/Servers/IIS/IIS/src/IIISEnvironmentFeature.cs">available in-box</see>
/// on .NET 8, but we have an internal copy so we can utilize its features on downlevel versions.
/// </summary>
internal interface IIISEnvironmentFeature
{
/// <summary>
/// Gets the version of IIS that is being used.
/// </summary>
Version IISVersion { get; }

/// <summary>
/// Gets the AppPool name that is currently running
/// </summary>
string AppPoolId { get; }

/// <summary>
/// Gets the path to the AppPool config
/// </summary>
string AppPoolConfigFile { get; }

/// <summary>
/// Gets path to the application configuration that is currently running
/// </summary>
string AppConfigPath { get; }

/// <summary>
/// Gets the physical path of the application.
/// </summary>
string ApplicationPhysicalPath { get; }

/// <summary>
/// Gets the virtual path of the application.
/// </summary>
string ApplicationVirtualPath { get; }

/// <summary>
/// Gets ID of the current application.
/// </summary>
string ApplicationId { get; }

/// <summary>
/// Gets the name of the current site.
/// </summary>
string SiteName { get; }

/// <summary>
/// Gets the id of the current site.
/// </summary>
uint SiteId { get; }
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Server.IIS;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting.Server;

namespace Microsoft.AspNetCore.SystemWebAdapters;

Expand All @@ -21,6 +25,8 @@ public static void AddHostingRuntime(this IServiceCollection services)
services.TryAddEnumerable(ServiceDescriptor.Singleton<IStartupFilter, HostingEnvironmentStartupFilter>());

services.AddOptions<SystemWebAdaptersOptions>()

// This configures for anyone using older IIS modules that don't set the values (and to maintain behavior with the adapters <1.3)
.Configure(options =>
{
options.IsHosted = true;
Expand All @@ -32,6 +38,20 @@ public static void AddHostingRuntime(this IServiceCollection services)
options.AppDomainAppVirtualPath = config.pwzVirtualApplicationPath;
options.AppDomainAppPath = config.pwzFullApplicationPath;
}
})

// On .NET 8+, IIISEnvironmentFeature is available by default if running on IIS. We have an internal version
// we load at startup so that regardless of version and server this may be available (for example, in case some
// one wants to set the environment variables on a Kestrel hosted system to get the behavior)
.Configure<IServer>((options, server) =>
{
if (server.Features.Get<IIISEnvironmentFeature>() is { } feature)
{
options.AppDomainAppPath = feature.ApplicationPhysicalPath;
options.AppDomainAppVirtualPath = feature.ApplicationVirtualPath;
options.ApplicationID = feature.ApplicationId;
options.SiteName = feature.SiteName;
}
});
}

Expand All @@ -43,11 +63,82 @@ public HostingEnvironmentStartupFilter(HostingEnvironmentAccessor accessor)
}

public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder => next(builder);
=> builder =>
{
// We ensure this feature is available for both pre-.NET 8 as well as .NET 8+ on non-IIS systems if the right environment variables are set
if (builder.ApplicationServices.GetService<IServer>() is { } server && server.Features.Get<IIISEnvironmentFeature>() is null)
{
if (IISEnvironmentFeature.TryCreate(builder.ApplicationServices.GetRequiredService<IConfiguration>(), out var feature))
{
server.Features.Set<IIISEnvironmentFeature>(feature);
}
}

next(builder);
};

public void Dispose()
{
HostingEnvironmentAccessor.Current = null;
}
}

/// <summary>
/// Copy of https://github.com/dotnet/aspnetcore/blob/4218bd758012820a955b0185e5b1824168d00c6a/src/Servers/IIS/IIS/src/Core/IISEnvironmentFeature.cs
/// </summary>
private sealed class IISEnvironmentFeature : IIISEnvironmentFeature
{
public static bool TryCreate(IConfiguration configuration, [NotNullWhen(true)] out IIISEnvironmentFeature? result)
{
var feature = new IISEnvironmentFeature(configuration);

if (feature.IISVersion is not null)
{
result = feature;
return true;
}

result = null;
return false;
}

private IISEnvironmentFeature(IConfiguration configuration)
{
if (Version.TryParse(configuration["IIS_VERSION"], out var version))
{
IISVersion = version;
}

if (uint.TryParse(configuration["IIS_SITE_ID"], out var siteId))
{
SiteId = siteId;
}

AppPoolId = configuration["IIS_APP_POOL_ID"] ?? string.Empty;
AppPoolConfigFile = configuration["IIS_APP_POOL_CONFIG_FILE"] ?? string.Empty;
AppConfigPath = configuration["IIS_APP_CONFIG_PATH"] ?? string.Empty;
ApplicationPhysicalPath = configuration["IIS_PHYSICAL_PATH"] ?? string.Empty;
ApplicationVirtualPath = configuration["IIS_APPLICATION_VIRTUAL_PATH"] ?? string.Empty;
ApplicationId = configuration["IIS_APPLICATION_ID"] ?? string.Empty;
SiteName = configuration["IIS_SITE_NAME"] ?? string.Empty;
}

public Version IISVersion { get; } = null!;

public string AppPoolId { get; }

public string AppPoolConfigFile { get; }

public string AppConfigPath { get; }

public string ApplicationPhysicalPath { get; }

public string ApplicationVirtualPath { get; }

public string ApplicationId { get; }

public string SiteName { get; }

public uint SiteId { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.IIS;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;

namespace Microsoft.AspNetCore.SystemWebAdapters;

public class HttpRuntimeIntegrationTests : SelfHostedTestBase
{
private const string IIS_VERSION = "IIS_Version";
private const string IIS_SITE_ID = "IIS_SITE_ID";
private const string IIS_SITE_NAME = "IIS_SITE_NAME";
private const string IIS_APPLICATION_VIRTUAL_PATH = "IIS_APPLICATION_VIRTUAL_PATH";
private const string IIS_PHYSICAL_PATH = "IIS_PHYSICAL_PATH";
private const string IIS_APPLICATION_ID = "IIS_APPLICATION_ID";
private const string IIS_APP_CONFIG_PATH = "IIS_APP_CONFIG_PATH";
private const string IIS_APP_POOL_CONFIG_FILE = "IIS_APP_POOL_CONFIG_FILE";
private const string IIS_APP_POOL_ID = "IIS_APP_POOL_ID";

[Fact]
public async Task ConfigureRuntimeViaConfig()
{
// Arrange
using var host = await GetTestHost()
.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string?>
{
[IIS_VERSION] = "10.0",
[IIS_SITE_ID] = "1",
[IIS_APP_POOL_ID] = IIS_APP_POOL_ID,
[IIS_APP_POOL_CONFIG_FILE] = IIS_APP_POOL_CONFIG_FILE,
[IIS_APP_CONFIG_PATH] = IIS_APP_CONFIG_PATH,
[IIS_PHYSICAL_PATH] = IIS_PHYSICAL_PATH,
[IIS_APPLICATION_VIRTUAL_PATH] = IIS_APPLICATION_VIRTUAL_PATH,
[IIS_APPLICATION_ID] = IIS_APPLICATION_ID,
[IIS_SITE_NAME] = IIS_SITE_NAME,
});
})
.StartAsync();

// Act
var options = host.Services.GetRequiredService<IOptions<SystemWebAdaptersOptions>>().Value;

// Assert
Assert.Equal(IIS_SITE_NAME, options.SiteName);
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath);
Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath);
Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID);
Assert.True(options.IsHosted);
}

[Fact]
public async Task ConfigureRuntimeViaFeature()
{
// Arrange
var feature = new Mock<IIISEnvironmentFeature>();

feature.Setup(f => f.SiteName).Returns(IIS_SITE_NAME);
feature.Setup(f => f.ApplicationVirtualPath).Returns(IIS_APPLICATION_VIRTUAL_PATH);
feature.Setup(f => f.ApplicationPhysicalPath).Returns(IIS_PHYSICAL_PATH);
feature.Setup(f => f.ApplicationId).Returns(IIS_APPLICATION_ID);
feature.Setup(f => f.AppConfigPath).Returns(IIS_APP_CONFIG_PATH);
feature.Setup(f => f.AppPoolConfigFile).Returns(IIS_APP_POOL_CONFIG_FILE);
feature.Setup(f => f.AppPoolId).Returns(IIS_APP_POOL_ID);

using var host = await GetTestHost()
.StartAsync();

host.GetTestServer().Features.Set<IIISEnvironmentFeature>(feature.Object);

// Act
var options = host.Services.GetRequiredService<IOptions<SystemWebAdaptersOptions>>().Value;

// Assert
Assert.Equal(IIS_SITE_NAME, options.SiteName);
Assert.Equal(IIS_APPLICATION_VIRTUAL_PATH, options.AppDomainAppVirtualPath);
Assert.Equal(IIS_PHYSICAL_PATH, options.AppDomainAppPath);
Assert.Equal(IIS_APPLICATION_ID, options.ApplicationID);
Assert.True(options.IsHosted);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Microsoft.AspNetCore.SystemWebAdapters;

[Collection(nameof(SelfHostedTests))]
public class SelfHostedTestBase
{
/// <summary>
/// This method starts up a host in the background that
/// makes it possible to initialize <see cref="HttpRuntime"/>
/// and <see cref="HostingEnvironment"/> with values needed
/// for testing with the <paramref name="configure"/> option.
/// </summary>
/// <param name="configure">
/// Configuration for the hosting and runtime options.
/// </param>
protected static IHostBuilder GetTestHost()
=> new HostBuilder()
.ConfigureWebHost(webBuilder =>
{
webBuilder
.UseTestServer()
.ConfigureServices(services =>
{
services.AddSystemWebAdapters();
})
.Configure(app =>
{
// No need to configure pipeline for tests
});
});
}