Click here to Skip to main content
14,167,095 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

16.4K views
6 bookmarked
Posted 7 May 2015
Licenced CPOL

Accessing configuration variables using Dependency Injection in MVC 6

, 7 May 2015
Rate this:
Please Sign up or sign in to vote.
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:

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)

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.ConfigurationModel

 

 

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

License

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

Share

About the Author

Mostafa Asaduzzaman
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Questionerror 404 Pin
Member 1079367523-Sep-16 9:53
memberMember 1079367523-Sep-16 9:53 
Questionerror 404 Pin
Member 1079367523-Sep-16 9:50
memberMember 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190518.1 | Last Updated 7 May 2015
Article Copyright 2015 by Mostafa Asaduzzaman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid