Skip to content

Commit

Permalink
Switch to using NuGet package for listing all registered services
Browse files Browse the repository at this point in the history
  • Loading branch information
efleming18 committed Apr 6, 2019
1 parent 4ad595b commit 05767d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
45 changes: 11 additions & 34 deletions src/Web/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Ardalis.ListStartupServices;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -23,9 +24,9 @@
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;

namespace Microsoft.eShopWeb.Web
{
Expand Down Expand Up @@ -133,6 +134,13 @@ public void ConfigureServices(IServiceCollection services)
.AddCheck<HomePageHealthCheck>("home_page_health_check")
.AddCheck<ApiHealthCheck>("api_health_check");

services.Configure<ServiceConfig>(config =>
{
config.Services = new List<ServiceDescriptor>(services);

config.Path = "/allservices";
});

_services = services; // used to debug registered services
}

Expand Down Expand Up @@ -200,7 +208,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGene
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
ListAllRegisteredServices(app, linkGenerator);
app.UseShowAllServicesMiddleware();
app.UseDatabaseErrorPage();
}
else
Expand Down Expand Up @@ -237,36 +245,5 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, LinkGene
template: "{controller:slugify=Home}/{action:slugify=Index}/{id?}");
});
}

private void ListAllRegisteredServices(IApplicationBuilder app, LinkGenerator linkGenerator)
{
var homePageLink = linkGenerator.GetPathByAction("Index", "Catalog");
var loginLink = linkGenerator.GetPathByAction("SignIn", "Account");
app.Map("/allservices", builder => builder.Run(async context =>
{
var sb = new StringBuilder();
sb.Append("<a href=\"");
sb.Append(homePageLink);
sb.AppendLine("\">Return to site</a> | ");
sb.Append("<a href=\"");
sb.Append(loginLink);
sb.AppendLine("\">Login to site</a>");
sb.Append("<h1>All Services</h1>");
sb.Append("<table><thead>");
sb.Append("<tr><th>Type</th><th>Lifetime</th><th>Instance</th></tr>");
sb.Append("</thead><tbody>");
foreach (var svc in _services)
{
sb.Append("<tr>");
sb.Append($"<td>{svc.ServiceType.FullName}</td>");
sb.Append($"<td>{svc.Lifetime}</td>");
sb.Append($"<td>{svc.ImplementationType?.FullName}</td>");
sb.Append("</tr>");
}
sb.Append("</tbody></table>");
await context.Response.WriteAsync(sb.ToString());
}));
}

}
}
1 change: 1 addition & 0 deletions src/Web/Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


<ItemGroup>
<PackageReference Include="Ardalis.ListStartupServices" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.0" />
Expand Down

0 comments on commit 05767d6

Please sign in to comment.