From 1cd3f406daea7dd86d470afca9d43179e80c8388 Mon Sep 17 00:00:00 2001 From: Savorboard Date: Mon, 10 Jun 2024 22:25:57 +0800 Subject: [PATCH] Add option for controlling reponse from CapHeader. #1541 --- .../Internal/ISubscribeInvoker.Default.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs b/src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs index e7d91dfc2..3e7c01bc5 100644 --- a/src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs +++ b/src/DotNetCore.CAP/Internal/ISubscribeInvoker.Default.cs @@ -50,21 +50,19 @@ public async Task InvokeAsync(ConsumerContext context, var message = context.DeliverMessage; var parameterDescriptors = context.ConsumerDescriptor.Parameters; var executeParameters = new object?[parameterDescriptors.Count]; - var headerIndex = 0; for (var i = 0; i < parameterDescriptors.Count; i++) { var parameterDescriptor = parameterDescriptors[i]; if (parameterDescriptor.IsFromCap) { executeParameters[i] = GetCapProvidedParameter(parameterDescriptor, message, cancellationToken); - headerIndex = i; } else { if (message.Value != null) { // use ISerializer when reading from storage, skip other objects if not Json - if (_serializer.IsJsonType(message.Value)) + if (_serializer.IsJsonType(message.Value)) { executeParameters[i] = _serializer.Deserialize(message.Value, parameterDescriptor.ParameterType); @@ -125,7 +123,16 @@ public async Task InvokeAsync(ConsumerContext context, } } - return new ConsumerExecutedResult(resultObj, message.GetId(), message.GetCallbackName(), (executeParameters[headerIndex] as CapHeader)?.ResponseHeader); + var callbackName = message.GetCallbackName(); + if (string.IsNullOrEmpty(callbackName)) + { + return new ConsumerExecutedResult(resultObj, message.GetId(), null, null); + } + else + { + var capHeader = executeParameters.FirstOrDefault(x => x is CapHeader) as CapHeader; + return new ConsumerExecutedResult(resultObj, message.GetId(), callbackName, capHeader?.ResponseHeader); + } } private static object GetCapProvidedParameter(ParameterDescriptor parameterDescriptor, Message message,