Skip to content

TimeOnly is not read from appsettings #64739

Closed

Description

@wertzui

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Having an appsettings.json that looks like this

{
    "MyTime": "01:02"
}

When trying to bind this to a TimeOnly, the result is always an Exception (or 00:00 depending on how it is read), while the result is 01:02 when binding to a TimeSpan.

Expected Behavior

The value can be bound to a TimeOnly instance.

Steps To Reproduce

configuration.GetValue<TimeSpan>("MyTime"); // 01:02
configuration.GetValue<TimeOnly>("MyTime"); // System.NullReferenceException
configuration.GetSection("MyTime").Get<TimeOnly>(); // 00:00

Exceptions (if any)

It is a System.NullReferenceException

Stack trace for the Exception:

at System.Runtime.CompilerServices.CastHelpers.Unbox(Void* toTypeHnd, Object obj)
at Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue[T](IConfiguration configuration, String key, T defaultValue)
at Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue[T](IConfiguration configuration, String key)

.NET Version

6.0.200-preview.21617.4

Anything else?

No response

Activity

  1. transferred this issue fromdotnet/aspnetcoreon Feb 3, 2022
  2. ghost added
    untriaged
    on Feb 3, 2022
  3. tarekgh commented on Feb 3, 2022

    @tarekgh
    Member

    @wertzui Thanks for your report. This is a duplicate of #59253.

  4. ghost locked as resolved and limited conversation to collaborators on Mar 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Extensions-Configurationuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions