dotnetcore / CAP

Distributed transaction solution in micro-service base on eventually consistency, also an eventbus with Outbox pattern
http://cap.dotnetcore.xyz
MIT License
6.62k stars 1.28k forks source link

Datetime Parsing Error When Producer and Consumer Have Different Cultures. #1588

Open NikolozGob opened 4 days ago

NikolozGob commented 4 days ago

Hello, we have encountered datetime parsing error when we try to publish delayed message with transaction.

We are using Kafka + PostgreSQL.

This is the error: String '25.09.2024 10:55:38' was not recognized as a valid DateTime.

Stack Trace: at DotNetCore.CAP.CapTransactionBase.Flush() at DotNetCore.CAP.PostgreSqlCapTransaction.CommitAsync(CancellationToken cancellationToken) at Space.Service.Common.EventBus.EventBusTransaction.Commit(CancellationToken cancellationToken) at Space.Service.Card.Application.Features.PhysicalCard.Command.CreatePhysicalCardAsync.CreatePhysicalCardAsyncCommandHandler.Handle(CreatePhysicalCardAsyncCommand request, CancellationToken cancellationToken) in /home/runner01/actions-runner/_work/Space.Service.Card/Space.Service.Card/Space.Service.Card.Application/Features/PhysicalCard/Command/CreatePhysicalCardAsync/CreatePhysicalCardAsyncCommandHandler.cs:line 207

Error Happens here:

image

_dispatcher.EnqueueToScheduler(message, DateTime.Parse(message.Origin.Headers[Headers.SentTime]!)).ConfigureAwait(false);

DateTime.Parse throws an error.

It happens because when this event was produced, CurrentCulture was set to 'ru-RU' and "cap-senttime" was set to string in russian format.

When event was consumed and it tried to parse this datetime, culture info was set to 'en-EN' and parsing russian format throws exception.

Steps to reproduce: PUBLISHER:

  1. Set Culture Info To Russian.
  2. Publish Event.

CONSUMER:

  1. Set Culture Info to English
  2. Consume Event
  3. Open ICapTransaction
  4. Publish Delayed Event
  5. Commit Transaction

POSSIBLE FIXES: DateTime.ToString() and DateTime.Parse() should be called with Invariant Culture

yang-xiaodong commented 4 days ago

Hi @NikolozGob ,

Thanks for the feedback, Would you want to make a PR?

NikolozGob commented 4 days ago

Yes, I'll make PR