Browse Source

refactor.

undefined
Savorboard 7 years ago
parent
commit
c619aff920
8 changed files with 49 additions and 16 deletions
  1. +7
    -3
      src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs
  2. +9
    -2
      src/DotNetCore.CAP/Abstractions/IContentSerializer.cs
  3. +10
    -0
      src/DotNetCore.CAP/CAP.Builder.cs
  4. +1
    -0
      src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
  5. +1
    -1
      src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs
  6. +1
    -1
      src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs
  7. +3
    -3
      src/DotNetCore.CAP/LoggerExtensions.cs
  8. +17
    -6
      src/DotNetCore.CAP/Models/CapMessageDto.cs

+ 7
- 3
src/DotNetCore.CAP/Abstractions/CapPublisherBase.cs View File

@@ -68,13 +68,17 @@ namespace DotNetCore.CAP.Abstractions

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
};
return serializer.Serialize(message);

return packer.Pack(message);
}

#region private methods


+ 9
- 2
src/DotNetCore.CAP/Abstractions/IContentSerializer.cs View File

@@ -4,8 +4,15 @@ namespace DotNetCore.CAP.Abstractions
{
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);
}
}

+ 10
- 0
src/DotNetCore.CAP/CAP.Builder.cs View File

@@ -60,6 +60,16 @@ namespace DotNetCore.CAP
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>
/// Adds a scoped service of the type specified in serviceType with an implementation
/// </summary>


+ 1
- 0
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs View File

@@ -34,6 +34,7 @@ namespace Microsoft.Extensions.DependencyInjection

//Serializer and model binder
services.TryAddSingleton<IContentSerializer, JsonContentSerializer>();
services.TryAddSingleton<IMessagePacker, DefaultMessagePacker>();
services.TryAddSingleton<IConsumerServiceSelector, DefaultConsumerServiceSelector>();
services.TryAddSingleton<IModelBinderFactory, ModelBinderFactory>();
services.TryAddSingleton<IConsumerInvokerFactory, ConsumerInvokerFactory>();


+ 1
- 1
src/DotNetCore.CAP/IQueueExecutor.Publish.Base.cs View File

@@ -70,7 +70,7 @@ namespace DotNetCore.CAP
catch (Exception ex)
{
fetched.Requeue();
_logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex);
_logger.ExceptionOccuredWhileExecuting(message?.Name, ex);
return OperateResult.Failed(ex);
}
}


+ 1
- 1
src/DotNetCore.CAP/IQueueExecutor.Subscribe.cs View File

@@ -68,7 +68,7 @@ namespace DotNetCore.CAP
}
catch (Exception ex)
{
_logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex);
_logger.ExceptionOccuredWhileExecuting(message?.Name, ex);

fetched.Requeue();



+ 3
- 3
src/DotNetCore.CAP/LoggerExtensions.cs View File

@@ -93,7 +93,7 @@ namespace DotNetCore.CAP
_exceptionOccuredWhileExecutingJob = LoggerMessage.Define<string>(
LogLevel.Error,
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.");
}

@@ -157,9 +157,9 @@ namespace DotNetCore.CAP
_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,


+ 17
- 6
src/DotNetCore.CAP/Models/CapMessageDto.cs View File

@@ -3,7 +3,18 @@ using DotNetCore.CAP.Infrastructure;

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()
{
@@ -11,17 +22,17 @@ namespace DotNetCore.CAP.Models
Timestamp = DateTime.Now;
}

public CapMessageDto(object content) : this()
public CapMessageDto(string content) : this()
{
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; }
}
}

Loading…
Cancel
Save