Skip to content

Instantly share code, notes, and snippets.

@andreasohlund
Created October 16, 2018 11:09
Show Gist options
  • Save andreasohlund/8afe726cff38ae267511232e454ed090 to your computer and use it in GitHub Desktop.
Save andreasohlund/8afe726cff38ae267511232e454ed090 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NServiceBus;
using NServiceBus.MessageInterfaces;
using NServiceBus.Serialization;
using NServiceBus.Settings;
using JsonSerializer = Newtonsoft.Json.JsonSerializer;
internal static class Program
{
static async Task Main()
{
Console.Title = "Samples.Serialization.ExternalJson";
var endpointConfiguration = new EndpointConfiguration("Samples.Serialization.ExternalJson");
var serialization = endpointConfiguration.UseSerialization<JObjectSerializer>();
endpointConfiguration.UseTransport<LearningTransport>();
var endpointInstance = await Endpoint.Start(endpointConfiguration)
.ConfigureAwait(false);
#region message
var message = new CreateOrder
{
OrderId = 9,
Date = DateTime.Now,
CustomerId = 12,
OrderItems = new List<OrderItem>
{
new OrderItem
{
ItemId = 6,
Quantity = 2
},
new OrderItem
{
ItemId = 5,
Quantity = 4
}
}
};
await endpointInstance.SendLocal(message)
.ConfigureAwait(false);
#endregion
Console.WriteLine("Order Sent");
Console.WriteLine("Press any key to exit");
Console.ReadKey();
await endpointInstance.Stop()
.ConfigureAwait(false);
}
}
internal class JObjectSerializer : SerializationDefinition
{
public override Func<IMessageMapper, IMessageSerializer> Configure(ReadOnlySettings settings)
{
return _ => new DeserializerToJObjectSerializer();
}
}
internal class DeserializerToJObjectSerializer : IMessageSerializer
{
readonly JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
public void Serialize(object message, Stream stream)
{
var streamWriter = new StreamWriter(stream, Encoding.UTF8);
using (var writer = new JsonTextWriter(streamWriter)
{
Formatting = Formatting.None
})
{
writer.CloseOutput = false;
serializer.Serialize(writer, message);
writer.Flush();
}
}
public object[] Deserialize(Stream stream, IList<Type> messageTypes = null)
{
using (var streamReader = new StreamReader(stream, Encoding.UTF8))
using (var jsonReader = new JsonTextReader(streamReader))
{
var jObject = JObject.Load(jsonReader);
return new object[]
{
new JSONCatchAll
{
MessageAsJObject = jObject
}
};
}
}
public string ContentType { get; } = ContentTypes.Json;
}
internal class JSONCatchAllHandler : IHandleMessages<JSONCatchAll>
{
public Task Handle(JSONCatchAll message, IMessageHandlerContext context)
{
Console.WriteLine("Got the catch all, content:");
Console.WriteLine(message.MessageAsJObject.ToString());
return Task.CompletedTask;
}
}
class JSONCatchAll : IMessage
{
public JObject MessageAsJObject { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment