Skip to content

Commit 0a5735c

Browse files
committed
optionsbuilder scoped example
1 parent 54fe9e0 commit 0a5735c

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.Options;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
builder.Services.AddOptions<MySettings>()
6+
.Configure<IServiceProvider>((opts, provider) =>
7+
{
8+
using var scope = provider.CreateScope();
9+
var service = scope.ServiceProvider.GetRequiredService<ValueService>();
10+
opts.MyValue = service.GetValue();
11+
});
12+
13+
builder.Services.AddScoped<ValueService>();
14+
15+
var app = builder.Build();
16+
17+
app.MapGet("/", (IOptionsSnapshot<MySettings> settings, ValueService service) => new {
18+
mySettings = settings.Value.MyValue,
19+
service = service.GetValue(),
20+
});
21+
22+
app.Run();
23+
24+
public class ValueService
25+
{
26+
private readonly Guid _val = Guid.NewGuid();
27+
28+
// Return a fixed Guid for the lifetime of the service
29+
public Guid GetValue() => _val;
30+
}
31+
32+
public class MySettings
33+
{
34+
public Guid MyValue { get; set; }
35+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:41318",
7+
"sslPort": 44306
8+
}
9+
},
10+
"profiles": {
11+
"http": {
12+
"commandName": "Project",
13+
"dotnetRunMessages": true,
14+
"launchBrowser": true,
15+
"applicationUrl": "http://localhost:5284",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"https": {
21+
"commandName": "Project",
22+
"dotnetRunMessages": true,
23+
"launchBrowser": true,
24+
"applicationUrl": "https://localhost:7209;http://localhost:5284",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
},
29+
"IIS Express": {
30+
"commandName": "IISExpress",
31+
"launchBrowser": true,
32+
"environmentVariables": {
33+
"ASPNETCORE_ENVIRONMENT": "Development"
34+
}
35+
}
36+
}
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)