Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 5 (vNext) - Configuration

Tags:

c#

asp.net

I am trying to learn ASP.NET 5. I am using it on Mac OS X. At this time, I have a config.json file that looks like the following:

config.json

{
    "AppSettings": {
        "Environment":"dev"
    },

    "DbSettings": {
        "AppConnectionString": "..."
    },

    "EmailSettings": {
        "EmailApiKey": "..."        
    },  
}

I am trying to figure out how to load these settings into a configuration file in Startup.cs. Currently, I have a file that looks like this:

Configuration.cs

public class AppConfiguration
{
    public AppSettings AppSettings { get; set; }

    public DbSettings DbSettings { get; set; }

    public EmailSettings EmailSettings { get; set; }        
}

public class AppSettings
{
    public string Environment { get; set; }
}

public class DbSettings
{
    public string AppConnectionString { get; set; }
}

public class EmailSettings
{
    public string MandrillApiKey { get; set; }
}

Then, in Startup.cs, I have the following:

public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
  var configuration = new Configuration().AddJsonFile("config.json");
  Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<AppConfiguration>(Configuration);
} 

However, this approach doesn't work. Basically, its like it doesn't know how to map between the .json and the Config classes. How do I do this?

I would really like to stay with the DI approach so I can test my app more effectively.

Thank you

like image 299
Some User Avatar asked Jun 09 '15 16:06

Some User


People also ask

What is vNext?

vNext uses the Roslyn compiler to compile code dynamically. You will be able to edit a code file, refresh the browser, and see the changes without rebuilding the project.

Is .NET easier than Java?

NET makes use of natively generated languages such as C# and C++. These are quicker and use less storage than Java. NET also allows for code optimization and writing code, which improves speed. The internet is simple (a very effective combination), and it is significantly easier to locate work in the Indian market.

Which is better .NET or Java?

Java fetches most syntax from C and C++. Since it is a platform-independent language, you can run Java on various platforms . Net works on a common language infrastructure, supports arrays, type checking, checks variables and garbage collection. Hence, it provides durability, productivity and robustness.

Can we use .NET with Java?

NET works only on the Windows operating system and its different versions. There are a few open-source versions of . NET but they still focus on Windows users. Java Virtual Machine allows Java to run its code on any platform with any operating system.


2 Answers

In your Startup class, first specify your configuration sources:

private readonly IConfiguration configuration;

public Startup(IHostingEnvironment env)
{
    configuration = new Configuration()
        .AddJsonFile("config.json")
        .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
}

The env.EnvironmentName is something like "development" or "production". Suppose it is "development". The framework will try to pull configuration values from config.development.json first. If the requested values aren't there, or the config.development.json file doesn't exist, then the framework will try to pull the values from config.json.

Configure your config.json sections as services like this:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(configuration.GetSubKey("AppSettings"));
    services.Configure<DbSettings>(configuration.GetSubKey("DbSettings"));
    services.Configure<EmailSettings>(configuration.GetSubKey("EmailSettings"));
}

Calling Configure<T>(IConfiguration) on the IServiceCollection registers the type IOptions<T> in the dependency injection container. Note that you don't need your AppConfiguration class.

Then specify dependencies to be injected like this:

public class EmailController : Controller
{
    private readonly IOptions<EmailSettings> emailSettings;

    public EmailController (IOptions<EmailSettings> emailSettings)
    {
        this.emailSettings = emailSettings;
    }

    public IActionResult Index()
    {
        string apiKey = emailSettings.Options.EmailApiKey;
        ...
    }
}
like image 53
Timothy Shields Avatar answered Sep 18 '22 17:09

Timothy Shields


You can simply access the value of config property Environment by using Configuration class like this anywhere in your application.

var environment = Configuration["AppSettings:Environment"];

But if you want it to access through a property of class you can do this

public class AppSettings 
{ 
 public string Environment
 { 
 get 
 { 
  return new Configuration().Get("AppSettings:Environment").ToString(); 
 }
 set; 
 } 
}

Note I haven't set the value of Environment in setter because this value will be in config.json so there is no need to set this value in setter.

For more details have a look at this article.

like image 40
Mairaj Ahmad Avatar answered Sep 21 '22 17:09

Mairaj Ahmad