Change Grpc client creation from a per request approach to a client factory one.

This commit is contained in:
David Sanz 2020-12-16 18:01:27 +01:00
parent 1e212aa2e8
commit 2d59b7cc56
4 changed files with 31 additions and 30 deletions

View File

@ -11,31 +11,24 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
{ {
public class OrderingService : IOrderingService public class OrderingService : IOrderingService
{ {
private readonly HttpClient _httpClient; private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
private readonly UrlsConfig _urls;
private readonly ILogger<OrderingService> _logger; 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; _orderingGrpcClient = orderingGrpcClient;
_urls = config.Value;
_logger = logger; _logger = logger;
} }
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData) 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 command = MapToOrderDraftCommand(basketData);
var response = await client.CreateOrderDraftFromBasketDataAsync(command); var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
_logger.LogDebug(" grpc response: {@response}", response); _logger.LogDebug(" grpc response: {@response}", response);
return MapToResponse(response, basketData); return MapToResponse(response, basketData);
});
} }
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData) private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)

View File

@ -1,5 +1,6 @@
using Devspaces.Support; using Devspaces.Support;
using GrpcBasket; using GrpcBasket;
using GrpcOrdering;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -205,6 +206,12 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator
options.Address = new Uri(basketApi); options.Address = new Uri(basketApi);
}).AddInterceptor<GrpcExceptionInterceptor>(); }).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; return services;
} }

View File

@ -1,6 +1,6 @@
using GrpcOrdering; using GrpcOrdering;
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models; using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System.Linq; using System.Linq;
@ -11,30 +11,24 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
{ {
public class OrderingService : IOrderingService public class OrderingService : IOrderingService
{ {
private readonly UrlsConfig _urls; private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
private readonly ILogger<OrderingService> _logger; 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; _orderingGrpcClient = orderingGrpcClient;
_httpClient = httpClient;
_logger = logger; _logger = logger;
} }
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData) 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 command = MapToOrderDraftCommand(basketData);
var response = await client.CreateOrderDraftFromBasketDataAsync(command); var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
_logger.LogDebug(" grpc response: {@response}", response); _logger.LogDebug(" grpc response: {@response}", response);
return MapToResponse(response, basketData); return MapToResponse(response, basketData);
});
} }
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData) private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)

View File

@ -1,5 +1,6 @@
using Devspaces.Support; using Devspaces.Support;
using GrpcBasket; using GrpcBasket;
using GrpcOrdering;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -210,6 +211,12 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator
options.Address = new Uri(basketApi); options.Address = new Uri(basketApi);
}).AddInterceptor<GrpcExceptionInterceptor>(); }).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; return services;
} }
} }