Merge pull request #777 from hishamco/remove-warning

Avoid Building ServiceProvider in ConfigureServices
This commit is contained in:
Shaun Walker 2020-10-03 15:52:04 -04:00 committed by GitHub
commit 40524300bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 35 deletions

View File

@ -15,10 +15,10 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class OqtaneServiceCollectionExtensions
{
public static IServiceCollection AddOqtane(this IServiceCollection services, Runtime runtime)
public static IServiceCollection AddOqtane(this IServiceCollection services, Runtime runtime, string[] supportedCultures)
{
LoadAssemblies();
LoadSatelliteAssemblies();
LoadSatelliteAssemblies(supportedCultures);
services.AddOqtaneServices(runtime);
return services;
@ -122,7 +122,7 @@ namespace Microsoft.Extensions.DependencyInjection
}
}
private static void LoadSatelliteAssemblies()
private static void LoadSatelliteAssemblies(string[] supportedCultures)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
@ -133,39 +133,35 @@ namespace Microsoft.Extensions.DependencyInjection
AssemblyLoadContext.Default.Resolving += ResolveDependencies;
using (var serviceScope = ServiceActivator.GetScope())
foreach (var culture in supportedCultures)
{
var localizationManager = serviceScope.ServiceProvider.GetService<ILocalizationManager>();
foreach (var culture in localizationManager.GetSupportedCultures())
if (culture == Constants.DefaultCulture)
{
if (culture == Constants.DefaultCulture)
continue;
}
var assembliesFolder = new DirectoryInfo(Path.Combine(assemblyPath, culture));
foreach (var assemblyFile in assembliesFolder.EnumerateFiles(Constants.StalliteAssemblyExtension))
{
AssemblyName assemblyName;
try
{
assemblyName = AssemblyName.GetAssemblyName(assemblyFile.FullName);
}
catch
{
Console.WriteLine($"Not Satellite Assembly : {assemblyFile.Name}");
continue;
}
var assembliesFolder = new DirectoryInfo(Path.Combine(assemblyPath, culture));
foreach (var assemblyFile in assembliesFolder.EnumerateFiles(Constants.StalliteAssemblyExtension))
try
{
AssemblyName assemblyName;
try
{
assemblyName = AssemblyName.GetAssemblyName(assemblyFile.FullName);
}
catch
{
Console.WriteLine($"Not Satellite Assembly : {assemblyFile.Name}");
continue;
}
try
{
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(assemblyFile.FullName)));
Console.WriteLine($"Loaded : {assemblyName}");
}
catch (Exception e)
{
Console.WriteLine($"Failed : {assemblyName}\n{e}");
}
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(assemblyFile.FullName)));
Console.WriteLine($"Loaded : {assemblyName}");
}
catch (Exception e)
{
Console.WriteLine($"Failed : {assemblyName}\n{e}");
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Net.Http;
@ -26,11 +27,14 @@ namespace Oqtane
{
public class Startup
{
public IConfigurationRoot Configuration { get; }
private static readonly string[] DefaultSupportedCultures = new[] { Constants.DefaultCulture };
private string _webRoot;
private Runtime _runtime;
private bool _useSwagger;
public IConfigurationRoot Configuration { get; }
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
@ -128,7 +132,10 @@ namespace Oqtane
.AddSignInManager()
.AddDefaultTokenProviders();
services.Configure<LocalizationOptions>(Configuration.GetSection("Localization"));
var localizationSection = Configuration.GetSection("Localization");
var localizationOptions = localizationSection.Get<LocalizationOptions>();
services.Configure<LocalizationOptions>(localizationSection);
services.Configure<IdentityOptions>(options =>
{
@ -202,11 +209,11 @@ namespace Oqtane
services.AddTransient<ISqlRepository, SqlRepository>();
services.AddTransient<IUpgradeManager, UpgradeManager>();
// TODO: Check if there's a better way instead of building service provider
ServiceActivator.Configure(services.BuildServiceProvider());
// load the external assemblies into the app domain, install services
services.AddOqtane(_runtime);
services.AddOqtane(_runtime,
localizationOptions.SupportedCultures.IsNullOrEmpty()
? DefaultSupportedCultures
: localizationOptions.SupportedCultures);
services.AddMvc()
.AddNewtonsoftJson()