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

Implement manual configuration binding for proxy config #521

Merged
merged 6 commits into from
Nov 11, 2020

Conversation

davidfowl
Copy link
Contributor

  • Removed the intermediate *Data types. This makes it clear that there are really 2 models, the input configuration and the runtime model the proxy uses.
  • Removed use of IOptionMonitor<T> and bind directly to configuration due to inability to handle parsing or validation errors.

Fixes #481

PS: This change lead me to file this issue dotnet/runtime#44381

{
// This method is protected so we use reflection to trigger it. The alternative is to wrap or implement
// a custom configuration provider and expose OnReload as a public method
var reload = typeof(ConfigurationProvider).GetMethod("OnReload", BindingFlags.NonPublic | BindingFlags.Instance);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly hacky but it let me skip writing a custom configuration provider that replicates the in memory provider 😄. I can if this feels too hacky though...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's in tests, I think it's fine as long as it saves redundant code lines.

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't all of the int, double, and TimeSpan TryParse calls need culture and style specified?

- Removed the intermediate *Data types. This makes it clear that there are really 2 models, the input configuration and the runtime model the proxy uses.
- Removed use of IOptionMonitor<T> and bind directly to configuration due to inability to handle parsing or validation errors.

Fixes #481
@davidfowl davidfowl force-pushed the davidfowl/manual-config-binding branch from ca10922 to 7f99a25 Compare November 10, 2020 13:41
@davidfowl
Copy link
Contributor Author

Updated

Copy link
Member

@Tratcher Tratcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete the new ProxyHttpRequestData now that you've rebased.

Comment on lines 10 to 33
internal static int ReadInt32(this IConfiguration configuration, string name)
{
return configuration[name] is string value ? int.Parse(value, NumberStyles.None, CultureInfo.InvariantCulture) : default;
}

internal static double ReadDouble(this IConfiguration configuration, string name)
{
return configuration[name] is string value ? double.Parse(value, CultureInfo.InvariantCulture) : default;
}

internal static TimeSpan ReadTimeSpan(this IConfiguration configuration, string name)
{
return configuration[name] is string value ? TimeSpan.Parse(value, CultureInfo.InvariantCulture) : default;
}

internal static TEnum ReadEnum<TEnum>(this IConfiguration configuration, string name) where TEnum : struct
{
return configuration[name] is string value ? Enum.Parse<TEnum>(value, ignoreCase: true) : default;
}

internal static bool ReadBool(this IConfiguration configuration, string name)
{
return configuration[name] is string value ? bool.Parse(value) : default;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of the cases where you're parsing primitives should be returning nullables rater than returning the default (0, false, etc.). In fact, I think we should clarify our policy so that all options properties should be nullable and not set by default. That lets the implementation decide what default to apply if none is specified, especially since many of the implementations will replaceable.

E.g. this should be nullable and not set. Let the factory choose the default.

public bool DangerousAcceptAnyServerCertificate { get; set; } = true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this in a different PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- Be more explicit about defaults
- Move defaults to configuration reading
@davidfowl davidfowl merged commit 12c40f0 into master Nov 11, 2020
@davidfowl davidfowl deleted the davidfowl/manual-config-binding branch November 11, 2020 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use explicit config binding
4 participants