@@ -68,13 +68,17 @@ namespace DotNetCore.CAP.Abstractions | |||||
protected virtual string Serialize<T>(T obj, string callbackName = null) | protected virtual string Serialize<T>(T obj, string callbackName = null) | ||||
{ | { | ||||
var serializer = (IContentSerializer) ServiceProvider.GetService(typeof(IContentSerializer)); | |||||
var serializer = (IContentSerializer)ServiceProvider.GetService(typeof(IContentSerializer)); | |||||
var packer = (IMessagePacker)ServiceProvider.GetService(typeof(IMessagePacker)); | |||||
var message = new CapMessageDto(obj) | |||||
var content = serializer.Serialize(obj); | |||||
var message = new CapMessageDto(content) | |||||
{ | { | ||||
CallbackName = callbackName | CallbackName = callbackName | ||||
}; | }; | ||||
return serializer.Serialize(message); | |||||
return packer.Pack(message); | |||||
} | } | ||||
#region private methods | #region private methods | ||||
@@ -4,8 +4,15 @@ namespace DotNetCore.CAP.Abstractions | |||||
{ | { | ||||
public interface IContentSerializer | public interface IContentSerializer | ||||
{ | { | ||||
string Serialize<T>(T obj) where T : CapMessageDto, new(); | |||||
string Serialize<T>(T obj); | |||||
T DeSerialize<T>(string content) where T : CapMessageDto, new(); | |||||
T DeSerialize<T>(string content); | |||||
} | |||||
public interface IMessagePacker | |||||
{ | |||||
string Pack(CapMessage obj); | |||||
CapMessage UnPack(string packingMessage); | |||||
} | } | ||||
} | } |
@@ -60,6 +60,16 @@ namespace DotNetCore.CAP | |||||
return AddSingleton(typeof(IContentSerializer), typeof(T)); | return AddSingleton(typeof(IContentSerializer), typeof(T)); | ||||
} | } | ||||
/// <summary> | |||||
/// Add a custom message wapper | |||||
/// </summary> | |||||
/// <typeparam name="T">The type of the service.</typeparam> | |||||
public CapBuilder AddMessagePacker<T>() | |||||
where T : class, IMessagePacker | |||||
{ | |||||
return AddSingleton(typeof(IMessagePacker), typeof(T)); | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// Adds a scoped service of the type specified in serviceType with an implementation | /// Adds a scoped service of the type specified in serviceType with an implementation | ||||
/// </summary> | /// </summary> | ||||
@@ -34,6 +34,7 @@ namespace Microsoft.Extensions.DependencyInjection | |||||
//Serializer and model binder | //Serializer and model binder | ||||
services.TryAddSingleton<IContentSerializer, JsonContentSerializer>(); | services.TryAddSingleton<IContentSerializer, JsonContentSerializer>(); | ||||
services.TryAddSingleton<IMessagePacker, DefaultMessagePacker>(); | |||||
services.TryAddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>(); | services.TryAddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>(); | ||||
services.TryAddSingleton<IModelBinderFactory, ModelBinderFactory>(); | services.TryAddSingleton<IModelBinderFactory, ModelBinderFactory>(); | ||||
services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>(); | services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>(); | ||||
@@ -70,7 +70,7 @@ namespace DotNetCore.CAP | |||||
catch (Exception ex) | catch (Exception ex) | ||||
{ | { | ||||
fetched.Requeue(); | fetched.Requeue(); | ||||
_logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex); | |||||
_logger.ExceptionOccuredWhileExecuting(message?.Name, ex); | |||||
return OperateResult.Failed(ex); | return OperateResult.Failed(ex); | ||||
} | } | ||||
} | } | ||||
@@ -68,7 +68,7 @@ namespace DotNetCore.CAP | |||||
} | } | ||||
catch (Exception ex) | catch (Exception ex) | ||||
{ | { | ||||
_logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex); | |||||
_logger.ExceptionOccuredWhileExecuting(message?.Name, ex); | |||||
fetched.Requeue(); | fetched.Requeue(); | ||||
@@ -93,7 +93,7 @@ namespace DotNetCore.CAP | |||||
_exceptionOccuredWhileExecutingJob = LoggerMessage.Define<string>( | _exceptionOccuredWhileExecutingJob = LoggerMessage.Define<string>( | ||||
LogLevel.Error, | LogLevel.Error, | ||||
6, | 6, | ||||
"An exception occured while trying to execute a job: '{JobId}'. " + | |||||
"An exception occured while trying to execute a message: '{MessageId}'. " + | |||||
"Requeuing for another retry."); | "Requeuing for another retry."); | ||||
} | } | ||||
@@ -157,9 +157,9 @@ namespace DotNetCore.CAP | |||||
_expectedOperationCanceledException(logger, ex.Message, ex); | _expectedOperationCanceledException(logger, ex.Message, ex); | ||||
} | } | ||||
public static void ExceptionOccuredWhileExecutingJob(this ILogger logger, string jobId, Exception ex) | |||||
public static void ExceptionOccuredWhileExecuting(this ILogger logger, string messageId, Exception ex) | |||||
{ | { | ||||
_exceptionOccuredWhileExecutingJob(logger, jobId, ex); | |||||
_exceptionOccuredWhileExecutingJob(logger, messageId, ex); | |||||
} | } | ||||
public static void ModelBinderFormattingException(this ILogger logger, string methodName, string parameterName, | public static void ModelBinderFormattingException(this ILogger logger, string methodName, string parameterName, | ||||
@@ -3,7 +3,18 @@ using DotNetCore.CAP.Infrastructure; | |||||
namespace DotNetCore.CAP.Models | namespace DotNetCore.CAP.Models | ||||
{ | { | ||||
public class CapMessageDto | |||||
public abstract class CapMessage | |||||
{ | |||||
public virtual string Id { get; set; } | |||||
public virtual DateTime Timestamp { get; set; } | |||||
public virtual string Content { get; set; } | |||||
public virtual string CallbackName { get; set; } | |||||
} | |||||
public sealed class CapMessageDto: CapMessage | |||||
{ | { | ||||
public CapMessageDto() | public CapMessageDto() | ||||
{ | { | ||||
@@ -11,17 +22,17 @@ namespace DotNetCore.CAP.Models | |||||
Timestamp = DateTime.Now; | Timestamp = DateTime.Now; | ||||
} | } | ||||
public CapMessageDto(object content) : this() | |||||
public CapMessageDto(string content) : this() | |||||
{ | { | ||||
Content = content; | Content = content; | ||||
} | } | ||||
public virtual string Id { get; set; } | |||||
public override string Id { get; set; } | |||||
public virtual DateTime Timestamp { get; set; } | |||||
public override DateTime Timestamp { get; set; } | |||||
public virtual object Content { get; set; } | |||||
public override string Content { get; set; } | |||||
public virtual string CallbackName { get; set; } | |||||
public override string CallbackName { get; set; } | |||||
} | } | ||||
} | } |