Refactor Program.cs
This commit is contained in:
parent
e7f5fe9827
commit
f7d8888232
|
@ -0,0 +1,52 @@
|
||||||
|
using Microsoft.AspNetCore.Components.Authorization;
|
||||||
|
using Oqtane.Providers;
|
||||||
|
using Oqtane.Services;
|
||||||
|
using Oqtane.Shared;
|
||||||
|
|
||||||
|
namespace Microsoft.Extensions.DependencyInjection
|
||||||
|
{
|
||||||
|
public static class OqtaneServiceCollectionExtensions
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddOqtaneAuthorization(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddAuthorizationCore();
|
||||||
|
services.AddScoped<IdentityAuthenticationStateProvider>();
|
||||||
|
services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<IdentityAuthenticationStateProvider>());
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static IServiceCollection AddOqtaneScopedServices(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddScoped<SiteState>();
|
||||||
|
services.AddScoped<IInstallationService, InstallationService>();
|
||||||
|
services.AddScoped<IModuleDefinitionService, ModuleDefinitionService>();
|
||||||
|
services.AddScoped<IThemeService, ThemeService>();
|
||||||
|
services.AddScoped<IAliasService, AliasService>();
|
||||||
|
services.AddScoped<ITenantService, TenantService>();
|
||||||
|
services.AddScoped<ISiteService, SiteService>();
|
||||||
|
services.AddScoped<IPageService, PageService>();
|
||||||
|
services.AddScoped<IModuleService, ModuleService>();
|
||||||
|
services.AddScoped<IPageModuleService, PageModuleService>();
|
||||||
|
services.AddScoped<IUserService, UserService>();
|
||||||
|
services.AddScoped<IProfileService, ProfileService>();
|
||||||
|
services.AddScoped<IRoleService, RoleService>();
|
||||||
|
services.AddScoped<IUserRoleService, UserRoleService>();
|
||||||
|
services.AddScoped<ISettingService, SettingService>();
|
||||||
|
services.AddScoped<IPackageService, PackageService>();
|
||||||
|
services.AddScoped<ILogService, LogService>();
|
||||||
|
services.AddScoped<IJobService, JobService>();
|
||||||
|
services.AddScoped<IJobLogService, JobLogService>();
|
||||||
|
services.AddScoped<INotificationService, NotificationService>();
|
||||||
|
services.AddScoped<IFolderService, FolderService>();
|
||||||
|
services.AddScoped<IFileService, FileService>();
|
||||||
|
services.AddScoped<ISiteTemplateService, SiteTemplateService>();
|
||||||
|
services.AddScoped<ISqlService, SqlService>();
|
||||||
|
services.AddScoped<ISystemService, SystemService>();
|
||||||
|
services.AddScoped<ILocalizationService, LocalizationService>();
|
||||||
|
services.AddScoped<ILanguageService, LanguageService>();
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,14 +8,12 @@ using System.Net.Http;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.Loader;
|
using System.Runtime.Loader;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Components.Authorization;
|
|
||||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||||
using Microsoft.AspNetCore.Localization;
|
using Microsoft.AspNetCore.Localization;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.JSInterop;
|
using Microsoft.JSInterop;
|
||||||
using Oqtane.Interfaces;
|
using Oqtane.Interfaces;
|
||||||
using Oqtane.Modules;
|
using Oqtane.Modules;
|
||||||
using Oqtane.Providers;
|
|
||||||
using Oqtane.Services;
|
using Oqtane.Services;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
using Oqtane.UI;
|
using Oqtane.UI;
|
||||||
|
@ -28,7 +26,8 @@ namespace Oqtane.Client
|
||||||
{
|
{
|
||||||
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
var builder = WebAssemblyHostBuilder.CreateDefault(args);
|
||||||
builder.RootComponents.Add<App>("app");
|
builder.RootComponents.Add<App>("app");
|
||||||
HttpClient httpClient = new HttpClient {BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)};
|
|
||||||
|
var httpClient = new HttpClient {BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)};
|
||||||
|
|
||||||
builder.Services.AddSingleton(httpClient);
|
builder.Services.AddSingleton(httpClient);
|
||||||
builder.Services.AddOptions();
|
builder.Services.AddOptions();
|
||||||
|
@ -37,38 +36,10 @@ namespace Oqtane.Client
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
|
||||||
// register auth services
|
// register auth services
|
||||||
builder.Services.AddAuthorizationCore();
|
builder.Services.AddOqtaneAuthorization();
|
||||||
builder.Services.AddScoped<IdentityAuthenticationStateProvider>();
|
|
||||||
builder.Services.AddScoped<AuthenticationStateProvider>(s => s.GetRequiredService<IdentityAuthenticationStateProvider>());
|
|
||||||
|
|
||||||
// register scoped core services
|
// register scoped core services
|
||||||
builder.Services.AddScoped<SiteState>();
|
builder.Services.AddOqtaneScopedServices();
|
||||||
builder.Services.AddScoped<IInstallationService, InstallationService>();
|
|
||||||
builder.Services.AddScoped<IModuleDefinitionService, ModuleDefinitionService>();
|
|
||||||
builder.Services.AddScoped<IThemeService, ThemeService>();
|
|
||||||
builder.Services.AddScoped<IAliasService, AliasService>();
|
|
||||||
builder.Services.AddScoped<ITenantService, TenantService>();
|
|
||||||
builder.Services.AddScoped<ISiteService, SiteService>();
|
|
||||||
builder.Services.AddScoped<IPageService, PageService>();
|
|
||||||
builder.Services.AddScoped<IModuleService, ModuleService>();
|
|
||||||
builder.Services.AddScoped<IPageModuleService, PageModuleService>();
|
|
||||||
builder.Services.AddScoped<IUserService, UserService>();
|
|
||||||
builder.Services.AddScoped<IProfileService, ProfileService>();
|
|
||||||
builder.Services.AddScoped<IRoleService, RoleService>();
|
|
||||||
builder.Services.AddScoped<IUserRoleService, UserRoleService>();
|
|
||||||
builder.Services.AddScoped<ISettingService, SettingService>();
|
|
||||||
builder.Services.AddScoped<IPackageService, PackageService>();
|
|
||||||
builder.Services.AddScoped<ILogService, LogService>();
|
|
||||||
builder.Services.AddScoped<IJobService, JobService>();
|
|
||||||
builder.Services.AddScoped<IJobLogService, JobLogService>();
|
|
||||||
builder.Services.AddScoped<INotificationService, NotificationService>();
|
|
||||||
builder.Services.AddScoped<IFolderService, FolderService>();
|
|
||||||
builder.Services.AddScoped<IFileService, FileService>();
|
|
||||||
builder.Services.AddScoped<ISiteTemplateService, SiteTemplateService>();
|
|
||||||
builder.Services.AddScoped<ISqlService, SqlService>();
|
|
||||||
builder.Services.AddScoped<ISystemService, SystemService>();
|
|
||||||
builder.Services.AddScoped<ILocalizationService, LocalizationService>();
|
|
||||||
builder.Services.AddScoped<ILanguageService, LanguageService>();
|
|
||||||
|
|
||||||
await LoadClientAssemblies(httpClient);
|
await LoadClientAssemblies(httpClient);
|
||||||
|
|
||||||
|
@ -76,49 +47,18 @@ namespace Oqtane.Client
|
||||||
foreach (var assembly in assemblies)
|
foreach (var assembly in assemblies)
|
||||||
{
|
{
|
||||||
// dynamically register module services
|
// dynamically register module services
|
||||||
var implementationTypes = assembly.GetInterfaces<IService>();
|
RegisterModuleServices(assembly, builder.Services);
|
||||||
foreach (var implementationType in implementationTypes)
|
|
||||||
{
|
|
||||||
if (implementationType.AssemblyQualifiedName != null)
|
|
||||||
{
|
|
||||||
var serviceType = Type.GetType(implementationType.AssemblyQualifiedName.Replace(implementationType.Name, $"I{implementationType.Name}"));
|
|
||||||
builder.Services.AddScoped(serviceType ?? implementationType, implementationType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// dynamically register database providers
|
// dynamically register database providers
|
||||||
var databaseTypes = assembly.GetInterfaces<IOqtaneDatabase>();
|
RegisterDatabaseProviders(assembly, builder.Services);
|
||||||
foreach (var databaseType in databaseTypes)
|
|
||||||
{
|
|
||||||
if (databaseType.AssemblyQualifiedName != null)
|
|
||||||
{
|
|
||||||
var serviceType = Type.GetType("Oqtane.Interfaces.IDatabase, Oqtane.Shared");
|
|
||||||
builder.Services.AddScoped(serviceType ?? databaseType, databaseType);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// register client startup services
|
// register client startup services
|
||||||
var startUps = assembly.GetInstances<IClientStartup>();
|
RegisterClientStartups(assembly, builder.Services);
|
||||||
foreach (var startup in startUps)
|
|
||||||
{
|
|
||||||
startup.ConfigureServices(builder.Services);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var host = builder.Build();
|
var host = builder.Build();
|
||||||
var jsRuntime = host.Services.GetRequiredService<IJSRuntime>();
|
|
||||||
var interop = new Interop(jsRuntime);
|
|
||||||
var localizationCookie = await interop.GetCookie(CookieRequestCultureProvider.DefaultCookieName);
|
|
||||||
var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie).UICultures[0].Value;
|
|
||||||
var localizationService = host.Services.GetRequiredService<ILocalizationService>();
|
|
||||||
var cultures = await localizationService.GetCulturesAsync();
|
|
||||||
|
|
||||||
if (culture == null || !cultures.Any(c => c.Name.Equals(culture, StringComparison.OrdinalIgnoreCase)))
|
await SetCultureFromLocalizationCookie(host.Services);
|
||||||
{
|
|
||||||
culture = cultures.Single(c => c.IsDefault).Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
SetCulture(culture);
|
|
||||||
|
|
||||||
ServiceActivator.Configure(host.Services);
|
ServiceActivator.Configure(host.Services);
|
||||||
|
|
||||||
|
@ -174,6 +114,58 @@ namespace Oqtane.Client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void RegisterModuleServices(Assembly assembly, IServiceCollection services)
|
||||||
|
{
|
||||||
|
var implementationTypes = assembly.GetInterfaces<IService>();
|
||||||
|
foreach (var implementationType in implementationTypes)
|
||||||
|
{
|
||||||
|
if (implementationType.AssemblyQualifiedName != null)
|
||||||
|
{
|
||||||
|
var serviceType = Type.GetType(implementationType.AssemblyQualifiedName.Replace(implementationType.Name, $"I{implementationType.Name}"));
|
||||||
|
services.AddScoped(serviceType ?? implementationType, implementationType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RegisterDatabaseProviders(Assembly assembly, IServiceCollection services)
|
||||||
|
{
|
||||||
|
var databaseTypes = assembly.GetInterfaces<IOqtaneDatabase>();
|
||||||
|
foreach (var databaseType in databaseTypes)
|
||||||
|
{
|
||||||
|
if (databaseType.AssemblyQualifiedName != null)
|
||||||
|
{
|
||||||
|
var serviceType = Type.GetType("Oqtane.Interfaces.IDatabase, Oqtane.Shared");
|
||||||
|
services.AddScoped(serviceType ?? databaseType, databaseType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void RegisterClientStartups(Assembly assembly, IServiceCollection services)
|
||||||
|
{
|
||||||
|
var startUps = assembly.GetInstances<IClientStartup>();
|
||||||
|
foreach (var startup in startUps)
|
||||||
|
{
|
||||||
|
startup.ConfigureServices(services);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task SetCultureFromLocalizationCookie(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
var jsRuntime = serviceProvider.GetRequiredService<IJSRuntime>();
|
||||||
|
var interop = new Interop(jsRuntime);
|
||||||
|
var localizationCookie = await interop.GetCookie(CookieRequestCultureProvider.DefaultCookieName);
|
||||||
|
var culture = CookieRequestCultureProvider.ParseCookieValue(localizationCookie).UICultures[0].Value;
|
||||||
|
var localizationService = serviceProvider.GetRequiredService<ILocalizationService>();
|
||||||
|
var cultures = await localizationService.GetCulturesAsync();
|
||||||
|
|
||||||
|
if (culture == null || !cultures.Any(c => c.Name.Equals(culture, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
culture = cultures.Single(c => c.IsDefault).Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
SetCulture(culture);
|
||||||
|
}
|
||||||
|
|
||||||
private static void SetCulture(string culture)
|
private static void SetCulture(string culture)
|
||||||
{
|
{
|
||||||
var cultureInfo = CultureInfo.GetCultureInfo(culture);
|
var cultureInfo = CultureInfo.GetCultureInfo(culture);
|
||||||
|
|
|
@ -19,7 +19,6 @@ using Oqtane.Security;
|
||||||
using Oqtane.Services;
|
using Oqtane.Services;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
|
||||||
// ReSharper disable once CheckNamespace
|
|
||||||
namespace Microsoft.Extensions.DependencyInjection
|
namespace Microsoft.Extensions.DependencyInjection
|
||||||
{
|
{
|
||||||
public static class OqtaneServiceCollectionExtensions
|
public static class OqtaneServiceCollectionExtensions
|
||||||
|
|
Loading…
Reference in New Issue
Block a user