diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Dockerfile b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Dockerfile index 5f36895de..e76e65204 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Dockerfile +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Dockerfile @@ -1,8 +1,8 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base +FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 -FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src # It's important to keep lines from here down to "COPY . ." identical in all Dockerfiles diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs index aa1d5ce6a..e74a4b2f5 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs @@ -1,7 +1,6 @@ using Grpc.Core; using Grpc.Core.Interceptors; using Microsoft.Extensions.Logging; -using System; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastructure @@ -30,7 +29,6 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Infrastruct try { var response = await t; - _logger.LogDebug($"Response received: {response}"); return response; } catch (RpcException e) diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj index 09c238f9d..299646bcb 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Mobile.Shopping.HttpAggregator.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 Mobile.Shopping.HttpAggregator Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator ..\..\..\docker-compose.dcproj @@ -15,20 +15,20 @@ - - - - - - - - + + + + + + + + - - - + + + - + diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/CatalogService.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/CatalogService.cs index f98b90989..0101a7058 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/CatalogService.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Services/CatalogService.cs @@ -1,48 +1,32 @@ using CatalogApi; -using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Config; using Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Models; -using Microsoft.Extensions.Options; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; -using static CatalogApi.Catalog; namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator.Services { public class CatalogService : ICatalogService { - private readonly HttpClient _httpClient; - private readonly UrlsConfig _urls; + private readonly Catalog.CatalogClient _client; - public CatalogService(HttpClient httpClient, IOptions config) + public CatalogService(Catalog.CatalogClient client) { - _httpClient = httpClient; - _urls = config.Value; + _client = client; } public async Task GetCatalogItemAsync(int id) { - - return await GrpcCallerService.CallService(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id), async channel => - { - var client = new CatalogClient(channel); - var request = new CatalogItemRequest { Id = id }; - var response = await client.GetItemByIdAsync(request); - return MapToCatalogItemResponse(response); - }); + var request = new CatalogItemRequest { Id = id }; + var response = await _client.GetItemByIdAsync(request); + return MapToCatalogItemResponse(response); } public async Task> GetCatalogItemsAsync(IEnumerable ids) { - - return await GrpcCallerService.CallService(_urls.GrpcCatalog, async channel=> - { - var client = new CatalogClient(channel); - var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 }; - var response = await client.GetItemsByIdsAsync(request); - return response.Data.Select(this.MapToCatalogItemResponse); - }); + var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 }; + var response = await _client.GetItemsByIdsAsync(request); + return response.Data.Select(MapToCatalogItemResponse); } private CatalogItem MapToCatalogItemResponse(CatalogItemResponse catalogItemResponse) diff --git a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs index 62e8006cf..4f6e18d7c 100644 --- a/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Mobile.Bff.Shopping/aggregator/Startup.cs @@ -1,4 +1,5 @@ -using Devspaces.Support; +using CatalogApi; +using Devspaces.Support; using GrpcBasket; using GrpcOrdering; using HealthChecks.UI.Client; @@ -182,9 +183,6 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator //register http services - services.AddHttpClient() - .AddDevspacesSupport(); - services.AddHttpClient() .AddDevspacesSupport(); @@ -193,7 +191,7 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator public static IServiceCollection AddGrpcServices(this IServiceCollection services) { - services.AddSingleton(); + services.AddTransient(); services.AddScoped(); @@ -203,6 +201,14 @@ namespace Microsoft.eShopOnContainers.Mobile.Shopping.HttpAggregator options.Address = new Uri(basketApi); }).AddInterceptor(); + services.AddScoped(); + + services.AddGrpcClient((services, options) => + { + var catalogApi = services.GetRequiredService>().Value.GrpcCatalog; + options.Address = new Uri(catalogApi); + }).AddInterceptor(); + services.AddScoped(); services.AddGrpcClient((services, options) => diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs index 3120a3567..dc6b7e6f8 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Infrastructure/GrpcExceptionInterceptor.cs @@ -1,7 +1,6 @@ using Grpc.Core; using Grpc.Core.Interceptors; using Microsoft.Extensions.Logging; -using System; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Infrastructure @@ -30,7 +29,6 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Infrastructure try { var response = await t; - _logger.LogDebug($"Response received: {response}"); return response; } catch (RpcException e) diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs index 059e1d046..f925954a6 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs @@ -1,53 +1,41 @@ using CatalogApi; -using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Config; using Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Models; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System.Collections.Generic; using System.Linq; -using System.Net.Http; using System.Threading.Tasks; -using static CatalogApi.Catalog; namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services { public class CatalogService : ICatalogService { - private readonly HttpClient _httpClient; + private readonly Catalog.CatalogClient _client; private readonly ILogger _logger; - private readonly UrlsConfig _urls; - public CatalogService(HttpClient httpClient, ILogger logger, IOptions config) + public CatalogService(Catalog.CatalogClient client, ILogger logger) { - _httpClient = httpClient; + _client = client; _logger = logger; - _urls = config.Value; } public async Task GetCatalogItemAsync(int id) { - return await GrpcCallerService.CallService(_urls.GrpcCatalog, async channel => - { - var client = new CatalogClient(channel); - var request = new CatalogItemRequest { Id = id }; - _logger.LogInformation("grpc client created, request = {@request}", request); - var response = await client.GetItemByIdAsync(request); - _logger.LogInformation("grpc response {@response}", response); - return MapToCatalogItemResponse(response); - }); + var request = new CatalogItemRequest { Id = id }; + _logger.LogInformation("grpc request {@request}", request); + var response = await _client.GetItemByIdAsync(request); + _logger.LogInformation("grpc response {@response}", response); + return MapToCatalogItemResponse(response); + } public async Task> GetCatalogItemsAsync(IEnumerable ids) { - return await GrpcCallerService.CallService(_urls.GrpcCatalog, async channel => - { - var client = new CatalogClient(channel); - 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); - }); + var request = new CatalogItemsRequest { Ids = string.Join(",", ids), PageIndex = 1, PageSize = 10 }; + _logger.LogInformation("grpc request {@request}", request); + var response = await _client.GetItemsByIdsAsync(request); + _logger.LogInformation("grpc response {@response}", response); + return response.Data.Select(this.MapToCatalogItemResponse); + } private CatalogItem MapToCatalogItemResponse(CatalogItemResponse catalogItemResponse) diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs index c61d62109..5c586b324 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Startup.cs @@ -1,4 +1,5 @@ -using Devspaces.Support; +using CatalogApi; +using Devspaces.Support; using GrpcBasket; using GrpcOrdering; using HealthChecks.UI.Client; @@ -185,9 +186,6 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator //register http services - services.AddHttpClient() - .AddDevspacesSupport(); - services.AddHttpClient() .AddHttpMessageHandler() .AddDevspacesSupport(); @@ -197,7 +195,7 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator public static IServiceCollection AddGrpcServices(this IServiceCollection services) { - services.AddSingleton(); + services.AddTransient(); services.AddScoped(); @@ -207,6 +205,14 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator options.Address = new Uri(basketApi); }).AddInterceptor(); + services.AddScoped(); + + services.AddGrpcClient((services, options) => + { + var catalogApi = services.GetRequiredService>().Value.GrpcCatalog; + options.Address = new Uri(catalogApi); + }).AddInterceptor(); + services.AddScoped(); services.AddGrpcClient((services, options) => diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index a2c32d99b..7e4cd443d 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 portable true Catalog.API @@ -42,29 +42,30 @@ - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + - + - + + diff --git a/src/Services/Catalog/Catalog.API/CatalogSettings.cs b/src/Services/Catalog/Catalog.API/CatalogSettings.cs index a8c47991e..b50b1227e 100644 --- a/src/Services/Catalog/Catalog.API/CatalogSettings.cs +++ b/src/Services/Catalog/Catalog.API/CatalogSettings.cs @@ -2,11 +2,11 @@ { public class CatalogSettings { - public string PicBaseUrl { get;set;} + public string PicBaseUrl { get; set; } public string EventBusConnection { get; set; } public bool UseCustomizationData { get; set; } - public bool AzureStorageEnabled { get; set; } + public bool AzureStorageEnabled { get; set; } } } diff --git a/src/Services/Catalog/Catalog.API/Dockerfile b/src/Services/Catalog/Catalog.API/Dockerfile index 4a1f9d2f9..844d7c1b5 100644 --- a/src/Services/Catalog/Catalog.API/Dockerfile +++ b/src/Services/Catalog/Catalog.API/Dockerfile @@ -1,9 +1,9 @@ -FROM mcr.microsoft.com/dotnet/core/aspnet:3.1 AS base +FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base WORKDIR /app EXPOSE 80 EXPOSE 443 -FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build WORKDIR /src # It's important to keep lines from here down to "COPY . ." identical in all Dockerfiles diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index 3632609b4..1507b1d8e 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -1,9 +1,9 @@ -using Autofac.Extensions.DependencyInjection; -using Catalog.API.Extensions; +using Catalog.API.Extensions; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; +using Microsoft.eShopOnContainers.Services.Catalog.API; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -12,121 +12,113 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Serilog; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net; -namespace Microsoft.eShopOnContainers.Services.Catalog.API +var configuration = GetConfiguration(); + +Log.Logger = CreateSerilogLogger(configuration); + +try { - public class Program + Log.Information("Configuring web host ({ApplicationContext})...", Program.AppName); + var host = CreateHostBuilder(configuration, args); + + Log.Information("Applying migrations ({ApplicationContext})...", Program.AppName); + host.MigrateDbContext((context, services) => { - public static readonly string Namespace = typeof(Program).Namespace; - public static readonly string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); - - public static int Main(string[] args) - { - var configuration = GetConfiguration(); - - Log.Logger = CreateSerilogLogger(configuration); - - try - { - Log.Information("Configuring web host ({ApplicationContext})...", AppName); - var host = CreateHostBuilder(configuration, args); - - Log.Information("Applying migrations ({ApplicationContext})...", AppName); - host.MigrateDbContext((context, services) => - { - var env = services.GetService(); - var settings = services.GetService>(); - var logger = services.GetService>(); - - new CatalogContextSeed() - .SeedAsync(context, env, settings, logger) - .Wait(); - }) - .MigrateDbContext((_, __) => { }); - - Log.Information("Starting web host ({ApplicationContext})...", AppName); - host.Run(); - - return 0; - } - catch (Exception ex) - { - Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); - return 1; - } - finally - { - Log.CloseAndFlush(); - } - } - - private static IWebHost CreateHostBuilder(IConfiguration configuration, string[] args) => - WebHost.CreateDefaultBuilder(args) - .ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) - .CaptureStartupErrors(false) - .ConfigureKestrel(options => - { - var ports = GetDefinedPorts(configuration); - options.Listen(IPAddress.Any, ports.httpPort, listenOptions => - { - listenOptions.Protocols = HttpProtocols.Http1AndHttp2; - }); - options.Listen(IPAddress.Any, ports.grpcPort, listenOptions => - { - listenOptions.Protocols = HttpProtocols.Http2; - }); - - }) - .UseStartup() - .UseContentRoot(Directory.GetCurrentDirectory()) - .UseWebRoot("Pics") - .UseSerilog() - .Build(); - - private static Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) - { - var seqServerUrl = configuration["Serilog:SeqServerUrl"]; - var logstashUrl = configuration["Serilog:LogstashgUrl"]; - return new LoggerConfiguration() - .MinimumLevel.Verbose() - .Enrich.WithProperty("ApplicationContext", AppName) - .Enrich.FromLogContext() - .WriteTo.Console() - .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) - .WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl) - .ReadFrom.Configuration(configuration) - .CreateLogger(); - } - - private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config) - { - var grpcPort = config.GetValue("GRPC_PORT", 81); - var port = config.GetValue("PORT", 80); - return (port, grpcPort); - } - - private static IConfiguration GetConfiguration() - { - var builder = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .AddEnvironmentVariables(); - - var config = builder.Build(); - - if (config.GetValue("UseVault", false)) - { - builder.AddAzureKeyVault( - $"https://{config["Vault:Name"]}.vault.azure.net/", - config["Vault:ClientId"], - config["Vault:ClientSecret"]); - } - - return builder.Build(); - } + var env = services.GetService(); + var settings = services.GetService>(); + var logger = services.GetService>(); + + new CatalogContextSeed() + .SeedAsync(context, env, settings, logger) + .Wait(); + }) + .MigrateDbContext((_, __) => { }); + + Log.Information("Starting web host ({ApplicationContext})...", Program.AppName); + host.Run(); + + return 0; +} +catch (Exception ex) +{ + Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", Program.AppName); + return 1; +} +finally +{ + Log.CloseAndFlush(); +} + +IWebHost CreateHostBuilder(IConfiguration configuration, string[] args) => + WebHost.CreateDefaultBuilder(args) + .ConfigureAppConfiguration(x => x.AddConfiguration(configuration)) + .CaptureStartupErrors(false) + .ConfigureKestrel(options => + { + var ports = GetDefinedPorts(configuration); + options.Listen(IPAddress.Any, ports.httpPort, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http1AndHttp2; + }); + options.Listen(IPAddress.Any, ports.grpcPort, listenOptions => + { + listenOptions.Protocols = HttpProtocols.Http2; + }); + + }) + .UseStartup() + .UseContentRoot(Directory.GetCurrentDirectory()) + .UseWebRoot("Pics") + .UseSerilog() + .Build(); + +Serilog.ILogger CreateSerilogLogger(IConfiguration configuration) +{ + var seqServerUrl = configuration["Serilog:SeqServerUrl"]; + var logstashUrl = configuration["Serilog:LogstashgUrl"]; + return new LoggerConfiguration() + .MinimumLevel.Verbose() + .Enrich.WithProperty("ApplicationContext", Program.AppName) + .Enrich.FromLogContext() + .WriteTo.Console() + .WriteTo.Seq(string.IsNullOrWhiteSpace(seqServerUrl) ? "http://seq" : seqServerUrl) + .WriteTo.Http(string.IsNullOrWhiteSpace(logstashUrl) ? "http://logstash:8080" : logstashUrl) + .ReadFrom.Configuration(configuration) + .CreateLogger(); +} + +(int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config) +{ + var grpcPort = config.GetValue("GRPC_PORT", 81); + var port = config.GetValue("PORT", 80); + return (port, grpcPort); +} + +IConfiguration GetConfiguration() +{ + var builder = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .AddEnvironmentVariables(); + + var config = builder.Build(); + + if (config.GetValue("UseVault", false)) + { + builder.AddAzureKeyVault( + $"https://{config["Vault:Name"]}.vault.azure.net/", + config["Vault:ClientId"], + config["Vault:ClientSecret"]); } + + return builder.Build(); } + +public static class Program +{ + public static string Namespace = typeof(Startup).Namespace; + public static string AppName = Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1); +} \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj b/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj index a1e04922e..245f889dc 100644 --- a/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj +++ b/src/Services/Catalog/Catalog.FunctionalTests/Catalog.FunctionalTests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 false diff --git a/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj b/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj index 7c64276b4..f92c1a0d2 100644 --- a/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj +++ b/src/Services/Catalog/Catalog.UnitTests/Catalog.UnitTests.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1 + net5.0 false false