diff --git a/aspnetcore/fundamentals/dependency-injection.md b/aspnetcore/fundamentals/dependency-injection.md index 0c45cbb258..22ebe5ecae 100644 --- a/aspnetcore/fundamentals/dependency-injection.md +++ b/aspnetcore/fundamentals/dependency-injection.md @@ -249,7 +249,7 @@ Service1.Dispose Consider the following code: -[!code-csharp[](dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs?name=snippet2)] +[!code-csharp[](dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs?name=snippet3)] In the preceding code: diff --git a/aspnetcore/fundamentals/dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs b/aspnetcore/fundamentals/dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs index 0247a619dd..c52f0e6d52 100644 --- a/aspnetcore/fundamentals/dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs +++ b/aspnetcore/fundamentals/dependency-injection/samples/6.x/DIsample2/DIsample2/Program.cs @@ -1,4 +1,4 @@ -#define SECOND // FIRST SECOND +#define THRD // FIRST SECOND THRD #if FIRST #region snippet using DIsample2.Services; @@ -61,5 +61,36 @@ app.UseAuthorization(); app.MapRazorPages(); +app.Run(); +#elif THRD +using DIsample2.Services; +#region snippet3 +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddRazorPages(); + +builder.Services.AddSingleton(new Service1()); +builder.Services.AddSingleton(new Service2()); +#endregion + +var myKey = builder.Configuration["MyKey"]; +builder.Services.AddSingleton(sp => new Service3(myKey)); + +var app = builder.Build(); +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/Error"); + app.UseHsts(); +} + +app.UseHttpsRedirection(); +app.UseStaticFiles(); + +app.UseRouting(); + +app.UseAuthorization(); + +app.MapRazorPages(); + app.Run(); #endif