I am trying to learn ASP.NET 5. I am using it on Mac OS X. At this time, I have a config.json file that looks like the following:
config.json
{
"AppSettings": {
"Environment":"dev"
},
"DbSettings": {
"AppConnectionString": "..."
},
"EmailSettings": {
"EmailApiKey": "..."
},
}
I am trying to figure out how to load these settings into a configuration file in Startup.cs. Currently, I have a file that looks like this:
Configuration.cs
public class AppConfiguration
{
public AppSettings AppSettings { get; set; }
public DbSettings DbSettings { get; set; }
public EmailSettings EmailSettings { get; set; }
}
public class AppSettings
{
public string Environment { get; set; }
}
public class DbSettings
{
public string AppConnectionString { get; set; }
}
public class EmailSettings
{
public string MandrillApiKey { get; set; }
}
Then, in Startup.cs, I have the following:
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
var configuration = new Configuration().AddJsonFile("config.json");
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppConfiguration>(Configuration);
}
However, this approach doesn't work. Basically, its like it doesn't know how to map between the .json and the Config classes. How do I do this?
I would really like to stay with the DI approach so I can test my app more effectively.
Thank you
vNext uses the Roslyn compiler to compile code dynamically. You will be able to edit a code file, refresh the browser, and see the changes without rebuilding the project.
NET makes use of natively generated languages such as C# and C++. These are quicker and use less storage than Java. NET also allows for code optimization and writing code, which improves speed. The internet is simple (a very effective combination), and it is significantly easier to locate work in the Indian market.
Java fetches most syntax from C and C++. Since it is a platform-independent language, you can run Java on various platforms . Net works on a common language infrastructure, supports arrays, type checking, checks variables and garbage collection. Hence, it provides durability, productivity and robustness.
NET works only on the Windows operating system and its different versions. There are a few open-source versions of . NET but they still focus on Windows users. Java Virtual Machine allows Java to run its code on any platform with any operating system.
In your Startup
class, first specify your configuration sources:
private readonly IConfiguration configuration;
public Startup(IHostingEnvironment env)
{
configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
}
The env.EnvironmentName
is something like "development"
or "production"
. Suppose it is "development"
. The framework will try to pull configuration values from config.development.json
first. If the requested values aren't there, or the config.development.json
file doesn't exist, then the framework will try to pull the values from config.json
.
Configure your config.json
sections as services like this:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(configuration.GetSubKey("AppSettings"));
services.Configure<DbSettings>(configuration.GetSubKey("DbSettings"));
services.Configure<EmailSettings>(configuration.GetSubKey("EmailSettings"));
}
Calling Configure<T>(IConfiguration)
on the IServiceCollection
registers the type IOptions<T>
in the dependency injection container. Note that you don't need your AppConfiguration
class.
Then specify dependencies to be injected like this:
public class EmailController : Controller
{
private readonly IOptions<EmailSettings> emailSettings;
public EmailController (IOptions<EmailSettings> emailSettings)
{
this.emailSettings = emailSettings;
}
public IActionResult Index()
{
string apiKey = emailSettings.Options.EmailApiKey;
...
}
}
You can simply access the value of config property Environment
by using Configuration
class like this anywhere in your application.
var environment = Configuration["AppSettings:Environment"];
But if you want it to access through a property of class you can do this
public class AppSettings
{
public string Environment
{
get
{
return new Configuration().Get("AppSettings:Environment").ToString();
}
set;
}
}
Note I haven't set the value of Environment
in setter because this value will be in config.json
so there is no need to set this value in setter.
For more details have a look at this article.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With