ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
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
88
89
90
91
92
93
94
95
96
97
98
using Blazored.LocalStorage;
using Grpc.Core;
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
using HotelPms.Client.Blazor;
using HotelPms.Client.Blazor.Services;
using HotelPms.Client.Blazor.Util;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using MudBlazor;
using MudBlazor.Services;
 
 
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
 
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    EnvironmentSetting.Debug("CurrentDomain_UnhandledException");
}
 
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
 
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddMudServices(config =>
{
    config.SnackbarConfiguration.PositionClass = Defaults.Classes.Position.TopCenter;
 
    config.SnackbarConfiguration.PreventDuplicates = false;
    config.SnackbarConfiguration.NewestOnTop = false;
    config.SnackbarConfiguration.ShowCloseIcon = true;
    config.SnackbarConfiguration.VisibleStateDuration = 10000;
    config.SnackbarConfiguration.HideTransitionDuration = 500;
    config.SnackbarConfiguration.ShowTransitionDuration = 500;
    config.SnackbarConfiguration.SnackbarVariant = Variant.Filled;
});
builder.Services.AddBlazoredLocalStorage();
builder.Services.AddSingleton<IGlobalLoadingSpinner, DefaultGlobalLoadingSpinner>();
builder.Services.AddAuthorizationCore();
builder.Services.AddScoped<AuthenticationStateProvider, ApiAuthenticationStateProvider>();
builder.Services.AddScoped<IAuthService, AuthService>();
 
EnvironmentSetting.IsDevelopment = builder.HostEnvironment.IsDevelopment();
EnvironmentSetting.Init(builder.Configuration);
EnvironmentSetting.ServiceCollection = builder.Services;    
 
builder.Services.AddSingleton(services =>
{
    if (EnvironmentSetting.BackendUrl.Length == 0)
    {
        var navigationManager = services.GetRequiredService<NavigationManager>();
        EnvironmentSetting.BackendUrl = navigationManager.BaseUri;
    }
 
    var loadingSpinner = services.GetService<IGlobalLoadingSpinner>();
    var handler = new GrpcSubDirHandler(new HttpClientHandler(), loadingSpinner, EnvironmentSetting.SubDir);
    GrpcWebHandler httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, handler);  //Base64
    //GrpcWebHandler httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWeb, handler);
 
    //拦截器(当grpc客户端每次发起请求会通过这里获取http头信息,可以再里面判定过期)
    var credentials = CallCredentials.FromInterceptor((context, metadata) =>
    {
        if (!string.IsNullOrEmpty(ApiAuthenticationStateProvider.AccessToken))
        {
            EnvironmentSetting.Debug($"CallCredentials.FromInterceptor:Bearer {ApiAuthenticationStateProvider.AccessToken}");
            metadata.Add("Authorization", $"Bearer {ApiAuthenticationStateProvider.AccessToken}");
        }
        return Task.CompletedTask;
    });
 
 
    GrpcChannel channel = GrpcChannel.ForAddress(EnvironmentSetting.BackendUrl, new GrpcChannelOptions
    {
        HttpHandler = httpHandler,
        Credentials = ChannelCredentials.Create(new SslCredentials(), credentials)
    });
 
    //using Grpc.Core.Interceptors;   //必须引入
 
    //var channel = GrpcChannel.ForAddress("https://localhost:5001");
    //// 完整写法
    //var invoker = channel.CreateCallInvoker().Intercept(new GRPCClientLoggingInterceptor());
    //var client = new Greeter.GreeterClient(invoker);
    //await client.SayHelloAsync(new HelloRequest() { Name = "长安书小妆" });
 
    EnvironmentSetting.GrpcChannel = channel;
    return channel;
});
 
builder.Services.AddJsInputCore();
 
var host = builder.Build();
EnvironmentSetting.ServiceProvider = host.Services;
await host.RunAsync();
//await builder.Build().RunAsync();