@@ -33,149 +33,45 @@ public static class ConfigureServices
|
|||||||
public static IServiceCollection AddGrpcServices(this IServiceCollection services, IConfiguration configuration)
|
public static IServiceCollection AddGrpcServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
var baseUrl = configuration["GwUrl"];
|
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);
|
||||||
|
|
||||||
// Register HttpClient for gRPC
|
services.AddSingleton(sp => new PackageContract.PackageContractClient(channel));
|
||||||
services.AddScoped(sp =>
|
services.AddSingleton(sp => new UserContract.UserContractClient(channel));
|
||||||
{
|
services.AddSingleton(sp => new UserAddressContract.UserAddressContractClient(channel));
|
||||||
var httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
|
services.AddSingleton(sp => new UserOrderContract.UserOrderContractClient(channel));
|
||||||
httpClient.Timeout = TimeSpan.FromMinutes(10);
|
|
||||||
return httpClient;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Register gRPC clients as scoped services
|
|
||||||
services.AddScoped(sp =>
|
|
||||||
{
|
|
||||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
|
||||||
var localStorage = sp.GetRequiredService<ILocalStorageService>();
|
|
||||||
|
|
||||||
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var token = await localStorage.GetItemAsync<string>("auth:token");
|
|
||||||
if (!string.IsNullOrWhiteSpace(token))
|
|
||||||
{
|
|
||||||
metadata.Add("Authorization", $"Bearer {token}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// Ignore errors during token retrieval
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var channel = GrpcChannel.ForAddress(baseUrl, 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 new PackageContract.PackageContractClient(channel);
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddScoped(sp =>
|
|
||||||
{
|
|
||||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
|
||||||
var localStorage = sp.GetRequiredService<ILocalStorageService>();
|
|
||||||
|
|
||||||
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var token = await localStorage.GetItemAsync<string>("auth:token");
|
|
||||||
if (!string.IsNullOrWhiteSpace(token))
|
|
||||||
{
|
|
||||||
metadata.Add("Authorization", $"Bearer {token}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// Ignore errors during token retrieval
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var channel = GrpcChannel.ForAddress(baseUrl, 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 new UserContract.UserContractClient(channel);
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddScoped(sp =>
|
|
||||||
{
|
|
||||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
|
||||||
var localStorage = sp.GetRequiredService<ILocalStorageService>();
|
|
||||||
|
|
||||||
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var token = await localStorage.GetItemAsync<string>("auth:token");
|
|
||||||
if (!string.IsNullOrWhiteSpace(token))
|
|
||||||
{
|
|
||||||
metadata.Add("Authorization", $"Bearer {token}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// Ignore errors during token retrieval
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var channel = GrpcChannel.ForAddress(baseUrl, 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 new UserAddressContract.UserAddressContractClient(channel);
|
|
||||||
});
|
|
||||||
|
|
||||||
services.AddScoped(sp =>
|
|
||||||
{
|
|
||||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
|
||||||
var localStorage = sp.GetRequiredService<ILocalStorageService>();
|
|
||||||
|
|
||||||
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var token = await localStorage.GetItemAsync<string>("auth:token");
|
|
||||||
if (!string.IsNullOrWhiteSpace(token))
|
|
||||||
{
|
|
||||||
metadata.Add("Authorization", $"Bearer {token}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// Ignore errors during token retrieval
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
var channel = GrpcChannel.ForAddress(baseUrl, 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 new UserOrderContract.UserOrderContractClient(channel);
|
|
||||||
});
|
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static GrpcChannel CreateAuthenticatedChannel(string address, HttpClient httpClient, IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
||||||
|
{
|
||||||
|
// Get token from local storage if available
|
||||||
|
var localStorage = serviceProvider.GetService(typeof(ILocalStorageService)) as ILocalStorageService;
|
||||||
|
if (localStorage != null)
|
||||||
|
{
|
||||||
|
var token = await localStorage.GetItemAsync<string>("auth:token");
|
||||||
|
if (!string.IsNullOrWhiteSpace(token))
|
||||||
|
{
|
||||||
|
metadata.Add("Authorization", $"Bearer {token}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user