Change Grpc client creation from a per request approach to a client factory one.
This commit is contained in:
parent
1e212aa2e8
commit
2d59b7cc56
@ -11,31 +11,24 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
|
||||
{
|
||||
public class OrderingService : IOrderingService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly UrlsConfig _urls;
|
||||
private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
|
||||
private readonly ILogger<OrderingService> _logger;
|
||||
|
||||
public OrderingService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger)
|
||||
public OrderingService(OrderingGrpc.OrderingGrpcClient orderingGrpcClient, ILogger<OrderingService> logger)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
_urls = config.Value;
|
||||
_orderingGrpcClient = orderingGrpcClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
||||
{
|
||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||
|
||||
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async channel =>
|
||||
{
|
||||
var client = new OrderingGrpc.OrderingGrpcClient(channel);
|
||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||
var command = MapToOrderDraftCommand(basketData);
|
||||
var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
|
||||
_logger.LogDebug(" grpc response: {@response}", response);
|
||||
|
||||
var command = MapToOrderDraftCommand(basketData);
|
||||
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
|
||||
_logger.LogDebug(" grpc response: {@response}", response);
|
||||
|
||||
return MapToResponse(response, basketData);
|
||||
});
|
||||
return MapToResponse(response, basketData);
|
||||
}
|
||||
|
||||
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Devspaces.Support;
|
||||
using GrpcBasket;
|
||||
using GrpcOrdering;
|
||||
using HealthChecks.UI.Client;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@ -205,6 +206,12 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
|
||||
options.Address = new Uri(basketApi);
|
||||
}).AddInterceptor<GrpcExceptionInterceptor>();
|
||||
|
||||
services.AddGrpcClient<OrderingGrpc.OrderingGrpcClient>((services, options) =>
|
||||
{
|
||||
var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering;
|
||||
options.Address = new Uri(orderingApi);
|
||||
}).AddInterceptor<GrpcExceptionInterceptor>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
using GrpcOrdering;
|
||||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
|
||||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
||||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Linq;
|
||||
@ -11,30 +11,24 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
||||
{
|
||||
public class OrderingService : IOrderingService
|
||||
{
|
||||
private readonly UrlsConfig _urls;
|
||||
private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
|
||||
private readonly ILogger<OrderingService> _logger;
|
||||
public readonly HttpClient _httpClient;
|
||||
|
||||
public OrderingService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger)
|
||||
public OrderingService(OrderingGrpc.OrderingGrpcClient orderingGrpcClient, ILogger<OrderingService> logger)
|
||||
{
|
||||
_urls = config.Value;
|
||||
_httpClient = httpClient;
|
||||
_orderingGrpcClient = orderingGrpcClient;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
||||
{
|
||||
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async channel =>
|
||||
{
|
||||
var client = new OrderingGrpc.OrderingGrpcClient(channel);
|
||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||
|
||||
var command = MapToOrderDraftCommand(basketData);
|
||||
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
|
||||
_logger.LogDebug(" grpc response: {@response}", response);
|
||||
var command = MapToOrderDraftCommand(basketData);
|
||||
var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
|
||||
_logger.LogDebug(" grpc response: {@response}", response);
|
||||
|
||||
return MapToResponse(response, basketData);
|
||||
});
|
||||
return MapToResponse(response, basketData);
|
||||
}
|
||||
|
||||
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
||||
|
@ -1,5 +1,6 @@
|
||||
using Devspaces.Support;
|
||||
using GrpcBasket;
|
||||
using GrpcOrdering;
|
||||
using HealthChecks.UI.Client;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
@ -210,6 +211,12 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator
|
||||
options.Address = new Uri(basketApi);
|
||||
}).AddInterceptor<GrpcExceptionInterceptor>();
|
||||
|
||||
services.AddGrpcClient<OrderingGrpc.OrderingGrpcClient>((services, options) =>
|
||||
{
|
||||
var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering;
|
||||
options.Address = new Uri(orderingApi);
|
||||
}).AddInterceptor<GrpcExceptionInterceptor>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user