Unexpected error when using records
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
Hello! We have a record like this
```c#
public record TemplateRequirementsValidationRequestDto(
[property: DataMember(Name = "templateRequirements")]
string TemplateRequirements,
[property: DataMember(Name = "mailingKind")]
MailingKind MailingKind,
[property: DataMember(Name = "recipientsFileInternalId")]
int? RecipientsFileInternalId);
```
which we use in the endpoint like this
```c#
[HttpPost("do-smth")]
public async Task DoSmth(
[FromBody] TemplateRequirementsValidationRequestDto request)
{
// ...
}
```
this code works stably and has been live for a whole year
Our application has several instances (2) with a balancer that proxies requests to one of these instances. And one day we saw this error:
`InvalidOperationException: No property found that maps to constructor parameter 'RecipientsFileInternalId' for type 'Services.Core.TemplateRequirementsValidationRequestDto'. Validation requires that each bound parameter of a record type's primary constructor must have a property to read the value.`
We did a little research and found out that the error occurred only on one instance, while the other instance worked without this error. Therefore, half of the requests completed successfully. After restarting the problematic instance, the errors disappeared
This error was introduced into the framework in this [PR](https://github.com/dotnet/aspnetcore/pull/23976) with the addition of support for records and appears in [this](https://github.com/dotnet/aspnetcore/blob/d088530dced8d71ed81f322fecfcf6b03ae3d9de/src/Mvc/Mvc.Core/src/ModelBinding/Validation/DefaultComplexObjectValidationStrategy.cs#L99) place
[The tests](https://github.com/dotnet/aspnetcore/blob/e2a857c8ccda4dcfac3381a166faaf3542d85c62/src/Mvc/Mvc.Core/test/ModelBinding/Validation/DefaultComplexObjectValidationStrategyTest.cs#L162) are not very informative in this particular place - so it was not possible to understand why this error occurred, perhaps there is a race somewhere or something else. Help me figure it out!
### Expected Behavior
The error does not occur at random times
### Steps To Reproduce
No steps
### Exceptions (if any)
InvalidOperationException: No property found that maps to constructor parameter 'RecipientsFileInternalId' for type 'Services.Core.TemplateRequirementsValidationRequestDto'. Validation requires that each bound parameter of a record type's primary constructor must have a property to read the value.
### .NET Version
8.0.x
### Anything else?
_No response_
Contributor guide
Research direction
Start by reading DefaultComplexObjectValidationStrategy.cs around line 99 and the corresponding DefaultComplexObjectValidationStrategyTest.cs case around line 162. Investigate the intermittent record model-binding validation failure on .NET 8, using the reported record shape and two-instance behavior as context. Done means identifying why the error occurs on only one instance or adding a reproducible, informative test that demonstrates it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100