refactored the grpc client
This commit is contained in:
parent
311e1a2bfd
commit
454525d517
@ -2,41 +2,35 @@
|
|||||||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.Net.Http;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Grpc.Net.Client;
|
using Grpc.Net.Client;
|
||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using GrpcBasket;
|
using GrpcBasket;
|
||||||
using Grpc.Core;
|
using System.Net.Http;
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
||||||
{
|
{
|
||||||
public class BasketService : IBasketService
|
public class BasketService : IBasketService
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
|
||||||
private readonly UrlsConfig _urls;
|
private readonly UrlsConfig _urls;
|
||||||
|
public readonly HttpClient _httpClient;
|
||||||
private readonly ILogger<BasketService> _logger;
|
private readonly ILogger<BasketService> _logger;
|
||||||
|
|
||||||
public BasketService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<BasketService> logger)
|
public BasketService(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<BasketService> logger)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
|
||||||
_urls = config.Value;
|
_urls = config.Value;
|
||||||
|
_httpClient = httpClient;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<BasketData> GetById(string id)
|
public async Task<BasketData> GetById(string id)
|
||||||
{
|
{
|
||||||
return await GrpcCallerService.CallService(_urls.GrpcBasket, async httpClient =>
|
return await GrpcCallerService.CallService(_urls.GrpcBasket, async httpClient =>
|
||||||
{
|
{
|
||||||
_logger.LogWarning("######################## grpc client created, request = {@id}", id);
|
|
||||||
|
|
||||||
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
||||||
|
|
||||||
_logger.LogDebug("grpc client created, request = {@id}", id);
|
_logger.LogDebug("grpc client created, request = {@id}", id);
|
||||||
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
|
var response = await client.GetBasketByIdAsync(new BasketRequest { Id = id });
|
||||||
|
|
||||||
_logger.LogDebug("grpc response {@response}", response);
|
_logger.LogDebug("grpc response {@response}", response);
|
||||||
|
|
||||||
return MapToBasketData(response);
|
return MapToBasketData(response);
|
||||||
@ -54,37 +48,6 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|||||||
|
|
||||||
return client.UpdateBasketAsync(request);
|
return client.UpdateBasketAsync(request);
|
||||||
});
|
});
|
||||||
|
|
||||||
//AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
||||||
//AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
|
||||||
|
|
||||||
//using (var httpClientHandler = new HttpClientHandler())
|
|
||||||
//{
|
|
||||||
// httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
|
||||||
// using (var httpClient = new HttpClient(httpClientHandler))
|
|
||||||
// {
|
|
||||||
// httpClient.BaseAddress = new Uri(_urls.GrpcBasket);
|
|
||||||
|
|
||||||
// _logger.LogDebug("Creating grpc client for basket {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
|
||||||
|
|
||||||
// var client = GrpcClient.Create<Basket.BasketClient>(httpClient);
|
|
||||||
|
|
||||||
|
|
||||||
// try
|
|
||||||
// {
|
|
||||||
|
|
||||||
// _logger.LogDebug("Grpc update basket currentBasket {@currentBasket}", currentBasket);
|
|
||||||
// var request = MapToCustomerBasketRequest(currentBasket);
|
|
||||||
// _logger.LogDebug("Grpc update basket request {@request}", request);
|
|
||||||
|
|
||||||
// await client.UpdateBasketAsync(request);
|
|
||||||
// }
|
|
||||||
// catch (RpcException e)
|
|
||||||
// {
|
|
||||||
// _logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private BasketData MapToBasketData(CustomerBasketResponse customerBasketRequest)
|
private BasketData MapToBasketData(CustomerBasketResponse customerBasketRequest)
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Newtonsoft.Json;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -11,7 +10,6 @@ using Grpc.Net.Client;
|
|||||||
using System;
|
using System;
|
||||||
using static CatalogApi.Catalog;
|
using static CatalogApi.Catalog;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Grpc.Core;
|
|
||||||
|
|
||||||
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
||||||
{
|
{
|
||||||
@ -41,36 +39,15 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|||||||
|
|
||||||
public async Task<IEnumerable<CatalogItem>> GetCatalogItemsAsync(IEnumerable<int> ids)
|
public async Task<IEnumerable<CatalogItem>> GetCatalogItemsAsync(IEnumerable<int> ids)
|
||||||
{
|
{
|
||||||
|
return await GrpcCallerService.CallService(_urls.GrpcCatalog, async httpClient =>
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
|
||||||
|
|
||||||
using (var httpClientHandler = new HttpClientHandler())
|
|
||||||
{
|
{
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
var client = GrpcClient.Create<CatalogClient>(httpClient);
|
||||||
using (var httpClient = new HttpClient(httpClientHandler))
|
var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 };
|
||||||
{
|
_logger.LogInformation("grpc client created, request = {@request}", request);
|
||||||
httpClient.BaseAddress = new Uri(_urls.GrpcCatalog);
|
var response = await client.GetItemsByIdsAsync(request);
|
||||||
|
_logger.LogInformation("grpc response {@response}", response);
|
||||||
_logger.LogInformation("Creating grpc client for CatalogClient {@httpClient.BaseAddress}, {@httpClient} ", httpClient.BaseAddress, httpClient);
|
return response.Data.Select(this.MapToCatalogItemResponse);
|
||||||
|
});
|
||||||
try
|
|
||||||
{
|
|
||||||
var client = GrpcClient.Create<CatalogClient>(httpClient);
|
|
||||||
var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 };
|
|
||||||
_logger.LogInformation("grpc client created, request = {@request}", request);
|
|
||||||
var response = await client.GetItemsByIdsAsync(request);
|
|
||||||
_logger.LogInformation("grpc response {@response}", response);
|
|
||||||
return response.Data.Select(this.MapToCatalogItemResponse);
|
|
||||||
}
|
|
||||||
catch (RpcException e)
|
|
||||||
{
|
|
||||||
_logger.LogError($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private CatalogItem MapToCatalogItemResponse(CatalogItemResponse catalogItemResponse)
|
private CatalogItem MapToCatalogItemResponse(CatalogItemResponse catalogItemResponse)
|
||||||
|
@ -8,28 +8,30 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|||||||
{
|
{
|
||||||
public static class GrpcCallerService
|
public static class GrpcCallerService
|
||||||
{
|
{
|
||||||
public static Task<TResponse> CallService<TResponse>(string urlGrpc, Func<HttpClient, Task<TResponse>> func)
|
public static async Task<TResponse> CallService<TResponse>(string urlGrpc, Func<HttpClient, Task<TResponse>> func)
|
||||||
{
|
{
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||||
|
|
||||||
using (var httpClientHandler = new HttpClientHandler())
|
using var httpClientHandler = new HttpClientHandler
|
||||||
{
|
{
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
|
||||||
using (var httpClient = new HttpClient(httpClientHandler))
|
};
|
||||||
{
|
|
||||||
httpClient.BaseAddress = new Uri(urlGrpc);
|
|
||||||
Log.Debug("Creating grpc client base address {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
|
||||||
|
|
||||||
try
|
using var httpClient = new HttpClient(httpClientHandler)
|
||||||
{
|
{
|
||||||
return func(httpClient);
|
BaseAddress = new Uri(urlGrpc)
|
||||||
}
|
};
|
||||||
catch (RpcException e)
|
|
||||||
{
|
Log.Information("Creating grpc client base address urlGrpc ={@urlGrpc}, BaseAddress={@BaseAddress} ", urlGrpc, httpClient.BaseAddress);
|
||||||
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
|
return await func(httpClient);
|
||||||
|
}
|
||||||
|
catch (RpcException e)
|
||||||
|
{
|
||||||
|
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
||||||
@ -38,34 +40,34 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task CallService(string urlGrpc, Func<HttpClient, Task> func)
|
public static async Task CallService(string urlGrpc, Func<HttpClient, Task> func)
|
||||||
{
|
{
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
||||||
|
|
||||||
using (var httpClientHandler = new HttpClientHandler())
|
using var httpClientHandler = new HttpClientHandler
|
||||||
{
|
{
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }
|
||||||
using (var httpClient = new HttpClient(httpClientHandler))
|
};
|
||||||
{
|
|
||||||
httpClient.BaseAddress = new Uri(urlGrpc);
|
|
||||||
Log.Debug("Creating grpc client base address {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
|
||||||
|
|
||||||
try
|
using var httpClient = new HttpClient(httpClientHandler)
|
||||||
{
|
{
|
||||||
return func(httpClient);
|
BaseAddress = new Uri(urlGrpc)
|
||||||
}
|
};
|
||||||
catch (RpcException e)
|
|
||||||
{
|
Log.Debug("Creating grpc client base address {@httpClient.BaseAddress} ", httpClient.BaseAddress);
|
||||||
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
|
await func(httpClient);
|
||||||
|
}
|
||||||
|
catch (RpcException e)
|
||||||
|
{
|
||||||
|
Log.Error($"Error calling via grpc: {e.Status} - {e.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", false);
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", false);
|
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", false);
|
||||||
|
|
||||||
return default;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,52 +14,30 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services
|
|||||||
{
|
{
|
||||||
public class OrderingService : IOrderingService
|
public class OrderingService : IOrderingService
|
||||||
{
|
{
|
||||||
private readonly HttpClient _httpClient;
|
|
||||||
private readonly UrlsConfig _urls;
|
private readonly UrlsConfig _urls;
|
||||||
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(HttpClient httpClient, IOptions<UrlsConfig> config, ILogger<OrderingService> logger)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
|
||||||
_urls = config.Value;
|
_urls = config.Value;
|
||||||
|
_httpClient = httpClient;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
public async Task<OrderData> GetOrderDraftAsync(BasketData basketData)
|
||||||
{
|
{
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
|
return await GrpcCallerService.CallService(_urls.GrpcOrdering, async httpClient =>
|
||||||
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2Support", true);
|
|
||||||
|
|
||||||
using (var httpClientHandler = new HttpClientHandler())
|
|
||||||
{
|
{
|
||||||
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; };
|
var client = GrpcClient.Create<OrderingGrpc.OrderingGrpcClient>(httpClient);
|
||||||
using (var httpClient = new HttpClient(httpClientHandler))
|
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
||||||
{
|
|
||||||
httpClient.BaseAddress = new Uri(_urls.GrpcOrdering);
|
|
||||||
|
|
||||||
_logger.LogDebug(" Creating grpc client for ordering {@httpClient.BaseAddress}", httpClient.BaseAddress);
|
var command = MapToOrderDraftCommand(basketData);
|
||||||
|
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
|
||||||
|
_logger.LogDebug(" grpc response: {@response}", response);
|
||||||
|
|
||||||
var client = GrpcClient.Create<OrderingGrpc.OrderingGrpcClient>(httpClient);
|
return MapToResponse(response, basketData);
|
||||||
|
});
|
||||||
_logger.LogDebug(" grpc client created, basketData={@basketData}", basketData);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var command = MapToOrderDraftCommand(basketData);
|
|
||||||
var response = await client.CreateOrderDraftFromBasketDataAsync(command);
|
|
||||||
|
|
||||||
_logger.LogDebug(" grpc response: {@response}", response);
|
|
||||||
|
|
||||||
return MapToResponse(response, basketData);
|
|
||||||
}
|
|
||||||
catch (RpcException e)
|
|
||||||
{
|
|
||||||
_logger.LogError($"Error calling via grpc to ordering: {e.Status} - {e.Message}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
private OrderData MapToResponse(GrpcOrdering.OrderDraftDTO orderDraft, BasketData basketData)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user