Repository navigation
Open
Labels
area-Extensions-Configurationin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature
Milestone
Description
Description
ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property
Reproduction Steps
build this code
namespace BindGeneratorRepro;
using System.Reflection;
internal class Program
{
private static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.AddOptions();
builder.Services.AddOptions<MyOptionA>()
.BindConfiguration("myPath", options => options.BindNonPublicProperties = true);
var app = builder.Build();
app.MapGet("/version", () =>
{
var fileVersionAttribute = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyFileVersionAttribute>();
return Results.Text(fileVersionAttribute!.Version);
});
app.Run();
}
}
internal sealed record MyOptionA
{
public required string MyParamA { get; init; }
public required MyOptionB MyParamAB { get; init; }
}
internal sealed record MyOptionB
{
public required string MyParamB { get; init; }
}Expected behavior
Code should compiles
Actual behavior
build files with error CS9035
BinGeneratorRepro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(106,31,106,40): error CS9035: Required member 'MyOptionB.MyParamB' must be set in the object initializer or attribute constructor.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
<EnableRequestDelegateGenerator>true</EnableRequestDelegateGenerator>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
</PropertyGroup>
</Project>Reacted by Bereznikov Alexey, Mark Junker, Valeriy Gourov, Alex Martossy, N0D4N, Hadrien Bècle, Austin Stanley, Mark Adamson, Craig E. Shea, Jasper and 21 more
Reacted by Paulus Pärssinen
Metadata
Metadata
Assignees
Labels
area-Extensions-Configurationin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is mergedsource-generatorIndicates an issue with a source generator featureIndicates an issue with a source generator feature
Activity
xmarshal commentedon Nov 20, 2023
[-]ConfigurationBinder source generator generates code that doesn't compile when using recodrs with requried properties as option Property [/-][+]ConfigurationBinder source generator generates code that doesn't compile when using recodrs or classes with requried properties as option Property [/+][-]ConfigurationBinder source generator generates code that doesn't compile when using recodrs or classes with requried properties as option Property [/-][+]ConfigurationBinder source generator build fails with CS9035 if option class have Property with requried[/+]ThaDaVos commentedon Apr 20, 2024
Any update on this?
ericstj commentedon Aug 8, 2024
Right now the source generator doesn't use object initializers at all, nor will it bind to
initproperties. This would require some significant refactoring of the generated code to support this scenario. We can consider this in a future release.KnapSac commentedon Dec 23, 2024
Are there plans to do some work in this area for .NET 10? Anything I can do to help?
rmannibucau commentedon Jun 25, 2025
up?
3 remaining items
snakex64 commentedon Aug 21, 2025
This is still encouraging unclean programming practices and forcing devs to not use newer more explicit syntaxes. For people in companies where you have to fight to move the mentality towards using nullables, init-only setters and required members this is a slap in the face that it's still an issue years later, and it just becomes an argument by other devs to never use them
NoirHare commentedon Oct 20, 2025
up?
abelevtsov commentedon Nov 28, 2025
Temporary set
<EnableConfigurationBindingGenerator>false</EnableConfigurationBindingGenerator>in my Web API .NET 10 project - it works.rmannibucau commentedon Nov 28, 2025
except if you do use AOT mode
vborovikov commentedon Dec 13, 2025
Surprisingly it doesn't work in a desktop app. I don't even use AOT, just trying to publish an app as a framework-dependent single file.
Something re-enables this flag during the build process.
vborovikov commentedon Dec 13, 2025
So this is my workaround for my specific use-case, a WPF app which uses
Microsoft.Extensions.*packages and is required to be published with the deployment mode asFramework-dependentand withProduce single fileoption enabled.At the beginning of the
*.csprojfile add:At the end of the
*.csprojfile add:So in my case the target
ResolveLockFileAnalyzerskept adding that analyzer back to the list of analyzers. The original target_Microsoft_Extensions_Configuration_Binder_RemoveAnalyzerfrom"Microsoft.Extensions.Configuration.Binderpackage doesn't depend on it so it wasn't triggered at the right time.This workaround might work for you too as it is. But If it's not working look at MSBuild logs and find targets which populate
Analyzeritems withMicrosoft.Extensions.Configuration.Binderanalyzer and update theAfterTargetsattribute with these targets.rosebyte commentedon Mar 5, 2026
Triage: we may consider this feature if we happen to refactor the source generator.
hwoodiwiss commentedon Jun 5, 2026
@rosebyte Is this something that you'd consider a community contribution for? This is currently one of the bigger barriers to cleanly adopting NRT in a lot of my teams codebases, so I'm personally motivated to get something working in the shorter term.
Fix config binding source generator duplicating collection items boun…
Address review: bind-existing-instance and required-nested coverage