Skip to content

Commit d07b984

Browse files
committed
Issue dotnet-architecture#9 - Do not hold the ASP.NET Core Configuration object as Singleton in the IoC container
1 parent 0a18af1 commit d07b984

11 files changed

Lines changed: 18 additions & 18 deletions

File tree

src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class RedisBasketRepository : IBasketRepository
1818
private ConnectionMultiplexer _redis;
1919

2020

21-
public RedisBasketRepository(IOptions<BasketSettings> options, ILoggerFactory loggerFactory)
21+
public RedisBasketRepository(IOptionsSnapshot<BasketSettings> options, ILoggerFactory loggerFactory)
2222
{
2323
_settings = options.Value;
2424
_logger = loggerFactory.CreateLogger<RedisBasketRepository>();

src/Services/Basket/Basket.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void ConfigureServices(IServiceCollection services)
4444
//that cost to startup instead of having the first request pay the
4545
//penalty.
4646
services.AddSingleton<ConnectionMultiplexer>((sp) => {
47-
var config = sp.GetRequiredService<IOptions<BasketSettings>>().Value;
47+
var config = sp.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
4848
var ips = Dns.GetHostAddressesAsync(config.ConnectionString).Result;
4949
return ConnectionMultiplexer.Connect(ips.First().ToString());
5050
});

src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.Controllers
1414
public class CatalogController : ControllerBase
1515
{
1616
private readonly CatalogContext _context;
17-
private readonly IOptions<Settings> _settings;
17+
private readonly IOptionsSnapshot<Settings> _settings;
1818

19-
public CatalogController(CatalogContext context, IOptions<Settings> settings)
19+
public CatalogController(CatalogContext context, IOptionsSnapshot<Settings> settings)
2020
{
2121
_context = context;
2222
_settings = settings;

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ public Startup(IHostingEnvironment env)
3838

3939
public void ConfigureServices(IServiceCollection services)
4040
{
41-
services.AddSingleton<IConfiguration>(Configuration);
42-
4341
services.AddDbContext<CatalogContext>(c =>
4442
{
4543
c.UseSqlServer(Configuration["ConnectionString"]);

src/Services/Identity/Identity.API/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ namespace IdentityServer4.Quickstart.UI.Controllers
1515
public class HomeController : Controller
1616
{
1717
private readonly IIdentityServerInteractionService _interaction;
18-
private readonly IOptions<AppSettings> _settings;
18+
private readonly IOptionsSnapshot<AppSettings> _settings;
1919
private readonly IRedirectService _redirectSvc;
2020

21-
public HomeController(IIdentityServerInteractionService interaction, IOptions<AppSettings> settings,IRedirectService redirectSvc)
21+
public HomeController(IIdentityServerInteractionService interaction, IOptionsSnapshot<AppSettings> settings,IRedirectService redirectSvc)
2222
{
2323
_interaction = interaction;
2424
_settings = settings;

src/Web/WebMVC/Services/BasketService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
1414
{
1515
public class BasketService : IBasketService
1616
{
17-
private readonly IOptions<AppSettings> _settings;
17+
private readonly IOptionsSnapshot<AppSettings> _settings;
1818
private HttpClient _apiClient;
1919
private readonly string _remoteServiceBaseUrl;
2020
private IHttpContextAccessor _httpContextAccesor;
2121

22-
public BasketService(IOptions<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
22+
public BasketService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
2323
{
2424
_settings = settings;
2525
_remoteServiceBaseUrl = _settings.Value.BasketUrl;

src/Web/WebMVC/Services/CatalogService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
1515
{
1616
public class CatalogService : ICatalogService
1717
{
18-
private readonly IOptions<AppSettings> _settings;
18+
private readonly IOptionsSnapshot<AppSettings> _settings;
1919
private HttpClient _apiClient;
2020
private readonly string _remoteServiceBaseUrl;
2121

22-
public CatalogService(IOptions<AppSettings> settings, ILoggerFactory loggerFactory) {
22+
public CatalogService(IOptionsSnapshot<AppSettings> settings, ILoggerFactory loggerFactory) {
2323
_settings = settings;
2424
_remoteServiceBaseUrl = $"{_settings.Value.CatalogUrl}/api/v1/catalog/";
2525

src/Web/WebMVC/Services/OrderingService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public class OrderingService : IOrderingService
1515
{
1616
private HttpClient _apiClient;
1717
private readonly string _remoteServiceBaseUrl;
18-
private readonly IOptions<AppSettings> _settings;
18+
private readonly IOptionsSnapshot<AppSettings> _settings;
1919
private readonly IHttpContextAccessor _httpContextAccesor;
2020

21-
public OrderingService(IOptions<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
21+
public OrderingService(IOptionsSnapshot<AppSettings> settings, IHttpContextAccessor httpContextAccesor)
2222
{
2323
_remoteServiceBaseUrl = $"{settings.Value.OrderingUrl}/api/v1/orders";
2424
_settings = settings;

src/Web/WebMVC/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"System.IdentityModel.Tokens.Jwt": "5.0.0",
3636
"Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.0.0",
3737
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
38-
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0"
38+
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
39+
"Microsoft.Extensions.Options": "1.1.0"
3940
},
4041
"tools": {
4142
"BundlerMinifier.Core": "2.0.238",

src/Web/WebSPA/eShopOnContainers.WebSPA/Server/Controllers/HomeController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ namespace eShopConContainers.WebSPA.Server.Controllers
1111
public class HomeController : Controller
1212
{
1313
private readonly IHostingEnvironment _env;
14-
private readonly IOptions<AppSettings> _settings;
14+
private readonly IOptionsSnapshot<AppSettings> _settings;
1515

16-
public HomeController(IHostingEnvironment env, IOptions<AppSettings> settings)
16+
public HomeController(IHostingEnvironment env, IOptionsSnapshot<AppSettings> settings)
1717
{
1818
_env = env;
1919
_settings = settings;

0 commit comments

Comments
 (0)