ordering.api error integrationevents
This commit is contained in:
		
							parent
							
								
									4a30a1cc9e
								
							
						
					
					
						commit
						216463f847
					
				| @ -14,7 +14,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF | |||||||
|     public class IntegrationEventLogEntry |     public class IntegrationEventLogEntry | ||||||
|     { |     { | ||||||
|         private IntegrationEventLogEntry() { } |         private IntegrationEventLogEntry() { } | ||||||
|         public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId, ILogger<IntegrationEventLogService> logger) |         public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId) | ||||||
|         { |         { | ||||||
|             EventId = @event.Id;             |             EventId = @event.Id;             | ||||||
|             CreationTime = @event.CreationDate; |             CreationTime = @event.CreationDate; | ||||||
| @ -23,7 +23,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF | |||||||
|             State = EventStateEnum.NotPublished; |             State = EventStateEnum.NotPublished; | ||||||
|             TimesSent = 0; |             TimesSent = 0; | ||||||
|             TransactionId = transactionId.ToString(); |             TransactionId = transactionId.ToString(); | ||||||
| 
 |  | ||||||
|         } |         } | ||||||
|         public Guid EventId { get; private set; } |         public Guid EventId { get; private set; } | ||||||
|         public string EventTypeName { 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 Content { get; private set; } | ||||||
|         public string TransactionId { get; private set; } |         public string TransactionId { get; private set; } | ||||||
| 
 | 
 | ||||||
|         public IntegrationEventLogEntry DeserializeJsonContent(Type type, ILogger<IntegrationEventLogService> logger) |         public IntegrationEventLogEntry DeserializeJsonContent(Type type) | ||||||
|         { |         { | ||||||
|             logger.LogInformation("----- DeserializeJsonContent {Content} {Type}", Content, type); |  | ||||||
| 
 |  | ||||||
|             IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent; |             IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent; | ||||||
|             return this; |             return this; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -18,13 +18,11 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi | |||||||
|     public class IntegrationEventLogService : IIntegrationEventLogService |     public class IntegrationEventLogService : IIntegrationEventLogService | ||||||
|     { |     { | ||||||
|         private readonly IntegrationEventLogContext _integrationEventLogContext; |         private readonly IntegrationEventLogContext _integrationEventLogContext; | ||||||
|         private readonly ILogger<IntegrationEventLogService> _logger; |  | ||||||
|         private readonly DbConnection _dbConnection; |         private readonly DbConnection _dbConnection; | ||||||
|         private readonly List<Type> _eventTypes; |         private readonly List<Type> _eventTypes; | ||||||
| 
 | 
 | ||||||
|         public IntegrationEventLogService(DbConnection dbConnection, ILogger<IntegrationEventLogService> logger) |         public IntegrationEventLogService(DbConnection dbConnection) | ||||||
|         { |         { | ||||||
|             _logger =logger; |  | ||||||
|             _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); |             _dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection)); | ||||||
|             _integrationEventLogContext = new IntegrationEventLogContext( |             _integrationEventLogContext = new IntegrationEventLogContext( | ||||||
|                 new DbContextOptionsBuilder<IntegrationEventLogContext>() |                 new DbContextOptionsBuilder<IntegrationEventLogContext>() | ||||||
| @ -40,22 +38,24 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi | |||||||
| 
 | 
 | ||||||
|         public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync(Guid transactionId) |         public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync(Guid transactionId) | ||||||
|         { |         { | ||||||
|             _logger.LogInformation("----- RetrieveEventLogsPendingToPublishAsync {TransactionId}", transactionId); |  | ||||||
| 
 |  | ||||||
|             var tid = transactionId.ToString(); |             var tid = transactionId.ToString(); | ||||||
| 
 | 
 | ||||||
|             return await _integrationEventLogContext.IntegrationEventLogs |             var result = await _integrationEventLogContext.IntegrationEventLogs | ||||||
|                 .Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished) |                 .Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished).ToListAsync(); | ||||||
|                 .OrderBy(o => o.CreationTime) | 
 | ||||||
|                 .Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName), _logger)) |             if(result != null && result.Any()){ | ||||||
|                 .ToListAsync();               |                 return result.OrderBy(o => o.CreationTime) | ||||||
|  |                     .Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName))); | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             return new List<IntegrationEventLogEntry>(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction) |         public Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction) | ||||||
|         { |         { | ||||||
|             if (transaction == null) throw new ArgumentNullException(nameof(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.Database.UseTransaction(transaction.GetDbTransaction()); | ||||||
|             _integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry); |             _integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry); | ||||||
|  | |||||||
| @ -7,7 +7,9 @@ using Microsoft.eShopOnContainers.Services.Basket.API.Model; | |||||||
| using Microsoft.eShopOnContainers.Services.Basket.API.Services; | using Microsoft.eShopOnContainers.Services.Basket.API.Services; | ||||||
| using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||||
| using System; | using System; | ||||||
|  | using System.Linq; | ||||||
| using System.Net; | using System.Net; | ||||||
|  | using System.Security.Claims; | ||||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||||
| 
 | 
 | ||||||
| namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers | 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) ? |             basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ? | ||||||
|                 guid : basketCheckout.RequestId; |                 guid : basketCheckout.RequestId; | ||||||
| 
 | 
 | ||||||
|  | _logger.LogInformation("----- CheckoutAsync userId: {userId} ", userId); | ||||||
|  | 
 | ||||||
|             var basket = await _repository.GetBasketAsync(userId); |             var basket = await _repository.GetBasketAsync(userId); | ||||||
| 
 | 
 | ||||||
|             if (basket == null) |             if (basket == null) | ||||||
| @ -68,7 +72,13 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers | |||||||
|                 return BadRequest(); |                 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, |             var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, userName, basketCheckout.City, basketCheckout.Street, | ||||||
|                 basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName, |                 basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName, | ||||||
|  | |||||||
| @ -277,7 +277,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API | |||||||
|         public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) |         public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) | ||||||
|         { |         { | ||||||
|             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( |             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( | ||||||
|                 sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>())); |                 sp => (DbConnection c) => new IntegrationEventLogService(c)); | ||||||
| 
 | 
 | ||||||
|             services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>(); |             services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -56,8 +56,6 @@ namespace Ordering.API.Application.Behaviors | |||||||
|                         await _dbContext.CommitTransactionAsync(transaction); |                         await _dbContext.CommitTransactionAsync(transaction); | ||||||
| 
 | 
 | ||||||
|                         transactionId = transaction.TransactionId; |                         transactionId = transaction.TransactionId; | ||||||
| 
 |  | ||||||
|                         _logger.LogInformation("----- End transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName); |  | ||||||
|                     } |                     } | ||||||
| 
 | 
 | ||||||
|                     await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId); |                     await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId); | ||||||
|  | |||||||
| @ -273,7 +273,7 @@ | |||||||
|             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |             services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); | ||||||
|             services.AddTransient<IIdentityService, IdentityService>(); |             services.AddTransient<IIdentityService, IdentityService>(); | ||||||
|             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( |             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( | ||||||
|                 sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>())); |                 sp => (DbConnection c) => new IntegrationEventLogService(c)); | ||||||
| 
 | 
 | ||||||
|             services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>(); |             services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -27,6 +27,7 @@ | |||||||
|     <PackageReference Include="Microsoft.AspNetCore.SignalR.Redis" Version="$(Microsoft_AspNetCore_SignalR_Redis)" /> |     <PackageReference Include="Microsoft.AspNetCore.SignalR.Redis" Version="$(Microsoft_AspNetCore_SignalR_Redis)" /> | ||||||
|     <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="$(Microsoft_Extensions_Logging_AzureAppServices)" /> |     <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="$(Microsoft_Extensions_Logging_AzureAppServices)" /> | ||||||
|     <PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_AspNetCore)" /> |     <PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_AspNetCore)" /> | ||||||
|  |     <PackageReference Include="Microsoft.AspNetCore.App" Version="$(Microsoft_AspNetCore_App)" /> | ||||||
|     <PackageReference Include="Serilog.Enrichers.Environment" Version="$(Serilog_Enrichers_Environment)" /> |     <PackageReference Include="Serilog.Enrichers.Environment" Version="$(Serilog_Enrichers_Environment)" /> | ||||||
|     <PackageReference Include="Serilog.Settings.Configuration" Version="$(Serilog_Settings_Configuration)" /> |     <PackageReference Include="Serilog.Settings.Configuration" Version="$(Serilog_Settings_Configuration)" /> | ||||||
|     <PackageReference Include="Serilog.Sinks.Console" Version="$(Serilog_Sinks_Console)" /> |     <PackageReference Include="Serilog.Sinks.Console" Version="$(Serilog_Sinks_Console)" /> | ||||||
|  | |||||||
| @ -109,7 +109,7 @@ namespace Ordering.SignalrHub | |||||||
|             RegisterEventBus(services); |             RegisterEventBus(services); | ||||||
| 
 | 
 | ||||||
|             services.AddOptions(); |             services.AddOptions(); | ||||||
| 
 |              | ||||||
|             //configure autofac |             //configure autofac | ||||||
|             var container = new ContainerBuilder(); |             var container = new ContainerBuilder(); | ||||||
|             container.RegisterModule(new ApplicationModule()); |             container.RegisterModule(new ApplicationModule()); | ||||||
| @ -136,6 +136,10 @@ namespace Ordering.SignalrHub | |||||||
| 
 | 
 | ||||||
|             app.UseCors("CorsPolicy"); |             app.UseCors("CorsPolicy"); | ||||||
|             app.UseRouting(); |             app.UseRouting(); | ||||||
|  | 
 | ||||||
|  |             //app.UseAuthorization(); | ||||||
|  |             app.UseAutentication(); | ||||||
|  | 
 | ||||||
|             app.UseEndpoints(endpoints => |             app.UseEndpoints(endpoints => | ||||||
|             { |             { | ||||||
|                 endpoints.MapHealthChecks("/hc", new HealthCheckOptions() |                 endpoints.MapHealthChecks("/hc", new HealthCheckOptions() | ||||||
|  | |||||||
| @ -291,7 +291,7 @@ namespace Webhooks.API | |||||||
|         public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) |         public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration) | ||||||
|         { |         { | ||||||
|             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( |             services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>( | ||||||
|                 sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>())); |                 sp => (DbConnection c) => new IntegrationEventLogService(c)); | ||||||
| 
 | 
 | ||||||
|             if (configuration.GetValue<bool>("AzureServiceBusEnabled")) |             if (configuration.GetValue<bool>("AzureServiceBusEnabled")) | ||||||
|             { |             { | ||||||
|  | |||||||
| @ -1,4 +1,5 @@ | |||||||
| using Microsoft.eShopOnContainers.WebMVC.ViewModels; | using Microsoft.eShopOnContainers.WebMVC.ViewModels; | ||||||
|  | using Microsoft.Extensions.Logging; | ||||||
| using Microsoft.Extensions.Options; | using Microsoft.Extensions.Options; | ||||||
| using Newtonsoft.Json; | using Newtonsoft.Json; | ||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| @ -14,15 +15,17 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services | |||||||
|     { |     { | ||||||
|         private readonly IOptions<AppSettings> _settings; |         private readonly IOptions<AppSettings> _settings; | ||||||
|         private readonly HttpClient _apiClient; |         private readonly HttpClient _apiClient; | ||||||
|  |         private readonly ILogger<BasketService> _logger; | ||||||
|         private readonly string _basketByPassUrl; |         private readonly string _basketByPassUrl; | ||||||
|         private readonly string _purchaseUrl; |         private readonly string _purchaseUrl; | ||||||
| 
 | 
 | ||||||
|         private readonly string _bffUrl; |         private readonly string _bffUrl; | ||||||
| 
 | 
 | ||||||
|         public BasketService(HttpClient httpClient, IOptions<AppSettings> settings) |         public BasketService(HttpClient httpClient, IOptions<AppSettings> settings, ILogger<BasketService> logger) | ||||||
|         { |         { | ||||||
|             _apiClient = httpClient; |             _apiClient = httpClient; | ||||||
|             _settings = settings; |             _settings = settings; | ||||||
|  |             _logger =logger; | ||||||
| 
 | 
 | ||||||
|             _basketByPassUrl = $"{_settings.Value.PurchaseUrl}/api/v1/b/basket"; |             _basketByPassUrl = $"{_settings.Value.PurchaseUrl}/api/v1/b/basket"; | ||||||
|             _purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1"; |             _purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1"; | ||||||
| @ -57,6 +60,8 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services | |||||||
|             var uri = API.Basket.CheckoutBasket(_basketByPassUrl); |             var uri = API.Basket.CheckoutBasket(_basketByPassUrl); | ||||||
|             var basketContent = new StringContent(JsonConvert.SerializeObject(basket), System.Text.Encoding.UTF8, "application/json"); |             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); |             var response = await _apiClient.PostAsync(uri, basketContent); | ||||||
| 
 | 
 | ||||||
|             response.EnsureSuccessStatusCode(); |             response.EnsureSuccessStatusCode(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user