Introduction
ASP.NET 5 with the Visual Studio 2015 RC along with other CTP releases provide efficiency way to hand dependencies via injecting through the interfaces. In this short tip, I will explore how to access the configuration variables from config.json via injecting the IConfiguration service.
Background
ASP.NET 5 no more uses web.config or machine.config for storing and using the configuration variables for the application. Instead it uses config.json file to define/store all the variables needed for the application. Default config.json file included in the project for Entity Framework has the initial settings for the database connection string as below:
Individual entities in config.json may appear as name-value pairs or rich objects. For our purpose we will use name-value pairs to define the string variables which will be accessed from the controller via Injection service. Entities in config.json can also refer to other objects.
Dependency Injection (DI) in ASP.NET 5
Dependency Injection (DI) is a popular software design pattern particularly efficient in the case of Inversion of Control, in which one or more dependencies are injected into dependent objects. The pattern is used to create program designs that are loosely coupled and testable. In ASP.NET 5 dependency injection (DI) is a first class citizen. A minimalistic DI container has been provided out of the box but there are scopes to bring out custom containers if the default one does not serve the purposes.
To know more about DI in ASP.NET 5/vNext, please have a look the MSDN blog at:
http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
Description
To access configuration variables from config.json we need to inject IConfiguration interface. We also need makes changes to our controller methods. This can be done in three steps;
Step 1:
Add the configuration variables in config.json in name-value pairs as below:
Step 2 :
ASP.NET needs to know what to return when a constructor requires an instance of IConfiguration. This can be defined in Startup.cs as singleton, as we don't need to change it thoughout the application life cycle. This can be done as adding new service to ConfigurationServices() method in Startup.cs
services.AddSingleton(_ =>Configuration)Colourised in 1ms
I have added it in Startup.cs as below:
Step 3 :
In this step we need to specify that our controller expects an IConfiguration instance through the constructor.
Following the Explicit Dependency Principle ASP.NET 5's built-in support will allow Dependency Inject functioning correctly. I have assigned the instance to a local variable and then calling the Get method on the instance to access the configuration as below:
We need to include the namespace in our controller code.
using Microsoft.Framework.ConfigurationModelColourised in 0ms
Having done this I have created a simple view to display the two variables as below:
After running the application and navigating to the Test page we can see the two variables are displayed as expected.
Points of Interest
ASP.NET 5, C#6.0 and Visual Studio 2015 RC