I have some configuration interfaces
public interface IAppConfig
{
IFeatureConfiguration FeatureConfiguration { get; set; }
IOtherConfiguration OtherConfiguration { get; set; }
}
public interface IFeatureConfiguration
{
string SettingFoo { get; set; }
}
public interface IOtherConfiguration
{
string SettingBar { get; set; }
}
and classes
public class AppConfig : IAppConfig
{
public IFeatureConfiguration FeatureConfiguration { get; set; }
public IOtherConfiguration OtherConfiguration { get; set; }
}
public class FeatureConfiguration : IFeatureConfiguration
{
public string SettingFoo { get; set; }
}
public class OtherConfiguration : IOtherConfiguration
{
public string SettingBar { get; set; }
}
I wanted to bind appsetting.json to class in Startup.cs using code:
var appConfiguration = Configuration.GetSection("Configuration").Get<AppConfig>();
And I have exception:
"Cannot create instance of type '[...].IFeatureConfiguration' because it is either abstract or an interface."
I know I can get rid of interfaces and use only classes, but I really want them... Any help would be appreciated
Getinspects the type you passed as a type parameter, creates a new instance and sets its properties based on the config value paths. For complex properties,Getwill try to do the same - use the property's type to create a new instance and fill it's properties with the matching subsection's values. When a property is an interface - which class should it instantiate? There may be an infinite number of classes that implement this specific interfaceIAppConfigto use one set of classes in one part of the application and a different set in another? Or pull data from different sources based on configuration - which is what the configuration subsystem already does? Both cases would be problematic