Aspnetcore: Get Configuration from IConfigurationRoot

Created on 23 May 2016  路  4Comments  路  Source: dotnet/aspnetcore

Hello,

I always used this to register configuration.

services.Configure<ScopeConfiguration>(_configuration.GetSection("ScopeConfiguration"));

In my json:

"ScopeConfiguration": {
    "Flow": "implicit",
    "Scopes": [
      {
        "Name": "tts.warranty_request", // name of the policy, used in attribute: Authorize("read")
        "Value": "tts.warranty_request", // value as found in the token
        "Description": "Allows access to the warranty request service" // swagger description
      }
    ]
  },

and my class

public class ScopeConfiguration
    {
        public string Flow { get; set; }

        public List<Scope> Scopes { get; set; } = new List<Scope>();
    }

    public class Scope
    {
        /// <summary>
        /// Name of the policy, used in attribute: Authorize("read")
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Value as found in the token
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// Swagger description
        /// </summary>
        public string Description { get; set; }
    }

this works all fine with

   "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0-rc2-final",

now I always got those models back with

 ScopeConfiguration scopeConfiguration = _configuration.Get<ScopeConfiguration>("ScopeConfiguration");

but the "Get" method doesn't exist anymore. There is a "GetValue" which works for simple types but not for my class, and there is "Bind(object instance)", I don't know what this is for..

Can someone help me how I get typed object out of my Configuration?

Edit: Works with

ScopeConfiguration scopeConfiguration = services.BuildServiceProvider().GetService<IOptions<ScopeConfiguration>>().Value;

is this the way to do this?
I'll need a reference to IServiceCollection for this which may not be the case in unit tests.

Most helpful comment

I hit the same thing, this is an unfortunate change, especially when the underlying code could clearly handle this (it handles creating objects like this inside arrays in the bind statement). In the meantime this should handle it:

    public static class ConfigurationExtensions
    {
        public static T Get<T>(this IConfiguration config, string key) where T : new()
        {
            var instance = new T();
            config.GetSection(key).Bind(instance);
            return instance;
        }
    }

All 4 comments

Can you use DI to inject a reference to it? Like so:

  public void Configure( IOptions<ScopeConfiguration> config, IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext db)
        {
            ...
            ...
        }

Well this is not possible in Units Tests with XUnit as far as I know. Of cause I can plug in a third party DI Container, but the .Net Core one isn't supported yet.

I hit the same thing, this is an unfortunate change, especially when the underlying code could clearly handle this (it handles creating objects like this inside arrays in the bind statement). In the meantime this should handle it:

    public static class ConfigurationExtensions
    {
        public static T Get<T>(this IConfiguration config, string key) where T : new()
        {
            var instance = new T();
            config.GetSection(key).Bind(instance);
            return instance;
        }
    }

In the end I came up with the same solution.
Already saw that bind before but didn't know how to use it at first.

Thanks, closed

Was this page helpful?
0 / 5 - 0 ratings