As an example of more complex binding in ASP.Net Core 2.1; I found using the ConfigurationBuilder
.Get<T>()
method far easier to work with, as per the documention.
ASP.NET Core 1.1 and higher can use Get, which works with entire sections. Get can be more convenient than using Bind.
I bound the configuration in my Startup
method.
private Config Config { get; }
public Startup(IConfiguration Configuration)
{
Config = Configuration.Get<Config>();
}
This binds the appsettings
file:
{
"ConnectionStrings": {
"Accounts": "Server=localhost;Database=Accounts;Trusted_Connection=True;",
"test": "Server=localhost;Database=test;Trusted_Connection=True;",
"Client": "Server=localhost;Database={DYNAMICALLY_BOUND_CONTEXT};Trusted_Connection=True;",
"Support": "Server=localhost;Database=Support;Trusted_Connection=True;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Plugins": {
"SMS": {
"RouteMobile": {
"Scheme": "https",
"Host": "remote.host",
"Port": 84567,
"Path": "/bulksms",
"Username": "username",
"Password": "password",
"Source": "CompanyName",
"DeliveryReporting": true,
"MessageType": "Unicode"
}
},
"SMTP": {
"GenericSmtp": {
"Scheme": "https",
"Host": "mail.host",
"Port": 25,
"EnableSsl": true,
"Username": "[email protected]",
"Password": "password",
"DefaultSender": "[email protected]"
}
}
}
}
Into this configuration structure:
[DataContract]
public class Config
{
[DataMember]
public Dictionary<string, string> ConnectionStrings { get; set; }
[DataMember]
public PluginCollection Plugins { get; set; }
}
[DataContract]
public class PluginCollection
{
[DataMember]
public Dictionary<string, SmsConfiguration> Sms { get; set; }
[DataMember]
public Dictionary<string, EmailConfiguration> Smtp { get; set; }
}
[DataContract]
public class SmsConfiguration
{
[DataMember]
public string Scheme { get; set; }
[DataMember]
public string Host { get; set; }
[DataMember]
public int Port { get; set; }
[DataMember]
public string Path { get; set; }
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string Source { get; set; }
[DataMember]
public bool DeliveryReporting { get; set; }
[DataMember]
public string Encoding { get; set; }
}
[DataContract]
public class EmailConfiguration
{
[DataMember]
public string Scheme { get; set; }
[DataMember]
public string Host { get; set; }
[DataMember]
public int Port { get; set; }
[DataMember]
public string Path { get; set; }
[DataMember]
public string Username { get; set; }
[DataMember]
public string Password { get; set; }
[DataMember]
public string DefaultSender { get; set; }
[DataMember]
public bool EnableSsl { get; set; }
}
{ "foo": "bar", "openid email api://50095d53-b75e-4c70-951d-8e309168a22f/example": "example key" }
and interestingly the"foo": "bar"
key does come through but the second key does not. Is there a way to have any arbitrary key come through?