Ver a proveniência

add IMessagePacker

undefined
Savorboard há 7 anos
ascendente
cometimento
7cd55522bd
3 ficheiros alterados com 39 adições e 10 eliminações
  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 Ver ficheiro

@@ -47,7 +47,7 @@ namespace DotNetCore.CAP.Internal


object result; object result;
if (_executor.MethodParameters.Length > 0) if (_executor.MethodParameters.Length > 0)
result = await ExecuteWithParameterAsync(obj, message.Content.ToString());
result = await ExecuteWithParameterAsync(obj, message.Content);
else else
result = await ExecuteAsync(obj); result = await ExecuteAsync(obj);


@@ -89,23 +89,27 @@ namespace DotNetCore.CAP.Internal


private async Task SentCallbackMessage(string messageId, string topicName, object bodyObj) private async Task SentCallbackMessage(string messageId, string topicName, object bodyObj)
{ {
var callbackMessage = new CapMessageDto
{
Id = messageId,
Content = bodyObj
};

using (var scope = _serviceProvider.CreateScope()) using (var scope = _serviceProvider.CreateScope())
{ {
var provider = scope.ServiceProvider; var provider = scope.ServiceProvider;
var publisher = provider.GetRequiredService<ICallbackPublisher>(); 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 var publishedMessage = new CapPublishedMessage
{ {
Name = topicName, Name = topicName,
Content = Helper.ToJson(callbackMessage),
Content = content,
StatusName = StatusName.Scheduled StatusName = StatusName.Scheduled
}; };

await publisher.PublishAsync(publishedMessage); await publisher.PublishAsync(publishedMessage);
} }
} }


+ 2
- 2
src/DotNetCore.CAP/Internal/IContentSerializer.Json.cs Ver ficheiro

@@ -6,12 +6,12 @@ namespace DotNetCore.CAP.Internal
{ {
public class JsonContentSerializer : IContentSerializer 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); 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); return Helper.ToJson(messageObj);
} }


+ 25
- 0
src/DotNetCore.CAP/Internal/IMessagePacker.Default.cs Ver ficheiro

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

Carregando…
Cancelar
Guardar