Skip to content

Commit

Permalink
Specifying InvariantCulture for IsoDateTimeConverter used by Masstran…
Browse files Browse the repository at this point in the history
…sit in JsonSerializer (#1)
  • Loading branch information
elindanielsson authored Oct 27, 2020
1 parent 0f2b364 commit 82e3c35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion AUTHORS.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Renato Golia
Renato Golia
Elin Danielsson
35 changes: 35 additions & 0 deletions src/MassTransit/Configuration/IServiceBusFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using MassTransit;
using MassTransit.SubscriptionConfigurators;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Nybus.MassTransit;

namespace Nybus.Configuration
Expand Down Expand Up @@ -41,6 +45,10 @@ public IServiceBus CreateServiceBus(MassTransitConnectionDescriptor connectionDe

bus.ReceiveFrom(receiveUri);
bus.UseJsonSerializer();

bus.ConfigureJsonSerializer(ConfigureIsoDateTimeConverters);
bus.ConfigureJsonDeserializer(ConfigureIsoDateTimeConverters);

bus.SetConcurrentConsumerLimit(_concurrencyLimit);

foreach (var subscription in subscriptions)
Expand All @@ -49,6 +57,18 @@ public IServiceBus CreateServiceBus(MassTransitConnectionDescriptor connectionDe
bus.Validate();
});
}

private static JsonSerializerSettings ConfigureIsoDateTimeConverters(JsonSerializerSettings settings)
{
var isoDateTimeConverters = settings.Converters.OfType<IsoDateTimeConverter>().ToArray();

foreach (var converter in isoDateTimeConverters)
{
converter.Culture = CultureInfo.InvariantCulture;
}

return settings;
}
}

public class LoopbackServiceBusFactory : IServiceBusFactory
Expand All @@ -64,12 +84,27 @@ public IServiceBus CreateServiceBus(MassTransitConnectionDescriptor connectionDe
bus.ReceiveFrom(receiveUri);
bus.UseJsonSerializer();

bus.ConfigureJsonSerializer(ConfigureIsoDateTimeConverters);
bus.ConfigureJsonDeserializer(ConfigureIsoDateTimeConverters);

foreach (var subscription in subscriptions)
bus.Subscribe(subscription);

bus.Validate();

});
}

private static JsonSerializerSettings ConfigureIsoDateTimeConverters(JsonSerializerSettings settings)
{
var isoDateTimeConverters = settings.Converters.OfType<IsoDateTimeConverter>().ToArray();

foreach (var converter in isoDateTimeConverters)
{
converter.Culture = CultureInfo.InvariantCulture;
}

return settings;
}
}
}

0 comments on commit 82e3c35

Please sign in to comment.