-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMauiProgram.cs
More file actions
87 lines (81 loc) · 3.66 KB
/
MauiProgram.cs
File metadata and controls
87 lines (81 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using Blazored.LocalStorage;
using Boiler.Mobile.Framework;
using Boiler.Mobile.Framework.Auth;
using Boiler.Mobile.Framework.Http;
using Boiler.Mobile.Framework.Logging;
using Fluxor;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using MudBlazor.Services;
using System.Reflection;
namespace Boiler.Mobile
{
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
var _assembly = Assembly.GetExecutingAssembly();
var _assemblyName = _assembly.GetName().Name;
#region Config
using var stream = _assembly.GetManifestResourceStream($"{_assemblyName}.appSettings.json");
var config = new ConfigurationBuilder()
.AddJsonStream(stream)
.Build();
builder.Configuration.AddConfiguration(config);
builder.Services.AddMauiBlazorWebView();
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
#endregion
#region Http
builder.Services.AddTransient<AuthorizationHeaderHandler>();
builder.Services.AddScoped(sp => {
var serverAddress = builder.Configuration.GetSection("UrlAddress").Value;
var navigationManager = sp.GetRequiredService<NavigationManager>();
HttpClientHandler clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
var localStorageService = sp.GetRequiredService<ILocalStorageService>();
var authTokenHandler = new AuthorizationHeaderHandler(localStorageService);
authTokenHandler.InnerHandler = clientHandler;
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
ApiClient _http = new ApiClient(authTokenHandler) { BaseAddress = new Uri(serverAddress) };
_http.OnUnauthorizedResponse += () => navigationManager.NavigateTo(Boiler.Mobile.Shared.Routes.User.Logout);
return _http;
});
#endregion
#region Auth
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<ClientAuthroizationStateNotifier>();
builder.Services.AddScoped<AuthenticationStateProvider, ClientAuthorizationStateProvider>();
#endregion
#region dependency
var servicesBuilder = new ApiServicesBuilder();
servicesBuilder.ConfigureServices(builder.Services, new string[] { _assemblyName });
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddMudServices();
builder.Services.AddHttpContextAccessor();
#endregion
#region Fluxor
builder.Services.AddFluxor(opts =>
{
opts.ScanAssemblies(typeof(App).Assembly);
opts.AddMiddleware<LoggingMiddleware>();
opts.UseReduxDevTools();
});
#endregion
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
return builder.Build();
}
}
}