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); + } + } +}