Created
May 27, 2021 16:14
-
-
Save ericsampson/ca679850137f4b6c6fc201d3bb9ca66e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Options; | |
using Microsoft.Extensions.DependencyInjection; | |
var config = new ConfigurationBuilder() | |
.AddInMemoryCollection(new Dictionary<string, string>() { { "Param", "yay!" } }) | |
.Build(); | |
var serviceProvider = new ServiceCollection() | |
.Configure<Options>(opt => config.Bind(opt)) // << this is the way >> | |
.BuildServiceProvider(); | |
Options options = serviceProvider.GetRequiredService<IOptions<Options>>().Value; // simulate receiving IOptions from DI | |
Console.WriteLine(options.Param); | |
Console.ReadLine(); | |
class Options { | |
public string Param { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment