[.NET Generator] Auto-generate ClientSettings class and IConfiguration-enabled constructors#9920
Draft
[.NET Generator] Auto-generate ClientSettings class and IConfiguration-enabled constructors#9920
Conversation
…onfiguration-enabled constructors
- Add new ClientSettingsProvider class that generates {Client}Settings classes
- Extends ClientSettings from System.ClientModel.Primitives (future type)
- Properties for endpoint (Uri?) and options ({Client}Options?)
- BindCore override method binding from IConfigurationSection
- Marked [Experimental("SCME0002")]
- Add IConfigurationSection constructor to ClientOptionsProvider
- Internal constructor calling base(section)
- Guards with null/Exists check
- Binds non-version string properties from configuration
- Marked [Experimental("SCME0002")]
- Add settings constructor to ClientProvider for root clients with configurable endpoints
- Calls primary constructor via this() initializer
- Passes endpoint, credential (if auth), and options from settings
- Marked [Experimental("SCME0002")]
- Add ClientSettingsProvider output in ScmOutputLibrary.BuildClient
- Add CSharpType.FromExternalType() public factory method for types not in current NuGet
- Update tests to account for new constructors and generated code
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…Settings feature - Use HashSet for O(1) version property name lookup in BuildConfigurationSectionConstructor - Rename local variable from 'propValue' suffix to 'FromConfig' for clarity - Extract IsSettingsConstructor helper in test for maintainability Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add auto-generation of ClientSettings class and IConfiguration constructors
[.NET Generator] Auto-generate ClientSettings class and IConfiguration-enabled constructors
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The .NET generator did not produce
IConfiguration-based client construction support, requiring manual implementation per client. This adds automatic generation of the three artifacts needed for every root client to support standard .NET configuration patterns (appsettings.json, env vars, DI containers).New:
ClientSettingsProviderGenerates
{Client}Settings : ClientSettings(System.ClientModel.Primitives) with:Uri? {EndpointName}and{Client}Options? Optionsproperties (credential handled by baseCredentialProvider)BindCore(IConfigurationSection section)override binding endpoint and options from config[Experimental("SCME0002")]on the classModified:
ClientOptionsProviderAdds an internal
IConfigurationSectionconstructor forwarding tobase(section), with an early-exit null/exists guard, binding non-version service-specific properties.Modified:
ClientProviderAdds a
public {Client}({Client}Settings settings)constructor delegating to the primary constructor — only for root clients with a configurable endpoint:Modified:
ScmOutputLibrary/ClientProviderClientProviderexposes aClientSettingsproperty (ClientSettingsProvider?)ScmOutputLibrary.BuildClientregisters theClientSettingsProviderin the output type set alongsideClientOptionsNotes
ClientOptions != null)ClientSettingsandIConfigurationSectionare referenced by name via a newCSharpTypefactory for external types not yet in the pinnedSystem.ClientModel1.9.0 — generated code will compile once the upstream package ships these typesWarning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
https://api.github.com/repos/Azure/azure-sdk-for-net/contents/sdk/core/System.ClientModel/src/usr/bin/curl curl -s REDACTED(http block)https://api.github.com/repos/Azure/azure-sdk-for-net/git/trees/main/usr/bin/curl curl -s REDACTED(http block)www.nuget.org/usr/bin/curl curl -L -s REDACTED(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
This section details on the original issue you should resolve
<issue_title>[.NET Generator] Auto-generate ClientSettings class and IConfiguration-enabled constructors for clients</issue_title>
<issue_description>## Overview
Parent issue: Azure/azure-sdk-for-net#55491
The .NET generator should automatically produce
IConfiguration-based client construction support for every generated client. This enables developers to configure clients using standard .NET configuration patterns (appsettings.json, environment variables) and register them with dependency injection containers.Two services have been manually implemented as reference:
The generator should produce these automatically so every client gets configuration support without manual customization.
What the Generator Needs to Produce
Three artifacts per client:
A.
{Client}SettingsclassClientSettings(fromSystem.ClientModel.Primitives)BindCore(IConfigurationSection section)override[Experimental("SCME0002")]B.
internal {Client}Options(IConfigurationSection section)constructorbase(section)— the base options class handles common properties (see Forwarding Binding)if (section is null || !section.Exists()) { return; }before binding any properties[Experimental("SCME0002")]C.
public {Client}({Client}Settings settings)constructor on the client(AuthenticationPolicy, Options)or(Uri, AuthenticationPolicy, Options)depending on the clientAuthenticationPolicy.Create(settings)to pass to the primary constructorsettings?.Endpoint,settings?.Options)[Experimental("SCME0002")]How to Determine Settings Properties
The generator should inspect the client's primary public constructor (the one with the body implementation) parameters:
Uri?properties on Settings (e.g.,VaultUri,Endpoint){Client}Options?property on SettingsClientSettings.Credential/CredentialProvider, NOT duplicatedHow to Implement BindCore
`csharp
protected override void BindCore(IConfigurationSection section)
{
if (section is null || !section.Exists())
{
return;
}
}
`
Key rule: If a configuration segment doesn't exist, bail early — don't set the property. No validation in BindCore. Let the client constructor that receives the settings do all required parameter validation (e.g.,
Argument.AssertNotNull).Type Binding Patterns
The generator should use these patterns to bind properties from
IConfigurationSection:stringsection[name] is string valTenantId = valboolbool.TryParse(section[name], out bool val)DisableChallengeResourceVerification = valUriUri.TryCreate(section[name], UriKind.Absolute, out Uri val)RedirectUri = valTimeSpanTimeSpan.TryParse(section[name], out TimeSpan val)NetworkTimeout = valintint.TryParse(section[name], out int val)MaxRetries = valstring[]/List<string>section.GetSection(name).GetChildren().Where(c => c.Value is not null).Select(c => c.Value!).ToList()AdditionallyAllowedTenantssection.GetSection(name)+.Exists()+new Type(section)BrowserCustomization = new BrowserCustomizationOptions(browserSection)new TypeName(section[name])after null checkAudience = new AppConfigurationAudience(audience)For a comprehensive example of bin...
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.