ASP.NET 5 (i.e. Core 1.0) Immutable Configuration -
i can't asp.net 5 (i.e. core 1.0) work immutable configuration classes.
let me demonstrate first using mutable class.
config.json
{ "name": "lou", "age": 30 } config.cs (mutable)
public class config { public string name { get; set; } public int age { get; set; } } registration in startup.cs
public void configureservices(iservicecollection services) { var configbuilder = new configurationbuilder() .addjsonfile("config.json"); var config = configbuilder.build(); services.configure<config>(config); services.addmvc(); } the code above works , values of config.name , config.age expected.
but once change config's property setters private, no longer set configuration builder:
public class config { public string name { get; private set; } // == null public int age { get; private set; } // == 0 } i have expected asp.net 5 use reflection , set properties asp.net 4, instead of ignoring them. missing something?
i created sample project demonstrate: https://github.com/johnnyoshika/mvc6-immutable-config master branch uses mutable config class while immutable-config branch uses immutable config class.
the configurationbinder object doesn't set private properties: https://github.com/aspnet/configuration/blob/ba1d9b4cbc363db9318f201f08fbfecc72e7922b/src/microsoft.extensions.configuration.binder/configurationbinder.cs#l64-l82
here's issue tracking that: https://github.com/aspnet/configuration/issues/394
one way workaround create interface getters that's implemented config code. then, can use in code.
Comments
Post a Comment