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"); builder.RootComponents.Add("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(); builder.Services.AddAuthorizationCore(); builder.Services.AddScoped(); builder.Services.AddScoped(); 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(); EnvironmentSetting.BackendUrl = navigationManager.BaseUri; } var loadingSpinner = services.GetService(); 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();