From 915694b3feb366da70e20051eceaaeea7b7626f7 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Fri, 19 Nov 2021 08:40:17 -1000 Subject: [PATCH] fix sample (#23990) --- .../fundamentals/dependency-injection.md | 2 +- .../6.x/DIsample2/DIsample2/Program.cs | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) 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