Skip to content

Fix NRE in KestrelServerOptions.Listen*() #21467

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

Merged
2 commits merged into from
May 5, 2020
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
17 changes: 9 additions & 8 deletions src/Servers/Kestrel/Core/src/Internal/ConfigurationReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@ internal class ConfigurationReader

private readonly IConfiguration _configuration;

private IDictionary<string, CertificateConfig> _certificates;
private EndpointDefaults _endpointDefaults;
private IEnumerable<EndpointConfig> _endpoints;
private bool? _latin1RequestHeaders;

public ConfigurationReader(IConfiguration configuration)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
Certificates = ReadCertificates();
EndpointDefaults = ReadEndpointDefaults();
Endpoints = ReadEndpoints();
Latin1RequestHeaders = _configuration.GetValue<bool>(Latin1RequestHeadersKey);
}

public IDictionary<string, CertificateConfig> Certificates { get; }
public EndpointDefaults EndpointDefaults { get; }
public IEnumerable<EndpointConfig> Endpoints { get; }
public bool Latin1RequestHeaders { get; }
public IDictionary<string, CertificateConfig> Certificates => _certificates ??= ReadCertificates();
public EndpointDefaults EndpointDefaults => _endpointDefaults ??= ReadEndpointDefaults();
public IEnumerable<EndpointConfig> Endpoints => _endpoints ??= ReadEndpoints();
public bool Latin1RequestHeaders => _latin1RequestHeaders ??= _configuration.GetValue<bool>(Latin1RequestHeadersKey);

private IDictionary<string, CertificateConfig> ReadCertificates()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Servers/Kestrel/Core/src/KestrelConfigurationLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ internal KestrelConfigurationLoader(KestrelServerOptions options, IConfiguration
Options = options ?? throw new ArgumentNullException(nameof(options));
Configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
ReloadOnChange = reloadOnChange;

ConfigurationReader = new ConfigurationReader(configuration);
}

public KestrelServerOptions Options { get; }
Expand Down
11 changes: 11 additions & 0 deletions src/Servers/Kestrel/Core/test/KestrelServerOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,16 @@ public void ConfigureEndpointDefaultsAppliesToNewEndpoints()
});
Assert.Equal(HttpProtocols.Http2, options.CodeBackedListenOptions[3].Protocols);
}

[Fact]
public void CanCallListenAfterConfigure()
{
var options = new KestrelServerOptions();
options.Configure();

// This is a regression test to verify the Listen* methods don't throw a NullReferenceException if called after Configure().
// https://github.com/dotnet/aspnetcore/issues/21423
options.ListenLocalhost(5000);
}
}
}
6 changes: 4 additions & 2 deletions src/Servers/Kestrel/Kestrel/test/ConfigurationReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void ReadEndpointWithMissingUrl_Throws()
{
new KeyValuePair<string, string>("Endpoints:End1", ""),
}).Build();
Assert.Throws<InvalidOperationException>(() => new ConfigurationReader(config));
var reader = new ConfigurationReader(config);
Assert.Throws<InvalidOperationException>(() => reader.Endpoints);
}

[Fact]
Expand All @@ -107,7 +108,8 @@ public void ReadEndpointWithEmptyUrl_Throws()
{
new KeyValuePair<string, string>("Endpoints:End1:Url", ""),
}).Build();
Assert.Throws<InvalidOperationException>(() => new ConfigurationReader(config));
var reader = new ConfigurationReader(config);
Assert.Throws<InvalidOperationException>(() => reader.Endpoints);
}

[Fact]
Expand Down