-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Description
I have a project on Windows Forms and after upgrading from net6.0-windows to net8.0-windows, there was a problem when building IConfigurationBuilder related to the FileSystemWatcher ObjectDisposedException error. The following code throw a error :
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ServiceCollection services = new();
Startup.ConfigureServices(services);
// other code
}
}
public static class Startup
{
public static void ConfigureServices(IServiceCollection services)
{
IConfiguration configuration = BuildConfiguration();
// other code
}
private static IConfiguration BuildConfiguration()
{
string? environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true);
builder.AddServiceConnectConfiguration();
return builder.Build();
}
}
The following exception occurs during execution :
CoreCLR Version: 8.0.23.53103
.NET Version: 8.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'System.IO.FileSystemWatcher'.
at System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)
at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.TryEnableFileSystemWatcher()
at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
at Microsoft.Extensions.FileProviders.PhysicalFileProvider.Watch(String filter)
at Microsoft.Extensions.Primitives.ChangeToken.ChangeTokenRegistration1..ctor(Func1 changeTokenProducer, Action1 changeTokenConsumer, TState state) at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func1 changeTokenProducer, Action changeTokenConsumer)
at Microsoft.Extensions.Configuration.FileConfigurationProvider..ctor(FileConfigurationSource source)
at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
If I change reloadOnChange to false, no error will occur.
Reproduction Steps
Create a simple winforms application on .net8-windows with the above code.
Expected behavior
No error have occured.
Actual behavior
Application has not started due to following exception : System.ObjectDisposedException
Cannot access a disposed object.
Object name: 'System.IO.FileSystemWatcher'.
Regression?
No issues with the same code in .NET 6.0
Known Workarounds
No response
Configuration
.net8.0.100 Windows 11 Pro 22621.1848 x64
Other information
No response