Skip to content

Commit 5787dbd

Browse files
authored
Stop setting a default serializer (#6923)
* Stop setting a default serializer * Update existing tests to configure serializer * Add new test
1 parent d607a9a commit 5787dbd

10 files changed

+55
-28
lines changed

src/NServiceBus.AcceptanceTests/Core/SubscriptionMigration/AcceptanceTestingTransportServer.cs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public async Task<EndpointConfiguration> GetConfiguration(RunDescriptor runDescr
3232
await persistenceConfiguration.Configure(endpointConfiguration.EndpointName, configuration, runDescriptor.Settings, endpointConfiguration.PublisherMetadata);
3333
runDescriptor.OnTestCompleted(_ => persistenceConfiguration.Cleanup());
3434

35+
configuration.UseSerialization<SystemJsonSerializer>();
36+
3537
await configurationBuilderCustomization(configuration);
3638

3739
// scan types at the end so that all types used by the configuration have been loaded into the AppDomain

src/NServiceBus.AcceptanceTests/EndpointTemplates/ServerWithNoDefaultPersistenceDefinitions.cs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public virtual async Task<EndpointConfiguration> GetConfiguration(RunDescriptor
2121

2222
await builder.DefineTransport(TransportConfiguration, runDescriptor, endpointConfiguration).ConfigureAwait(false);
2323

24+
builder.UseSerialization<SystemJsonSerializer>();
25+
2426
await configurationBuilderCustomization(builder).ConfigureAwait(false);
2527

2628
// scan types at the end so that all types used by the configuration have been loaded into the AppDomain

src/NServiceBus.AcceptanceTests/Serialization/When_defining_serializer_with_no_content_type.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class When_defining_serializer_with_no_content_type : NServiceBusAcceptanceTest
1414
{
1515
[Test]
16-
public void Should_fail_endpoint_startup_for_default_serializer()
16+
public void Should_fail_endpoint_startup_for_main_serializer()
1717
{
1818
var exception = Assert.ThrowsAsync<ArgumentException>(() => Scenario.Define<ScenarioContext>()
1919
.WithEndpoint<EndpointWithInvalidSerializer>(e => e

src/NServiceBus.Core.Tests/Config/When_configuring_transport_twice.cs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public async Task Last_one_wins()
1515
{
1616
var config = new EndpointConfiguration("Endpoint");
1717
config.AssemblyScanner().ExcludeAssemblies("NServiceBus.Core.Tests.dll");
18+
config.UseSerialization<SystemJsonSerializer>();
1819
var transport1 = new FakeTransportDefinition();
1920
var transport2 = new FakeTransportDefinition();
2021
config.UseTransport(transport1).DisablePublishing();

src/NServiceBus.Core.Tests/Config/When_starting_an_endpoint_with_already_used_configuration.cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public async Task Should_throw()
1111
{
1212
var configuration = new EndpointConfiguration("Endpoint1");
1313
configuration.UseTransport(new LearningTransport());
14+
configuration.UseSerialization<SystemJsonSerializer>();
1415
var scanner = configuration.AssemblyScanner();
1516
scanner.ExcludeAssemblies("NServiceBus.Core.Tests.dll");
1617

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace NServiceBus.Core.Tests.Config;
2+
3+
using System;
4+
using NUnit.Framework;
5+
6+
[TestFixture]
7+
class When_starting_endpoint_without_configuring_serializer
8+
{
9+
[Test]
10+
public void Should_throw()
11+
{
12+
var configuration = new EndpointConfiguration("Endpoint1");
13+
configuration.UseTransport(new LearningTransport());
14+
15+
var scanner = configuration.AssemblyScanner();
16+
scanner.ExcludeAssemblies("NServiceBus.Core.Tests.dll");
17+
18+
var exception = Assert.ThrowsAsync<Exception>(async () => await Endpoint.Create(configuration).ConfigureAwait(false));
19+
Assert.True(exception.Message.StartsWith("A serializer has not been configured"));
20+
}
21+
}

src/NServiceBus.Core.Tests/Serializers/MessageDeserializerResolverTests.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
using System;
44
using System.Collections.Generic;
55
using System.IO;
6-
using Serialization;
76
using NUnit.Framework;
7+
using Serialization;
88

99
[TestFixture]
1010
public class MessageDeserializerResolverTests
@@ -32,8 +32,8 @@ public void RetrievesSerializerByContentType(string contentType)
3232
[Test]
3333
public void UnknownContentTypeFallsBackToDefaultSerialization()
3434
{
35-
var defaultSerializer = new FakeSerializer(ContentTypes.Xml);
36-
var resolver = new MessageDeserializerResolver(defaultSerializer, new IMessageSerializer[]
35+
var mainSerializer = new FakeSerializer(ContentTypes.Xml);
36+
var resolver = new MessageDeserializerResolver(mainSerializer, new IMessageSerializer[]
3737
{
3838
new FakeSerializer(ContentTypes.Json)
3939
});
@@ -44,29 +44,29 @@ public void UnknownContentTypeFallsBackToDefaultSerialization()
4444
};
4545
var serializer = resolver.Resolve(headers);
4646

47-
Assert.AreSame(defaultSerializer, serializer);
47+
Assert.AreSame(mainSerializer, serializer);
4848
}
4949

5050
[Test]
5151
public void NoContentTypeFallsBackToDefaultSerialization()
5252
{
53-
var defaultSerializer = new FakeSerializer(ContentTypes.Xml);
54-
var resolver = new MessageDeserializerResolver(defaultSerializer, new IMessageSerializer[]
53+
var mainSerializer = new FakeSerializer(ContentTypes.Xml);
54+
var resolver = new MessageDeserializerResolver(mainSerializer, new IMessageSerializer[]
5555
{
5656
new FakeSerializer(ContentTypes.Json)
5757
});
5858

5959
var serializer = resolver.Resolve([]);
6060

61-
Assert.AreEqual(defaultSerializer, serializer);
61+
Assert.AreEqual(mainSerializer, serializer);
6262
}
6363

6464
[TestCase(null)]
6565
[TestCase("")]
6666
public void EmptyContentTypeFallsBackToDefaultSerialization(string headerValue)
6767
{
68-
var defaultSerializer = new FakeSerializer(ContentTypes.Xml);
69-
var resolver = new MessageDeserializerResolver(defaultSerializer, new IMessageSerializer[]
68+
var mainSerializer = new FakeSerializer(ContentTypes.Xml);
69+
var resolver = new MessageDeserializerResolver(mainSerializer, new IMessageSerializer[]
7070
{
7171
new FakeSerializer(ContentTypes.Json)
7272
});
@@ -76,7 +76,7 @@ public void EmptyContentTypeFallsBackToDefaultSerialization(string headerValue)
7676
{ Headers.ContentType, headerValue}
7777
});
7878

79-
Assert.AreEqual(defaultSerializer, serializer);
79+
Assert.AreEqual(mainSerializer, serializer);
8080
}
8181

8282
[Test]

src/NServiceBus.Core/Serialization/SerializationFeature.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ protected internal sealed override void Setup(FeatureConfigurationContext contex
2626
var messageMetadataRegistry = settings.Get<MessageMetadataRegistry>();
2727
mapper.Initialize(messageMetadataRegistry.GetAllMessages().Select(m => m.MessageType));
2828

29-
var defaultSerializerAndDefinition = settings.GetMainSerializer();
29+
var mainSerializerAndDefinition = settings.GetMainSerializer();
3030

31-
var defaultSerializer = CreateMessageSerializer(defaultSerializerAndDefinition, mapper, settings);
31+
var mainSerializer = CreateMessageSerializer(mainSerializerAndDefinition, mapper, settings);
3232

3333
var additionalDeserializerDefinitions = context.Settings.GetAdditionalSerializers();
3434
var additionalDeserializers = new List<IMessageSerializer>();
@@ -51,10 +51,10 @@ protected internal sealed override void Setup(FeatureConfigurationContext contex
5151

5252
var allowMessageTypeInference = settings.IsMessageTypeInferenceEnabled();
5353
var allowDynamicTypeLoading = settings.IsDynamicTypeLoadingEnabled();
54-
var resolver = new MessageDeserializerResolver(defaultSerializer, additionalDeserializers);
54+
var resolver = new MessageDeserializerResolver(mainSerializer, additionalDeserializers);
5555
var logicalMessageFactory = new LogicalMessageFactory(messageMetadataRegistry, mapper);
5656
context.Pipeline.Register("DeserializeLogicalMessagesConnector", new DeserializeMessageConnector(resolver, logicalMessageFactory, messageMetadataRegistry, mapper, allowMessageTypeInference), "Deserializes the physical message body into logical messages");
57-
context.Pipeline.Register("SerializeMessageConnector", new SerializeMessageConnector(defaultSerializer, messageMetadataRegistry), "Converts a logical message into a physical message");
57+
context.Pipeline.Register("SerializeMessageConnector", new SerializeMessageConnector(mainSerializer, messageMetadataRegistry), "Converts a logical message into a physical message");
5858

5959
context.Services.AddSingleton(_ => mapper);
6060
context.Services.AddSingleton<IMessageCreator>(sp => sp.GetRequiredService<IMessageMapper>());
@@ -65,11 +65,11 @@ protected internal sealed override void Setup(FeatureConfigurationContext contex
6565

6666
context.Settings.AddStartupDiagnosticsSection("Serialization", new
6767
{
68-
DefaultSerializer = new
68+
MainSerializer = new
6969
{
70-
Type = defaultSerializerAndDefinition.Item1.GetType().FullName,
71-
Version = FileVersionRetriever.GetFileVersion(defaultSerializerAndDefinition.Item1.GetType()),
72-
defaultSerializer.ContentType
70+
Type = mainSerializerAndDefinition.Item1.GetType().FullName,
71+
Version = FileVersionRetriever.GetFileVersion(mainSerializerAndDefinition.Item1.GetType()),
72+
mainSerializer.ContentType
7373
},
7474
AdditionalDeserializers = additionalDeserializerDiagnostics,
7575
AllowMessageTypeInference = allowMessageTypeInference,

src/NServiceBus.Core/Serialization/SerializationSettingsExtensions.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ public static void SetMainSerializer(this SettingsHolder settings, Serialization
3838

3939
public static Tuple<SerializationDefinition, SettingsHolder> GetMainSerializer(this IReadOnlySettings settings)
4040
{
41-
if (!settings.TryGet(MainSerializerSettingsKey, out Tuple<SerializationDefinition, SettingsHolder> defaultSerializerAndSettings))
41+
if (!settings.TryGet(MainSerializerSettingsKey, out Tuple<SerializationDefinition, SettingsHolder> mainSerializerAndSettings))
4242
{
43-
var noDefaultSerializerMsg = $"Because no message serializer was selected, the default {nameof(XmlSerializer)} will be used instead. In a future version of NServiceBus the {nameof(XmlSerializer)} will no longer be the default. For better forward compatibility, either choose a different message serializer, or make the choice of XML serialization explicit using endpointConfiguration.{nameof(SerializationConfigExtensions.UseSerialization)}<{nameof(XmlSerializer)}>()";
44-
log.Warn(noDefaultSerializerMsg);
45-
defaultSerializerAndSettings = Tuple.Create<SerializationDefinition, SettingsHolder>(new XmlSerializer(), new SettingsHolder());
43+
throw new Exception($"A serializer has not been configured. Use 'EndpointConfiguration.{nameof(SerializationConfigExtensions.UseSerialization)}()' to specify a serializer.");
4644
}
47-
return defaultSerializerAndSettings;
45+
46+
return mainSerializerAndSettings;
4847
}
4948
}

src/NServiceBus.Core/Serializers/MessageDeserializerResolver.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class MessageDeserializerResolver
88
{
9-
public MessageDeserializerResolver(IMessageSerializer defaultSerializer, IEnumerable<IMessageSerializer> additionalDeserializers)
9+
public MessageDeserializerResolver(IMessageSerializer mainSerializer, IEnumerable<IMessageSerializer> additionalDeserializers)
1010
{
11-
this.defaultSerializer = defaultSerializer;
11+
this.mainSerializer = mainSerializer;
1212

1313
foreach (var additionalDeserializer in additionalDeserializers)
1414
{
@@ -30,9 +30,10 @@ public IMessageSerializer Resolve(Dictionary<string, string> headers)
3030
return serializer;
3131
}
3232
}
33-
return defaultSerializer;
33+
34+
return mainSerializer;
3435
}
3536

36-
readonly IMessageSerializer defaultSerializer;
37+
readonly IMessageSerializer mainSerializer;
3738
readonly Dictionary<string, IMessageSerializer> serializersMap = [];
3839
}

0 commit comments

Comments
 (0)