Update ConfigureServices.cs
This commit is contained in:
@@ -33,26 +33,24 @@ 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);
|
|
||||||
|
|
||||||
services.AddSingleton(sp => new PackageContract.PackageContractClient(channel));
|
// Register HttpClient for gRPC
|
||||||
services.AddSingleton(sp => new UserContract.UserContractClient(channel));
|
services.AddScoped(sp =>
|
||||||
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 httpClient = new HttpClient(new GrpcWebHandler(GrpcWebMode.GrpcWeb, new HttpClientHandler()));
|
||||||
|
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) =>
|
var credentials = CallCredentials.FromInterceptor(async (context, metadata) =>
|
||||||
{
|
{
|
||||||
// Get token from local storage if available
|
try
|
||||||
var localStorage = serviceProvider.GetService(typeof(ILocalStorageService)) as ILocalStorageService;
|
|
||||||
if (localStorage != null)
|
|
||||||
{
|
{
|
||||||
var token = await localStorage.GetItemAsync<string>("auth:token");
|
var token = await localStorage.GetItemAsync<string>("auth:token");
|
||||||
if (!string.IsNullOrWhiteSpace(token))
|
if (!string.IsNullOrWhiteSpace(token))
|
||||||
@@ -60,11 +58,13 @@ public static class ConfigureServices
|
|||||||
metadata.Add("Authorization", $"Bearer {token}");
|
metadata.Add("Authorization", $"Bearer {token}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Ignore errors during token retrieval
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// SslCredentials is used here because this channel is using TLS.
|
var channel = GrpcChannel.ForAddress(baseUrl, new GrpcChannelOptions
|
||||||
// CallCredentials can't be used with ChannelCredentials.Insecure on non-TLS channels.
|
|
||||||
var channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions
|
|
||||||
{
|
{
|
||||||
UnsafeUseInsecureChannelCallCredentials = true,
|
UnsafeUseInsecureChannelCallCredentials = true,
|
||||||
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials),
|
Credentials = ChannelCredentials.Create(new SslCredentials(), credentials),
|
||||||
@@ -72,6 +72,110 @@ public static class ConfigureServices
|
|||||||
MaxReceiveMessageSize = 1000 * 1024 * 1024, // 1 GB
|
MaxReceiveMessageSize = 1000 * 1024 * 1024, // 1 GB
|
||||||
MaxSendMessageSize = 1000 * 1024 * 1024 // 1 GB
|
MaxSendMessageSize = 1000 * 1024 * 1024 // 1 GB
|
||||||
});
|
});
|
||||||
return channel;
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user