Skip to content

Commit 2d59b7c

Browse files
committed
Change Grpc client creation from a per request approach to a client factory one.
1 parent 1e212aa commit 2d59b7c

4 files changed

Lines changed: 31 additions & 30 deletions

File tree

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/OrderingService.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,24 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services
1111
{
1212
public class OrderingService : IOrderingService
1313
{
14-
private readonly HttpClient _httpClient;
15-
private readonly UrlsConfig _urls;
14+
private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
1615
private readonly ILogger<OrderingService> _logger;
1716

18-
public OrderingService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger)
17+
public OrderingService(OrderingGrpc.OrderingGrpcClient orderingGrpcClient, ILogger<OrderingService> logger)
1918
{
20-
_httpClient = httpClient;
21-
_urls = config.Value;
19+
_orderingGrpcClient = orderingGrpcClient;
2220
_logger = logger;
2321
}
2422

2523
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
2624
{
25+
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
2726

28-
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async channel =>
29-
{
30-
var client = new OrderingGrpc.OrderingGrpcClient(channel);
31-
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
32-
33-
var command = MapToOrderDraftCommand(basketData);
34-
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
35-
_logger.LogDebug(" grpc response: {@response}", response);
27+
var command = MapToOrderDraftCommand(basketData);
28+
var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
29+
_logger.LogDebug(" grpc response: {@response}", response);
3630

37-
return MapToResponse(response, basketData);
38-
});
31+
return MapToResponse(response, basketData);
3932
}
4033

4134
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)

src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Devspaces.Support;
22
using GrpcBasket;
3+
using GrpcOrdering;
34
using HealthChecks.UI.Client;
45
using Microsoft.AspNetCore.Authentication.JwtBearer;
56
using Microsoft.AspNetCore.Builder;
@@ -205,6 +206,12 @@ public static IServiceCollection AddGrpcServices(this IServiceCollection service
205206
options.Address = new Uri(basketApi);
206207
}).AddInterceptor<GrpcExceptionInterceptor>();
207208

209+
services.AddGrpcClient<OrderingGrpc.OrderingGrpcClient>((services, options) =>
210+
{
211+
var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering;
212+
options.Address = new Uri(orderingApi);
213+
}).AddInterceptor<GrpcExceptionInterceptor>();
214+
208215
return services;
209216
}
210217

src/ApiGateways/Web.Bff.Shopping/aggregator/Services/OrderingService.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using GrpcOrdering;
2-
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config;
32
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
3+
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services;
44
using Microsoft.Extensions.Logging;
55
using Microsoft.Extensions.Options;
66
using System.Linq;
@@ -11,30 +11,24 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
1111
{
1212
public class OrderingService : IOrderingService
1313
{
14-
private readonly UrlsConfig _urls;
14+
private readonly OrderingGrpc.OrderingGrpcClient _orderingGrpcClient;
1515
private readonly ILogger<OrderingService> _logger;
16-
public readonly HttpClient _httpClient;
1716

18-
public OrderingService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger)
17+
public OrderingService(OrderingGrpc.OrderingGrpcClient orderingGrpcClient, ILogger<OrderingService> logger)
1918
{
20-
_urls = config.Value;
21-
_httpClient = httpClient;
19+
_orderingGrpcClient = orderingGrpcClient;
2220
_logger = logger;
2321
}
2422

2523
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
2624
{
27-
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async channel =>
28-
{
29-
var client = new OrderingGrpc.OrderingGrpcClient(channel);
30-
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
25+
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
3126

32-
var command = MapToOrderDraftCommand(basketData);
33-
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
34-
_logger.LogDebug(" grpc response: {@response}", response);
27+
var command = MapToOrderDraftCommand(basketData);
28+
var response = await _orderingGrpcClient.CreateOrderDraftFromBasketDataAsync(command);
29+
_logger.LogDebug(" grpc response: {@response}", response);
3530

36-
return MapToResponse(response, basketData);
37-
});
31+
return MapToResponse(response, basketData);
3832
}
3933

4034
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)

src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Devspaces.Support;
22
using GrpcBasket;
3+
using GrpcOrdering;
34
using HealthChecks.UI.Client;
45
using Microsoft.AspNetCore.Authentication.JwtBearer;
56
using Microsoft.AspNetCore.Builder;
@@ -210,6 +211,12 @@ public static IServiceCollection AddGrpcServices(this IServiceCollection service
210211
options.Address = new Uri(basketApi);
211212
}).AddInterceptor<GrpcExceptionInterceptor>();
212213

214+
services.AddGrpcClient<OrderingGrpc.OrderingGrpcClient>((services, options) =>
215+
{
216+
var orderingApi = services.GetRequiredService<IOptions<UrlsConfig>>().Value.GrpcOrdering;
217+
options.Address = new Uri(orderingApi);
218+
}).AddInterceptor<GrpcExceptionInterceptor>();
219+
213220
return services;
214221
}
215222
}

0 commit comments

Comments
 (0)