Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core RC2 Configure Custom AppSettings

Say I put the settings below in appsettings.json.

"MySettings": {
    "SmtpHost": "smtp.mydomain.com"",
    "WebService": "http://localhost:1337"
}

And I have the class below to hold those settings.

public class MySettings
{
    public string SmtpHost{ get; set; }
    public string WebService{ get; set; }
}

With RC1 I would use the line of code below in the ConfigureServices() method to load those configuration settings.

services.Configure<MySettings>(Configuration.GetSection("MySettings"));

But in RC2 that same line of code gives me this error

Cannot convert from 'MicrosoftExtensions.Configuration.IConfigurationSection' to 'System.Action<MySettings>'.

like image 686
Clint B Avatar asked May 25 '16 13:05

Clint B


2 Answers

You simply need to reference a different package with RC2. In your project.json simply add a reference to the "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0-rc2-final" package, and you'll get the correct extension method that you're looking for.

"dependencies": {
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0-rc2-final"
}

Like several of the changes with ASP.NET Core RC2, there was a lot of re-packing and moving of things. I put together a migration guide that you might find useful.

like image 93
David Pine Avatar answered Nov 11 '22 20:11

David Pine


you need to add the package:

"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0-rc2-final",

and make sure you have this using:

using Microsoft.Extensions.Configuration;
like image 3
Joe Audette Avatar answered Nov 11 '22 18:11

Joe Audette