[Minimal API docs] Adding an example of how to resolve dependencies just like in configure method (#23998)

Co-authored-by: Kirk Larkin <6025110+serpent5@users.noreply.github.com>
pull/24033/head
Swiftly1 2021-11-23 10:49:17 +01:00 committed by GitHub
parent a97adf91ba
commit 26cab4819b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

View File

@ -147,6 +147,14 @@ The following code writes a message to the log on application startup:
For more information, see <xref:fundamentals/logging/index?view=aspnetcore-6.0>
### Access the Dependency Injection (DI) container
The following code shows how to get services from the DI container during application startup:
[!code-csharp[](minimal-apis/samples/WebMinAPIs/Program.cs?name=snippet_dependencies)]
For more information, see <xref:fundamentals/dependency-injection?view=aspnetcore-6.0>.
## WebApplicationBuilder
This section contains sample code using <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder>.

View File

@ -1,4 +1,4 @@
#define Default // Default CREATE P1 PM PE I1 I0 IP CERT CERT2 CERT3 RE CONFIG LOG REB
#define Default // Default CREATE P1 PM PE I1 I0 IP CERT CERT2 CERT3 RE CONFIG LOG #i REB
// CONFIGB LOGB IWHB DEP R1 LE LF IM SM NR NR2 RP WILD CON OV EPB OP1 OP2 OP3 OP4
// CB BA CJSON MULTI STREAM XTN AUTH1 AUTH2 AUTH3 AUTH4 CORS CORS2 SWAG SWAG2
// FIL2 IHB CHNGR ADDMID
@ -207,6 +207,27 @@ app.MapGet("/", () => "Hello World");
app.Run();
#endregion
#elif DEPS
#region snippet_dependencies
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped<SampleService>();
var app = builder.Build();
app.MapControllers();
using (var scope = app.Services.CreateScope())
{
var sampleService = scope.ServiceProvider.GetRequiredService<SampleService>();
sampleService.DoSomething();
}
app.Run();
#endregion
class SampleService { public void DoSomething() { } }
#elif REB // Read Env Builder
#region snippet_reb
var builder = WebApplication.CreateBuilder(args);