Skip to content

Instantly share code, notes, and snippets.

@rondefreitas
Last active May 11, 2016 18:36
Show Gist options
  • Save rondefreitas/ba66505089a2784b7483167918bb5e56 to your computer and use it in GitHub Desktop.
Save rondefreitas/ba66505089a2784b7483167918bb5e56 to your computer and use it in GitHub Desktop.
Example on how to use a POCO Settings class in ASP.NET Core
public interface IGoogleApiRequest
{
string ShowMyKey();
}
public class GoogleApiRequest : IGoogleApiRequest
{
private readonly IGoogleApiSettings _settings;
public GoogleApiRequest(IGoogleApiSettings settings) {
if(settings == null) throw new ArgumentNullException(nameof(settings));
_settings = settings;
}
public string ShowMyKey()
{
return $"Here's your key: {_settings.ApiKey}";
}
}
public interface IGoogleApiSettings
{
string ApiKey { get; set; }
bool EnablePlacesApi { get; set; }
}
public class GoogleApiSettings
{
public string ApiKey { get; set; }
public bool EnablePlacesApi { get; set; } = true;
}
REM this is an example of how you add your secret
REM make sure you've reviewed the docs at http://docs.asp.net/en/latest/security/app-secrets.html
REM or at http://go.microsoft.com/fwlink/?LinkID=532709
user-secret set GoogleApiSettings:ApiKey YOUR-GOOGLE-API-KEY-HERE
{
"AppSettings": {
"SomeSetting": "???"
},
"GoogleApiSettings": {
"ApiKey": "SINCE WE ARE USING USER SECRETS THIS SHOULD NEVER BE THE VALUE",
"EnablePlacesApi": false
},
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=localhost\\sqlexpress;Database=WebApp;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}
public class HomeController : Controller {
private IGoogleApiRequest _googleApiRequest;
public HomeController(IGoogleApiRequest googleApiRequest)
{
_googleApiRequest = googleApiRequest;
}
// if you do constructor injection
public IActionResult ApiKeyFromConstructor()
{
return Content(_googleApiRequest.ShowMyKey());
}
// if you do method body injection
public IActionResult ApiKeyFromMethodBody([FromServices]IGoogleApiRequest fromServicesGoogleApiRequest)
{
return Content(fromServicesGoogleApiRequest.ShowMyKey());
}
}
// make sure to add this dependency:
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
using ClassLibrary;
// warning, incomplete class - provided for reference only
public partial class Startup
{
public Startup()
{
// perform normal initialization from appsettings, etc
// make sure to use this for user secrets
if (hostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// do other things
var googleSettingsSection = Configuration.GetSection("GoogleApiSettings");
// one way to do it - personally I prefer this over the IOptions API
services.AddTransient<IGoogleApiSettings>(provider => googleSettingsSection.Get<GoogleApiSettings>());
// or if you really want to make use of IOptions...
services.AddOptions().Configure<GoogleApiSettings>(googleSettingsSection);
// you can then override the construction of your injected service
services.AddTransient<IGoogleApiRequest>(
provider =>
{
var options = provider.GetService<IOptions<GoogleApiSettings>>()?.Value;
return new GoogleApiRequest(options);
});
// do more things
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment