Skip to content

Instantly share code, notes, and snippets.

@mowensoft
Created June 21, 2016 14:21
Show Gist options
  • Save mowensoft/22036edb1637c4f2eca0a655c5a8d3ce to your computer and use it in GitHub Desktop.
Save mowensoft/22036edb1637c4f2eca0a655c5a8d3ce to your computer and use it in GitHub Desktop.
namespace NServiceBusXmlSerializerBug
{
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using NServiceBus;
using NServiceBus.MessageMutator;
using NUnit.Framework;
// ReSharper disable InconsistentNaming
public class When_using_XmlSerializer_with_a_custom_namespace
{
[Test]
public void Should_use_custom_namespace_when_serializing_the_message()
{
var endpointConfiguration = new EndpointConfiguration("myqueue");
endpointConfiguration.UseSerialization<XmlSerializer>()
.Namespace("http://schemas.mycompany.com");
var transport = endpointConfiguration.UseTransport<MsmqTransport>();
transport.ConnectionString("useTransactionalQueues=false");
transport.Transactions(TransportTransactionMode.None);
endpointConfiguration.UsePersistence<InMemoryPersistence>();
endpointConfiguration.SendOnly();
endpointConfiguration.RegisterComponents(c =>
c.ConfigureComponent<WriteOutgoingMessages>(DependencyLifecycle.InstancePerCall));
var endpoint = Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();
var message = new CustomerBecamePreferred {CustomerId = "123"};
endpoint.SendLocal(message).Wait();
var xml = WriteOutgoingMessages.SerializedMessage;
var doc = XDocument.Parse(xml);
var @namespace = doc.Root.GetDefaultNamespace();
Assert.AreEqual("http://schemas.mycompany.com/NServiceBusXmlSerializerBug", @namespace);
}
}
public class CustomerBecamePreferred : IMessage
{
public string CustomerId { get; set; }
}
public class WriteOutgoingMessages : IMutateOutgoingTransportMessages
{
public static string SerializedMessage { get; set; }
public Task MutateOutgoing(MutateOutgoingTransportMessageContext context)
{
var bytes = context.OutgoingBody;
var serializedMessage = Encoding.UTF8.GetString(bytes);
SerializedMessage = serializedMessage;
return Task.FromResult(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment