//this supports dependency injection by allowing the passing of any dependencies to this consuming code when it's constructed. //Since the HomeController now depends on an abstraction defined by the IWeatherForecaster interface, it is now decoupled from changes to the implementation, which gets injected public HomeController(IWeatherForecaster weatherForecaster, IOptions <FeaturesConfiguration> options) { _weatherForecaster = weatherForecaster; //Once bound, you can inject an instance of IOptions of T into any classes which need access to configuration values. _featuresConfiguration = options.Value; }
public override void Configure(IFunctionsHostBuilder builder) { IConfiguration configuration = new ConfigurationBuilder() .SetBasePath(Environment.CurrentDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); IConfigurationSection influxDBConfigurationSection = configuration.GetSection("InfluxDB"); IConfigurationSection featuresConfigurationSection = configuration.GetSection("Features"); IInfluxDBConfiguration influxDBConfiguration = new InfluxDBConfiguration(); IFeaturesConfiguration featuresConfiguration = new FeaturesConfiguration(); influxDBConfigurationSection.Bind(influxDBConfiguration); featuresConfigurationSection.Bind(featuresConfiguration); InfluxDBClientOptions influxDBClientOptions = InfluxDBClientOptions.Builder .CreateNew() .Url(influxDBConfiguration.Url) .AuthenticateToken(influxDBConfiguration.Token.ToCharArray()) .Org(influxDBConfiguration.Organisation) .Bucket(influxDBConfiguration.Bucket) .Build(); InfluxDBClient influxDBClient = InfluxDBClientFactory.Create(influxDBClientOptions); ISensorRepository sensorRepository = new SensorRepository(influxDBClient); builder.Services.AddSingleton <IFeaturesConfiguration>(featuresConfiguration); builder.Services.AddSingleton <ISensorRepository>(sensorRepository); builder.Services.AddSensorService(); builder.Services.AddFeatureService(); }
public HomeController( IWeatherForecaster weatherForecaster, IOptions <FeaturesConfiguration> featuresConfiguration) { _weatherForecaster = weatherForecaster; _featuresConfiguration = featuresConfiguration.Value; }
public HomeController(IWeatherForecaster weatherForecaster, IOptions <FeaturesConfiguration> options, ILogger <HomeController> logger) { _weatherForecaster = weatherForecaster; _featuresConfiguration = options.Value; _logger = logger; }
public IndexModel( IWeatherForecaster weatherForecaster, IOptions <FeaturesConfiguration> options, IHomePageGreetingService greetingService) { _weatherForecaster = weatherForecaster; _greetingService = greetingService; _featuresConfiguration = options.Value; }
public HomeController(INameParserService nameParserService, GuidService guidService, IOptions <FeaturesConfiguration> options, ILogger <HomeController> logger) { _logger = logger; _guidService = guidService; _nameParserService = nameParserService; _featuresConfiguration = options.Value; }
public HomeController(IConfiguration configuration, IOptions <FeaturesConfiguration> featuresConfiguration, IOptionsSnapshot <FeaturesConfiguration> featuresConfigurationSnapshot, IOptionsMonitor <FeaturesConfiguration> featuresConfigurationMonitor, IOptions <FeaturesConfigurationWithSimpleValidation> featuresConfigurationWithSimpleValidation) { _configuration = configuration; _featuresConfiguration = featuresConfiguration.Value; _featuresConfigurationSnapshot = featuresConfigurationSnapshot.Value; _featuresConfigurationMonitor = featuresConfigurationMonitor.CurrentValue; _featuresConfigurationWithSimpleValidation = featuresConfigurationWithSimpleValidation.Value; }
public HomeController(IWeatherForecaster weatherForecaster, IOptionsSnapshot <FeaturesConfiguration> options, // If we use IOptionsSnapshot, the configuration registered with scoped not singleton. //GuidGenerator guidGenerator, ILogger <HomeController> logger, IDistributedCache <WeatherResult> cache, IOptionsMonitor <SecondFeaturesConfiguration> secondFeatures) { _weatherForecaster = weatherForecaster; _featuresConfiguration = options.Value; // _guidGenerator = guidGenerator; _logger = logger; _cache = cache; _secondFeatures = secondFeatures.CurrentValue; // IOptionsMonitor also provide us OnChange() method. secondFeatures.OnChange(config => { _secondFeatures = config; _logger.LogInformation("The greeting configuration has been updated."); }); }
public WeatherForecastController(IWeatherForecaster weatherForecaster, IOptions <FeaturesConfiguration> options) { _weatherForecaster = weatherForecaster; _featuresConfiguration = options.Value; }