Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OptionsPatternMvc.Example.Settings;

namespace OptionsPatternMvc.Example.Controllers
{
Expand All @@ -17,15 +19,22 @@ public class WeatherForecastController : ControllerBase
};

private readonly ILogger<WeatherForecastController> _logger;
private readonly ExampleAppSettings _exampleAppSettings;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
public WeatherForecastController(
ILogger<WeatherForecastController> logger,
IOptions<ExampleAppSettings> exampleAppSettingsAccessor
)
{
_logger = logger;
_exampleAppSettings = exampleAppSettingsAccessor.Value;
}

[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
Debug.WriteLine($"Settings.Name={_exampleAppSettings.Name}");

var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Expand Down
10 changes: 10 additions & 0 deletions examples/OptionsPatternMvc.Example/Settings/ExampleAppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;

namespace OptionsPatternMvc.Example.Settings
{
public class ExampleAppSettings
{
[Required]
public string Name { get; set; }
}
}
4 changes: 4 additions & 0 deletions examples/OptionsPatternMvc.Example/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OptionsPatternMvc.Example.Extensions;
using OptionsPatternMvc.Example.Settings;

namespace OptionsPatternMvc.Example
{
Expand All @@ -25,6 +27,8 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddValidatedSettings<ExampleAppSettings>(Configuration);

services.AddControllers();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Options" Version="3.1.2" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>

</Project>