ASP.NET Core Settings Dependency Injection

There’s a variety of different ways to access configuration settings from an appsettings.json file in a project, these are all well documented here so this post is mostly intended to serve as a reminder for the methods I most frequently use.

Dependency injection (DI) is a technique for achieving loose coupling between objects and their collaborators, or dependencies. In this case that means that rather than relying on a static reference to a settings class we declare a dependency on the configuration object in the classes constructor allowing us to access it throughout the class.

By default when creating a new ASP.NET Core Web Application DI for the configuration is already setup for you. This is done at the start of the Startup.cs script.


public class Startup
{
	public Startup(IConfiguration configuration)
	{
		Configuration = configuration;
	}

	public IConfiguration Configuration { get; }
	
	...
}

To access this configuration object in a controller it just needs to be added to the constructor and .NET will take care of the rest.


public class HomeController : Controller
{
	private readonly IConfiguration _config;
	
	public HomeController(IConfiguration config)
	{
		_config = config;
	}
	
	...
}

Values within the configuration object can then be accessed throughout the controller, some standard segments in appsettings.json such as connection strings have a dedicated method for retrieving them.


public IActionResult Index()
{
	List blogs = AppCode.Common.GetBlogSummaries(_config.GetConnectionString("BlogConnection"));
	return View(blogs);
}

If custom setting values are stored in appsettings.json they can be retrieved by specifying the path to them directly like so.


public IActionResult Index()
{
	List blogs = AppCode.Common.GetBlogSummaries(_config["ConnectionStrings:BlogConnection"].ToString());
	return View(blogs);
}

If you have a complicated structure for your settings in appsettings.json then rather than rather than accessing each setting value individually as above you can bind it to a POCO class. In order to be able to bind to you settings object the NuGet package Microsoft.Extensions.Configuration.Binder needs to be installed and referenced.

appsettings.json


{
	"Window": {
		"Name": "MyWindow",
		"Height": 11,
		"Width": 11,
		"Top": 50,
		"Left": 60,
	}
	
}

Window.cs


public class Window
{
	public string Name { get; set; }

	public int Height { get; set; }

	public int Width { get; set; }

	public int Top { get; set; }
	
	public int Left { get; set; }
}

HomeController.cs


public class HomeController : Controller
{
	private readonly IConfiguration _config;
	
	public HomeController(IConfiguration config)
	{
		_config = config;
	}
	
	[HttpGet]
	public async Task<IActionResult> Index()
	{
		Window window = new Window();
		
		configuration.GetSection("Window").Bind(window);
	
		return View(window.Name);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *