Click here to Skip to main content
15,881,516 members
Articles / Web Development / ASP.NET
Tip/Trick

Accessing configuration variables using Dependency Injection in MVC 6

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 May 2015CPOL3 min read 24.6K   6   2
How to access configuration variables via dependency injection in MVC 6 - Visual Studio 2015 RC

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:

Image 1

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:

Image 2

 

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

C++
services.AddSingleton(_ =>Configuration)

I have added it in Startup.cs as below:

Image 3

 

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.

C++
using Microsoft.Framework.ConfigurationModel

 

Image 4

 

Having done this I have created a simple view to display the two variables as below:

 

Image 5

After running the application and navigating to the Test page we can see the two variables are displayed as expected.

Image 6

 

Points of Interest

ASP.NET 5, C#6.0 and Visual Studio 2015 RC

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionerror 404 Pin
Member 1079367523-Sep-16 9:53
Member 1079367523-Sep-16 9:53 
Questionerror 404 Pin
Member 1079367523-Sep-16 9:50
Member 1079367523-Sep-16 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.