Example #1
0
        static ModuleLoader()
        {
            var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly()?.Location);

            var assemblyThemePath = Path.Combine(@$ "{assemblyPath}", "Themes/MudBlazor");

            List <Assembly> allAssemblies = new();

            if (Directory.Exists(assemblyThemePath))
            {
                foreach (var dll in Directory.GetFiles(assemblyThemePath, "*.dll"))
                {
                    allAssemblies.Add(AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(dll))));
                }
            }

            var assemblyModulesPath = Path.Combine(@$ "{assemblyPath}", "Modules");

            if (Directory.Exists(assemblyModulesPath))
            {
                foreach (var dll in Directory.GetFiles(assemblyModulesPath, "*.dll"))
                {
                    allAssemblies.Add(AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(dll))));
                }
            }

            ModuleProvider.Init(allAssemblies);
        }
Example #2
0
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);

            //TODO see what oqtane does
            var baseModule  = new Theme.Material.Module();
            var adminModule = new Theme.Material.Admin.Module();
            var demoModule  = new Theme.Material.Demo.Module();

            Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();

            ModuleProvider.Init(allAssemblies);

            builder.RootComponents.AddRange(new[] { ModuleProvider.RootComponentMapping });

            builder.Services.AddLocalization();
            builder.Services.AddDataProtection().SetApplicationName(nameof(BlazorBoilerplate));
            builder.Services.AddProtectedBrowserStorage();
            builder.Services.AddSingleton(sp => new HttpClient {
                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
            });
            builder.Services.AddAuthorizationCore(config =>
            {
                config.AddPolicy(Policies.IsAdmin, Policies.IsAdminPolicy());
                config.AddPolicy(Policies.IsUser, Policies.IsUserPolicy());
                config.AddPolicy(Policies.IsReadOnly, Policies.IsUserPolicy());
                // config.AddPolicy(Policies.IsMyDomain, Policies.IsMyDomainPolicy());  Only works on the server end
            });

            builder.Services.AddScoped <AuthenticationStateProvider, IdentityAuthenticationStateProvider>();
            builder.Services.AddScoped <IAuthorizeApi, AuthorizeApi>();
            builder.Services.AddScoped <AppState>();
            builder.Services.AddTransient <IApiClient, ApiClient>();

            foreach (var module in ModuleProvider.Modules)
            {
                module.ConfigureWebAssemblyServices(builder.Services);
            }

            var host = builder.Build();

            foreach (var module in ModuleProvider.Modules)
            {
                module.ConfigureWebAssemblyHost(host);
            }

            using (var serviceScope = host.Services.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var js            = serviceScope.ServiceProvider.GetService <IJSRuntime>();
                var cookieCulture = await js.GetAspNetCoreCultureCookie();

                if (!string.IsNullOrWhiteSpace(cookieCulture))
                {
                    CultureInfo.DefaultThreadCurrentCulture   = new CultureInfo(cookieCulture);
                    CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CurrentCulture;
                }
            }

            await host.RunAsync();
        }
Example #3
0
        static ModuleLoader()
        {
            var assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);

            assemblyPath = @$ "{assemblyPath}\Modules";

            List <Assembly> allAssemblies = new List <Assembly>();

            if (Directory.Exists(assemblyPath))
            {
                foreach (var dll in Directory.GetFiles(assemblyPath, "*.dll"))
                {
                    allAssemblies.Add(AssemblyLoadContext.Default.LoadFromStream(new MemoryStream(File.ReadAllBytes(dll))));
                }

                ModuleProvider.Init(allAssemblies);
            }
        }
Example #4
0
        public static async Task Main(string[] args)
        {
            var builder = WebAssemblyHostBuilder.CreateDefault(args);

            //TODO see what oqtane does
            var baseModule  = new Theme.Material.Module();
            var adminModule = new Theme.Material.Admin.Module();
            var demoModule  = new Theme.Material.Demo.Module();

            Assembly[] allAssemblies = AppDomain.CurrentDomain.GetAssemblies();

            ModuleProvider.Init(allAssemblies);

            builder.RootComponents.AddRange(new[] { ModuleProvider.RootComponentMapping });

            builder.Services.AddSingleton <ILocalizationProvider, LocalizationProvider>();
            builder.Services.AddTextLocalization(options =>
            {
                options.ReturnOnlyKeyIfNotFound = !builder.HostEnvironment.IsDevelopment();
                options.FallBackNeutralCulture  = !builder.HostEnvironment.IsDevelopment();
            });
            builder.Services.AddDataProtection().SetApplicationName(nameof(BlazorBoilerplate));
            builder.Services.AddSingleton(sp => new HttpClient {
                BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
            });
            builder.Services.AddAuthorizationCore(config =>
            {
                config.AddPolicy(Policies.IsAdmin, Policies.IsAdminPolicy());
                config.AddPolicy(Policies.IsUser, Policies.IsUserPolicy());
                config.AddPolicy(Policies.IsReadOnly, Policies.IsReadOnlyPolicy());
                config.AddPolicy(Policies.TwoFactorEnabled, Policies.IsTwoFactorEnabledPolicy());
                // config.AddPolicy(Policies.IsMyDomain, Policies.IsMyDomainPolicy());  Only works on the server end
            });

            builder.Services.AddScoped <ILocalizationApiClient, LocalizationApiClient>();
            builder.Services.AddScoped <AuthenticationStateProvider, IdentityAuthenticationStateProvider>();
            builder.Services.AddScoped <IAccountApiClient, AccountApiClient>();
            builder.Services.AddScoped <AppState>();
            builder.Services.AddScoped <IApiClient, ApiClient>();

            foreach (var module in ModuleProvider.Modules)
            {
                module.ConfigureWebAssemblyServices(builder.Services);
            }

            var host = builder.Build();

            foreach (var module in ModuleProvider.Modules)
            {
                module.ConfigureWebAssemblyHost(host);
            }

            using (var serviceScope = host.Services.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var js            = serviceScope.ServiceProvider.GetService <IJSRuntime>();
                var cookieCulture = await js.GetAspNetCoreCultureCookie();

                if (!string.IsNullOrWhiteSpace(cookieCulture))
                {
                    CultureInfo.DefaultThreadCurrentCulture   = new CultureInfo(cookieCulture);
                    CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CurrentCulture;
                }

                var localizationApiClient = serviceScope.ServiceProvider.GetService <ILocalizationApiClient>();

                var localizationProvider = serviceScope.ServiceProvider.GetService <ILocalizationProvider>();

                var localizationRecordsTask = localizationApiClient.GetLocalizationRecords();

                var pluralFormRulesTask = localizationApiClient.GetPluralFormRules();

                await Task.WhenAll(new Task[] { localizationRecordsTask, pluralFormRulesTask });

                localizationProvider.Init(localizationRecordsTask.Result, pluralFormRulesTask.Result);
            }

            await host.RunAsync();
        }