Files
FrontOffice/src/FrontOffice.Main/ConfigureServices.cs
MeysamMoghaddam 1b8a584435 u
2025-09-28 01:28:44 +03:30

69 lines
3.1 KiB
C#

using Blazored.LocalStorage;
using Grpc.Core;
using Grpc.Net.Client.Web;
using Grpc.Net.Client;
using MudBlazor.Services;
using System.Text.Json;
using System.Text.Json.Serialization;
using FrontOffice.BFF.Package.Protobuf.Protos.Package;
using FrontOffice.BFF.User.Protobuf.Protos.User;
using FrontOffice.BFF.UserAddress.Protobuf.Protos.UserAddress;
using FrontOffice.BFF.UserOrder.Protobuf.Protos.UserOrder;
namespace Microsoft.Extensions.DependencyInjection;
public static class ConfigureServices
{
public static IServiceCollection AddCommonServices(this IServiceCollection services)
{
services.AddMudServices();
services.AddBlazoredLocalStorage(config =>
{
config.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
config.JsonSerializerOptions.IgnoreReadOnlyProperties = true;
config.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
config.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
config.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
config.JsonSerializerOptions.WriteIndented = false;
});
return services;
}
public static IServiceCollection AddGrpcServices(this IServiceCollection services, IConfiguration configuration)
{
var baseUrl = configuration["GwUrl"];
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
httpClient.Timeout = TimeSpan.FromMinutes(10); // TODO Check Timeout
var serviceProvider = services.BuildServiceProvider();
var channel = CreateAuthenticatedChannel(baseUrl, httpClient, serviceProvider);
services.AddSingleton(sp => new PackageContract.PackageContractClient(channel));
services.AddSingleton(sp => new UserContract.UserContractClient(channel));
services.AddSingleton(sp => new UserAddressContract.UserAddressContractClient(channel));
services.AddSingleton(sp => new UserOrderContract.UserOrderContractClient(channel));
return services;
}
private static GrpcChannel CreateAuthenticatedChannel(string address, HttpClient httpClient, IServiceProvider serviceProvider)
{
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
{
await Task.CompletedTask;
});
// SslCredentials is used here because this channel is using TLS.
// CallCredentials can't be used with ChannelCredentials.Insecure on non-TLS channels.
var channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions
{
UnsafeUseInsecureChannelCallCredentials = true,
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials),
HttpClient = httpClient,
MaxReceiveMessageSize = 1000 * 1024 * 1024, // 1 GB
MaxSendMessageSize = 1000 * 1024 * 1024 // 1 GB
});
return channel;
}
}