Merge pull request #777 from hishamco/remove-warning
Avoid Building ServiceProvider in ConfigureServices
This commit is contained in:
commit
40524300bf
|
@ -15,10 +15,10 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
public static class OqtaneServiceCollectionExtensions
|
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();
|
LoadAssemblies();
|
||||||
LoadSatelliteAssemblies();
|
LoadSatelliteAssemblies(supportedCultures);
|
||||||
services.AddOqtaneServices(runtime);
|
services.AddOqtaneServices(runtime);
|
||||||
|
|
||||||
return services;
|
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 assemblies = AppDomain.CurrentDomain.GetAssemblies();
|
||||||
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
|
var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
|
||||||
|
@ -133,39 +133,35 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||||
|
|
||||||
AssemblyLoadContext.Default.Resolving += ResolveDependencies;
|
AssemblyLoadContext.Default.Resolving += ResolveDependencies;
|
||||||
|
|
||||||
using (var serviceScope = ServiceActivator.GetScope())
|
foreach (var culture in supportedCultures)
|
||||||
{
|
{
|
||||||
var localizationManager = serviceScope.ServiceProvider.GetService<ILocalizationManager>();
|
if (culture == Constants.DefaultCulture)
|
||||||
foreach (var culture in localizationManager.GetSupportedCultures())
|
|
||||||
{
|
{
|
||||||
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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var assembliesFolder = new DirectoryInfo(Path.Combine(assemblyPath, culture));
|
try
|
||||||
foreach (var assemblyFile in assembliesFolder.EnumerateFiles(Constants.StalliteAssemblyExtension))
|
|
||||||
{
|
{
|
||||||
AssemblyName assemblyName;
|
Assembly assembly = AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(assemblyFile.FullName)));
|
||||||
try
|
Console.WriteLine($"Loaded : {assemblyName}");
|
||||||
{
|
}
|
||||||
assemblyName = AssemblyName.GetAssemblyName(assemblyFile.FullName);
|
catch (Exception e)
|
||||||
}
|
{
|
||||||
catch
|
Console.WriteLine($"Failed : {assemblyName}\n{e}");
|
||||||
{
|
|
||||||
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}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
@ -26,11 +27,14 @@ namespace Oqtane
|
||||||
{
|
{
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public IConfigurationRoot Configuration { get; }
|
private static readonly string[] DefaultSupportedCultures = new[] { Constants.DefaultCulture };
|
||||||
|
|
||||||
private string _webRoot;
|
private string _webRoot;
|
||||||
private Runtime _runtime;
|
private Runtime _runtime;
|
||||||
private bool _useSwagger;
|
private bool _useSwagger;
|
||||||
|
|
||||||
|
public IConfigurationRoot Configuration { get; }
|
||||||
|
|
||||||
public Startup(IWebHostEnvironment env)
|
public Startup(IWebHostEnvironment env)
|
||||||
{
|
{
|
||||||
var builder = new ConfigurationBuilder()
|
var builder = new ConfigurationBuilder()
|
||||||
|
@ -128,7 +132,10 @@ namespace Oqtane
|
||||||
.AddSignInManager()
|
.AddSignInManager()
|
||||||
.AddDefaultTokenProviders();
|
.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 =>
|
services.Configure<IdentityOptions>(options =>
|
||||||
{
|
{
|
||||||
|
@ -202,11 +209,11 @@ namespace Oqtane
|
||||||
services.AddTransient<ISqlRepository, SqlRepository>();
|
services.AddTransient<ISqlRepository, SqlRepository>();
|
||||||
services.AddTransient<IUpgradeManager, UpgradeManager>();
|
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
|
// load the external assemblies into the app domain, install services
|
||||||
services.AddOqtane(_runtime);
|
services.AddOqtane(_runtime,
|
||||||
|
localizationOptions.SupportedCultures.IsNullOrEmpty()
|
||||||
|
? DefaultSupportedCultures
|
||||||
|
: localizationOptions.SupportedCultures);
|
||||||
|
|
||||||
services.AddMvc()
|
services.AddMvc()
|
||||||
.AddNewtonsoftJson()
|
.AddNewtonsoftJson()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user