System.InvalidOperationException: Failed to convert configuration value '30 minutes'
at 'Jwt:ExpiryMinutes' to type 'System.Int32'. means a setting bound to an
int property holds text that is not a number. The message names the value,
the key and the type, so the fix is to make that value a plain number, here
"ExpiryMinutes": 30, and put the unit in the property name, not in the value.
Two sources were tested, and both gave the message: appsettings.json and,
after that file was fixed, an environment variable Jwt__ExpiryMinutes=thirty,
which overrode it. Bind1, JwtSettings and the
fix11-test-issuer value are this test's own names. The commands on this page ran in cmd.
The error
dotnet run --no-build --no-launch-profile
Unhandled exception. System.InvalidOperationException: Failed to convert configuration value '30 minutes' at 'Jwt:ExpiryMinutes' to type 'System.Int32'.
---> System.ArgumentException: 30 minutes is not a valid value for Int32. (Parameter 'value')
---> System.FormatException: The input string '30 minutes' was not in a correct format.
at System.Number.ThrowFormatException[TChar](ReadOnlySpan`1 value)
...
at Microsoft.Extensions.Configuration.ConfigurationBinder.Get[T](IConfiguration configuration, Action`1 configureOptions)
at Program.<Main>$(String[] args) in C:\...\fixes11\cfg\Bind1\Program.cs:line 3
exit=-532462766
The path is shortened. The two inner exceptions say the same thing from lower down: the
number converter, then Int32.Parse, refused the string.
Why it happens
var builder = WebApplication.CreateBuilder(args);
var jwt = builder.Configuration.GetSection("Jwt").Get<JwtSettings>()!;
Console.WriteLine($"Issuer {jwt.Issuer}, tokens last {jwt.ExpiryMinutes} minutes");
...
public class JwtSettings
{
public string Issuer { get; set; } = "";
public int ExpiryMinutes { get; set; }
}
{
"AllowedHosts": "*",
"Jwt": {
"Issuer": "fix11-test-issuer",
"ExpiryMinutes": "30 minutes"
}
}
The JSON value is a string in quotes, and the property is an int. The last
frames shown are ConfigurationBinder.Get[T] and the program's line 3, and
"30 minutes" did not parse as an Int32, so binding stopped with
the key named. A quoted number did bind: the same file with
"ExpiryMinutes": "30" printed tokens last 30 minutes. So the
quotes were not the problem here; the words after the number were. The build
before that run printed 0 warnings and 0 errors; the mistake showed only when the app
started.
The fix
{
"AllowedHosts": "*",
"Jwt": {
"Issuer": "fix11-test-issuer",
"ExpiryMinutes": 30
}
}
dotnet run --no-build --no-launch-profile
Issuer fix11-test-issuer, tokens last 30 minutes
exit=0
If the file is right and the message stays, look at the other sources. With the fixed file, this override brought the error back with its own value in the message:
set Jwt__ExpiryMinutes=thirty& dotnet run --no-build --no-launch-profile
Unhandled exception. System.InvalidOperationException: Failed to convert configuration value 'thirty' at 'Jwt:ExpiryMinutes' to type 'System.Int32'.
...
In the override run the message quoted the environment's value, 'thirty', not
the file's, which pointed at the source to fix. With Jwt__ExpiryMinutes=45
instead, the same run bound 45.
How it was reproduced
A project from dotnet new web -f net10.0 with SDK 11.0.100 on Windows 11, no
packages added, the template's appsettings.Development.json moved out. The
program bound the Jwt section with Get<JwtSettings>() at
start-up and printed the values; it exited instead of serving because the lab set
FIX11_NO_SERVE=1 and Program.cs returns before
app.Run() when it is set (that line is in the part cut from the excerpt
above). It ran with "30 minutes" in the file, with 30, with
"30", and with the file fixed and Jwt__ExpiryMinutes set
to thirty and to 45 in cmd.
Every command shown without a PS> marker ran in cmd: a PowerShell script passed it as cmd /c ... 2>&1 and printed the exit= line after it.
Frequently asked
- How do I fix Failed to convert configuration value to type System.Int32?
- Find the key named in the message and make its value a plain number. In this test "ExpiryMinutes": "30 minutes" failed and "ExpiryMinutes": 30 bound and the app started.
- Can an environment variable cause Failed to convert configuration value?
- Yes. In this test Jwt__ExpiryMinutes=thirty overrode a correct appsettings.json and gave the same exception with 'thirty' in the message, while Jwt__ExpiryMinutes=45 bound as 45.
- Why does the message show a value that is not in my appsettings.json?
- The message quotes the value that was bound, and a later source such as an environment variable can override the file. In this test the message showed 'thirty' from the environment, not the file's 30.
More decoded errors in the Fixes category. If the settings file itself is refused, see Failed to load configuration from file.