From 7cd55522bd41820e14aaa8d3e9c270fabc28b7a0 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Thu, 19 Oct 2017 18:10:22 +0800 Subject: [PATCH] add IMessagePacker --- .../Internal/IConsumerInvoker.Default.cs | 20 +++++++++------ .../Internal/IContentSerializer.Json.cs | 4 +-- .../Internal/IMessagePacker.Default.cs | 25 +++++++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs diff --git a/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs b/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs index 646ca33..6692959 100644 --- a/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs +++ b/src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs @@ -47,7 +47,7 @@ namespace DotNetCore.CAP.Internal object result; if (_executor.MethodParameters.Length > 0) - result = await ExecuteWithParameterAsync(obj, message.Content.ToString()); + result = await ExecuteWithParameterAsync(obj, message.Content); else result = await ExecuteAsync(obj); @@ -89,23 +89,27 @@ namespace DotNetCore.CAP.Internal private async Task SentCallbackMessage(string messageId, string topicName, object bodyObj) { - var callbackMessage = new CapMessageDto - { - Id = messageId, - Content = bodyObj - }; - using (var scope = _serviceProvider.CreateScope()) { var provider = scope.ServiceProvider; var publisher = provider.GetRequiredService(); + var serializer = provider.GetService(); + var packer = provider.GetService(); + + var callbackMessage = new CapMessageDto + { + Id = messageId, + Content = serializer.Serialize(bodyObj) + }; + var content = packer.Pack(callbackMessage); var publishedMessage = new CapPublishedMessage { Name = topicName, - Content = Helper.ToJson(callbackMessage), + Content = content, StatusName = StatusName.Scheduled }; + await publisher.PublishAsync(publishedMessage); } } diff --git a/src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs b/src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs index 2726cd6..c4fe3ee 100644 --- a/src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs +++ b/src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs @@ -6,12 +6,12 @@ namespace DotNetCore.CAP.Internal { public class JsonContentSerializer : IContentSerializer { - public T DeSerialize(string messageObjStr) where T : CapMessageDto, new() + public T DeSerialize(string messageObjStr) { return Helper.FromJson(messageObjStr); } - public string Serialize(T messageObj) where T : CapMessageDto, new() + public string Serialize(T messageObj) { return Helper.ToJson(messageObj); } diff --git a/src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs b/src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs new file mode 100644 index 0000000..1f7e976 --- /dev/null +++ b/src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs @@ -0,0 +1,25 @@ +using DotNetCore.CAP.Abstractions; +using DotNetCore.CAP.Models; + +namespace DotNetCore.CAP.Internal +{ + public class DefaultMessagePacker : IMessagePacker + { + private readonly IContentSerializer _serializer; + + public DefaultMessagePacker(IContentSerializer serializer) + { + _serializer = serializer; + } + + public string Pack(CapMessage obj) + { + return _serializer.Serialize(obj); + } + + public CapMessage UnPack(string packingMessage) + { + return _serializer.DeSerialize(packingMessage); + } + } +}