Skip to content
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

Dispose the ContentFileProvider when the host is disposed #50181

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class HostBuilder : IHostBuilder
private HostBuilderContext _hostBuilderContext;
private HostingEnvironment _hostingEnvironment;
private IServiceProvider _appServices;
private PhysicalFileProvider _defaultProvider;

/// <summary>
/// A central location for sharing state between components during the host building process.
Expand Down Expand Up @@ -162,7 +163,7 @@ private void CreateHostingEnvironment()
_hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name;
}

_hostingEnvironment.ContentRootFileProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath);
_hostingEnvironment.ContentRootFileProvider = _defaultProvider = new PhysicalFileProvider(_hostingEnvironment.ContentRootPath);
}

private string ResolveContentRootPath(string contentRootPath, string basePath)
Expand Down Expand Up @@ -219,6 +220,8 @@ private void CreateServiceProvider()
services.AddSingleton<IHost>(_ =>
{
return new Internal.Host(_appServices,
_hostingEnvironment,
_defaultProvider,
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
_appServices.GetRequiredService<IHostApplicationLifetime>(),
_appServices.GetRequiredService<ILogger<Internal.Host>>(),
_appServices.GetRequiredService<IHostLifetime>(),
Expand Down
49 changes: 40 additions & 9 deletions src/libraries/Microsoft.Extensions.Hosting/src/Internal/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -18,13 +19,23 @@ internal class Host : IHost, IAsyncDisposable
private readonly IHostLifetime _hostLifetime;
private readonly ApplicationLifetime _applicationLifetime;
private readonly HostOptions _options;
private readonly IHostEnvironment _hostEnvironment;
private readonly PhysicalFileProvider _defaultProvider;
private IEnumerable<IHostedService> _hostedServices;

public Host(IServiceProvider services, IHostApplicationLifetime applicationLifetime, ILogger<Host> logger,
IHostLifetime hostLifetime, IOptions<HostOptions> options)
public Host(IServiceProvider services,
IHostEnvironment hostEnvironment,
PhysicalFileProvider defaultProvider,
IHostApplicationLifetime applicationLifetime,
ILogger<Host> logger,
IHostLifetime hostLifetime,
IOptions<HostOptions> options)
{
Services = services ?? throw new ArgumentNullException(nameof(services));
_applicationLifetime = (applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime))) as ApplicationLifetime;
_hostEnvironment = hostEnvironment;
_defaultProvider = defaultProvider;

if (_applicationLifetime is null)
{
throw new ArgumentException("Replacing IHostApplicationLifetime is not supported.", nameof(applicationLifetime));
Expand Down Expand Up @@ -131,14 +142,34 @@ public async Task StopAsync(CancellationToken cancellationToken = default)

public async ValueTask DisposeAsync()
{
switch (Services)
// The user didn't change the ContentRootFileProvider instance, we can dispose it
if (ReferenceEquals(_hostEnvironment.ContentRootFileProvider, _defaultProvider))
{
// Dispose the content provider
await DisposeAsync(_hostEnvironment.ContentRootFileProvider).ConfigureAwait(false);
}
else
{
case IAsyncDisposable asyncDisposable:
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
break;
case IDisposable disposable:
disposable.Dispose();
break;
// In the rare case that the user replaced the ContentRootFileProvider, dispose it and the one
eerhardt marked this conversation as resolved.
Show resolved Hide resolved
// we originally created
await DisposeAsync(_hostEnvironment.ContentRootFileProvider).ConfigureAwait(false);
await DisposeAsync(_defaultProvider).ConfigureAwait(false);
}

// Dispose the service provider
await DisposeAsync(Services).ConfigureAwait(false);

static async ValueTask DisposeAsync(object o)
{
switch (o)
{
case IAsyncDisposable asyncDisposable:
await asyncDisposable.DisposeAsync().ConfigureAwait(false);
break;
case IDisposable disposable:
disposable.Dispose();
break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.Hosting.Fakes;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Microsoft.Extensions.Hosting.Tests
Expand Down Expand Up @@ -531,6 +532,20 @@ public void BuilderPropertiesAreAvailableInBuilderAndContext()
using (hostBuilder.Build()) { }
}

[Fact]
public void DisposingHostDisposesContentFileProvider()
{
var host = new HostBuilder()
.Build();

var env = host.Services.GetRequiredService<IHostEnvironment>();
var fileProvider = new FakeFileProvider();
env.ContentRootFileProvider = fileProvider;

host.Dispose();
Assert.True(fileProvider.Disposed);
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34580", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void HostServicesSameServiceProviderAsInHostBuilder()
Expand All @@ -544,6 +559,15 @@ public void HostServicesSameServiceProviderAsInHostBuilder()
Assert.Same(appServicesFromHostBuilder, host.Services);
}

private class FakeFileProvider : IFileProvider, IDisposable
{
public bool Disposed { get; set; }
public void Dispose() { Disposed = true; }
public IDirectoryContents GetDirectoryContents(string subpath) => throw new NotImplementedException();
public IFileInfo GetFileInfo(string subpath) => throw new NotImplementedException();
public IChangeToken Watch(string filter) => throw new NotImplementedException();
}

private class ServiceC
{
public ServiceC(ServiceD serviceD) { }
Expand Down