Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an instance of IConfiguration in asp.net core?

Tags:

I making a unittesting project to test my webapi and i need to initialize a controller the problem is that in the constructor it receive a IConfiguration that it is provide by dependency-injection and works fine.

But when I want to initialize it manually i have not way to get this instance.

I am trying to initialize it from a unittest project no inside of the same project.

The controller looks like:

public Controller(IConfiguration configuration) { _configuration = configuration; } 
like image 892
Rabel Obispo Avatar asked Dec 29 '17 04:12

Rabel Obispo


People also ask

How do I get IConfiguration in .NET core?

Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.

How do I get value from IConfiguration?

Using IConfiguration The IConfiguration is available in the dependency injection (DI) container, so you can directly access JSON properties by simply injecting IConfiguration in the constructor of a controller or class. It represents a set of key/value application configuration properties.

How do you inject IConfiguration in net core 6?

You could add the IConfiguration instance to the service collection as a singleton object in ConfigureServices : public void ConfigureServices(IServiceCollection service) { services. AddSingleton<IConfiguration>(Configuration); //... }

What is Appsetting json in asp net core?

The appsettings. json file is generally used to store the application configuration settings such as database connection strings, any application scope global variables, and much other information.


1 Answers

I'd probably should start from the statement that in .Net Core application you should not pass instance of IConfiguration to your controllers or other classes. You should use strongly typed settings injected through IOptions<T>. See this article for more details: Options pattern in ASP.NET Core.

When using options pattern, you will have POCO for the settings required by a controller. This settings object is then injected into controller wrapped into IOptions<T>:

public class ControllerSettings {     public string Setting1 { get; set; }      public int Setting2 { get; set; }      // ... }  public class Controller {     private readonly ControllerSettings _settings;      public Controller(IOptions<ControllerSettings> options)     {         _settings = options.Value;     } } 

Then it's pretty simple to pass any settings you want from a Unit Test. Just fill settings instance and wrap to IOptions<T> with any of available mocking frameworks, like Moq or NSubstitute:

[TestMethod] public void SomeTest() {     var settings = new ControllerSettings     {         Setting1 = "Some Value",         Setting2 = 123,     };      var options = new Mock<IOptions<ControllerSettings>>();     options.Setup(x => x.Value).Returns(settings);      var controller = new Controller(options.Object);      //  ... } 

Sometimes it's required to use real configuration of the project, for example when developing integration test. In this case you could create instance of ConfigurationBuilder and fill it with the same configuration sources as in tested code:

IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); // Duplicate here any configuration sources you use. configurationBuilder.AddJsonFile("AppSettings.json"); IConfiguration configuration = configurationBuilder.Build(); 
like image 117
CodeFuller Avatar answered Oct 14 '22 23:10

CodeFuller