Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Prev Previous commit
Next Next commit
Use built-in GetRequiredService<T>
  • Loading branch information
khellang authored and SteveSandersonMS committed Oct 4, 2017
commit d466cdb1d7ae395f8548f5644769895587f6b68d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.NodeServices;
using Microsoft.AspNetCore.SpaServices;
using Microsoft.AspNetCore.SpaServices.Prerendering;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;

Expand Down Expand Up @@ -41,9 +42,9 @@ public static void UsePrerendering(
var appBuilder = spaBuilder.AppBuilder;
var serviceProvider = appBuilder.ApplicationServices;
var nodeServices = GetNodeServices(serviceProvider);
var applicationStoppingToken = GetRequiredService<IApplicationLifetime>(serviceProvider)
var applicationStoppingToken = serviceProvider.GetRequiredService<IApplicationLifetime>()
.ApplicationStopping;
var applicationBasePath = GetRequiredService<IHostingEnvironment>(serviceProvider)
var applicationBasePath = serviceProvider.GetRequiredService<IHostingEnvironment>()
.ContentRootPath;
var moduleExport = new JavaScriptModuleExport(entryPoint);

Expand Down Expand Up @@ -92,12 +93,6 @@ public static void UsePrerendering(
});
}

private static T GetRequiredService<T>(IServiceProvider serviceProvider) where T: class
{
return (T)serviceProvider.GetService(typeof(T))
?? throw new Exception($"Could not resolve service of type {typeof(T).FullName} in service provider.");
}

private static async Task ApplyRenderResult(HttpContext context, RenderToStringResult renderResult)
{
if (!string.IsNullOrEmpty(renderResult.RedirectUrl))
Expand Down