Browse Source

add IMessagePacker

undefined
Savorboard 7 years ago
parent
commit
7cd55522bd
3 changed files with 39 additions and 10 deletions
  1. +12
    -8
      src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs
  2. +2
    -2
      src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs
  3. +25
    -0
      src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs

+ 12
- 8
src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs View File

@@ -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<ICallbackPublisher>();
var serializer = provider.GetService<IContentSerializer>();
var packer = provider.GetService<IMessagePacker>();

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


+ 2
- 2
src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs View File

@@ -6,12 +6,12 @@ namespace DotNetCore.CAP.Internal
{
public class JsonContentSerializer : IContentSerializer
{
public T DeSerialize<T>(string messageObjStr) where T : CapMessageDto, new()
public T DeSerialize<T>(string messageObjStr)
{
return Helper.FromJson<T>(messageObjStr);
}

public string Serialize<T>(T messageObj) where T : CapMessageDto, new()
public string Serialize<T>(T messageObj)
{
return Helper.ToJson(messageObj);
}


+ 25
- 0
src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs View File

@@ -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<CapMessageDto>(packingMessage);
}
}
}

Loading…
Cancel
Save