From 216463f847c113f95b400d9109977818ca9fb4b0 Mon Sep 17 00:00:00 2001 From: Erik Pique Date: Wed, 7 Aug 2019 17:06:20 +0200 Subject: [PATCH] ordering.api error integrationevents --- .../IntegrationEventLogEntry.cs | 7 ++---- .../Services/IntegrationEventLogService.cs | 22 +++++++++---------- .../Controllers/BasketController.cs | 12 +++++++++- src/Services/Catalog/Catalog.API/Startup.cs | 2 +- .../Behaviors/TransactionBehaviour.cs | 2 -- src/Services/Ordering/Ordering.API/Startup.cs | 2 +- .../Ordering.SignalrHub.csproj | 1 + .../Ordering/Ordering.SignalrHub/Startup.cs | 6 ++++- src/Services/Webhooks/Webhooks.API/Startup.cs | 2 +- src/Web/WebMVC/Services/BasketService.cs | 7 +++++- 10 files changed, 39 insertions(+), 24 deletions(-) diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs index ec532d6da..f9cf4a2c1 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs @@ -14,7 +14,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF public class IntegrationEventLogEntry { private IntegrationEventLogEntry() { } - public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId, ILogger logger) + public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId) { EventId = @event.Id; CreationTime = @event.CreationDate; @@ -23,7 +23,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF State = EventStateEnum.NotPublished; TimesSent = 0; TransactionId = transactionId.ToString(); - } public Guid EventId { get; private set; } public string EventTypeName { get; private set; } @@ -37,10 +36,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF public string Content { get; private set; } public string TransactionId { get; private set; } - public IntegrationEventLogEntry DeserializeJsonContent(Type type, ILogger logger) + public IntegrationEventLogEntry DeserializeJsonContent(Type type) { - logger.LogInformation("----- DeserializeJsonContent {Content} {Type}", Content, type); - IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent; return this; } diff --git a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs index b8e9eab9a..04fa84618 100644 --- a/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs +++ b/src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs @@ -18,13 +18,11 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi public class IntegrationEventLogService : IIntegrationEventLogService { private readonly IntegrationEventLogContext _integrationEventLogContext; - private readonly ILogger _logger; private readonly DbConnection _dbConnection; private readonly List _eventTypes; - public IntegrationEventLogService(DbConnection dbConnection, ILogger logger) + public IntegrationEventLogService(DbConnection dbConnection) { - _logger =logger; _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); _integrationEventLogContext = new IntegrationEventLogContext( new DbContextOptionsBuilder() @@ -40,22 +38,24 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi public async Task> RetrieveEventLogsPendingToPublishAsync(Guid transactionId) { - _logger.LogInformation("----- RetrieveEventLogsPendingToPublishAsync {TransactionId}", transactionId); - var tid = transactionId.ToString(); - return await _integrationEventLogContext.IntegrationEventLogs - .Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished) - .OrderBy(o => o.CreationTime) - .Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName), _logger)) - .ToListAsync(); + var result = await _integrationEventLogContext.IntegrationEventLogs + .Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished).ToListAsync(); + + if(result != null && result.Any()){ + return result.OrderBy(o => o.CreationTime) + .Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName))); + } + + return new List(); } public Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction) { if (transaction == null) throw new ArgumentNullException(nameof(transaction)); - var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId, _logger); + var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId); _integrationEventLogContext.Database.UseTransaction(transaction.GetDbTransaction()); _integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry); diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 0bf15fc42..629369dd1 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -7,7 +7,9 @@ using Microsoft.eShopOnContainers.Services.Basket.API.Model; using Microsoft.eShopOnContainers.Services.Basket.API.Services; using Microsoft.Extensions.Logging; using System; +using System.Linq; using System.Net; +using System.Security.Claims; using System.Threading.Tasks; namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers @@ -61,6 +63,8 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ? guid : basketCheckout.RequestId; +_logger.LogInformation("----- CheckoutAsync userId: {userId} ", userId); + var basket = await _repository.GetBasketAsync(userId); if (basket == null) @@ -68,7 +72,13 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers return BadRequest(); } - var userName = User.FindFirst(x => x.Type == "unique_name").Value; +_logger.LogInformation("----- CheckoutAsync basket: {@basket} ", basket); + +_logger.LogInformation("----- CheckoutAsync user identity: {User} ", string.Join(':', ((ClaimsIdentity)User.Identity).Claims.Select(c => c.Type + " " + c.Value))); + + var userName = User.FindFirst(x => x.Type == ClaimTypes.Name).Value; + + _logger.LogInformation("----- CheckoutAsync userName: {@userName} ", userName); var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, userName, basketCheckout.City, basketCheckout.Street, basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName, diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 451d5d5cf..64441f9ea 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -277,7 +277,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) { services.AddTransient>( - sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService>())); + sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient(); diff --git a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs index 0320cbd97..be32daeee 100644 --- a/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs +++ b/src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs @@ -56,8 +56,6 @@ namespace Ordering.API.Application.Behaviors await _dbContext.CommitTransactionAsync(transaction); transactionId = transaction.TransactionId; - - _logger.LogInformation("----- End transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName); } await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId); diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index 572c83320..fca490f28 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -273,7 +273,7 @@ services.AddSingleton(); services.AddTransient(); services.AddTransient>( - sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService>())); + sp => (DbConnection c) => new IntegrationEventLogService(c)); services.AddTransient(); diff --git a/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj b/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj index 3da5b61f7..67ad7c66f 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj +++ b/src/Services/Ordering/Ordering.SignalrHub/Ordering.SignalrHub.csproj @@ -27,6 +27,7 @@ + diff --git a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs index 456e73397..b3168e5d7 100644 --- a/src/Services/Ordering/Ordering.SignalrHub/Startup.cs +++ b/src/Services/Ordering/Ordering.SignalrHub/Startup.cs @@ -109,7 +109,7 @@ namespace Ordering.SignalrHub RegisterEventBus(services); services.AddOptions(); - + //configure autofac var container = new ContainerBuilder(); container.RegisterModule(new ApplicationModule()); @@ -136,6 +136,10 @@ namespace Ordering.SignalrHub app.UseCors("CorsPolicy"); app.UseRouting(); + + //app.UseAuthorization(); + app.UseAutentication(); + app.UseEndpoints(endpoints => { endpoints.MapHealthChecks("/hc", new HealthCheckOptions() diff --git a/src/Services/Webhooks/Webhooks.API/Startup.cs b/src/Services/Webhooks/Webhooks.API/Startup.cs index 1d454a7ab..5859e9e12 100644 --- a/src/Services/Webhooks/Webhooks.API/Startup.cs +++ b/src/Services/Webhooks/Webhooks.API/Startup.cs @@ -291,7 +291,7 @@ namespace Webhooks.API public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) { services.AddTransient>( - sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService>())); + sp => (DbConnection c) => new IntegrationEventLogService(c)); if (configuration.GetValue("AzureServiceBusEnabled")) { diff --git a/src/Web/WebMVC/Services/BasketService.cs b/src/Web/WebMVC/Services/BasketService.cs index dd0a50feb..e9f015e54 100644 --- a/src/Web/WebMVC/Services/BasketService.cs +++ b/src/Web/WebMVC/Services/BasketService.cs @@ -1,4 +1,5 @@ using Microsoft.eShopOnContainers.WebMVC.ViewModels; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using System.Collections.Generic; @@ -14,15 +15,17 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services { private readonly IOptions _settings; private readonly HttpClient _apiClient; + private readonly ILogger _logger; private readonly string _basketByPassUrl; private readonly string _purchaseUrl; private readonly string _bffUrl; - public BasketService(HttpClient httpClient, IOptions settings) + public BasketService(HttpClient httpClient, IOptions settings, ILogger logger) { _apiClient = httpClient; _settings = settings; + _logger =logger; _basketByPassUrl = $"{_settings.Value.PurchaseUrl}/api/v1/b/basket"; _purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1"; @@ -57,6 +60,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services var uri = API.Basket.CheckoutBasket(_basketByPassUrl); var basketContent = new StringContent(JsonConvert.SerializeObject(basket), System.Text.Encoding.UTF8, "application/json"); + _logger.LogInformation("Uri chechout {uri}", uri); + var response = await _apiClient.PostAsync(uri, basketContent); response.EnsureSuccessStatusCode();