1
0
mirror of synced 2024-11-05 02:59:16 +00:00

Fixed InvalidCastException for Task<T> methods

This commit is contained in:
Mark van Renswoude 2017-02-10 12:07:34 +01:00
parent ddc9d77a4c
commit b319be8d76
2 changed files with 39 additions and 15 deletions

View File

@ -1,4 +1,6 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Threading.Tasks;
using RabbitMQ.Client.Framing;
using Tapeti.Config;
@ -15,26 +17,29 @@ namespace Tapeti.Default
if (context.Result.HasHandler)
return;
bool isTask;
if (context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTask))
Type actualType;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTask, out actualType))
return;
if (isTask)
{
if (isTask)
var handler = GetType().GetMethod("PublishGenericTaskResult", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(actualType);
context.Result.SetHandler(async (messageContext, value) =>
{
context.Result.SetHandler(async (messageContext, value) =>
{
var message = await (Task<object>)value;
if (message != null)
await Reply(message, messageContext);
});
}
else
context.Result.SetHandler((messageContext, value) =>
value == null ? null : Reply(value, messageContext));
await (Task)handler.Invoke(null, new[] { messageContext, value });
});
}
else
context.Result.SetHandler((messageContext, value) =>
value == null ? null : Reply(value, messageContext));
}
private Task Reply(object message, IMessageContext messageContext)
private static Task Reply(object message, IMessageContext messageContext)
{
var publisher = (IInternalPublisher)messageContext.DependencyResolver.Resolve<IPublisher>();
var properties = new BasicProperties();
@ -49,5 +54,13 @@ namespace Tapeti.Default
return publisher.Publish(message, properties);
}
private static async Task PublishGenericTaskResult<T>(IMessageContext messageContext, object value) where T : class
{
var message = await (Task<T>)value;
if (message != null)
await Reply(message, messageContext);
}
}
}

View File

@ -5,21 +5,32 @@ namespace Tapeti.Helpers
{
public static class TaskTypeHelper
{
public static bool IsTypeOrTaskOf(this Type type, Func<Type, bool> predicate, out bool isTask)
public static bool IsTypeOrTaskOf(this Type type, Func<Type, bool> predicate, out bool isTask, out Type actualType)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>))
{
isTask = true;
var genericArguments = type.GetGenericArguments();
return genericArguments.Length == 1 && predicate(genericArguments[0]);
if (genericArguments.Length == 1 && predicate(genericArguments[0]))
{
actualType = genericArguments[0];
return true;
}
}
isTask = false;
actualType = type;
return predicate(type);
}
public static bool IsTypeOrTaskOf(this Type type, Func<Type, bool> predicate, out bool isTask)
{
Type actualType;
return IsTypeOrTaskOf(type, predicate, out isTask, out actualType);
}
public static bool IsTypeOrTaskOf(this Type type, Type compareTo, out bool isTask)
{
return IsTypeOrTaskOf(type, t => t == compareTo, out isTask);