Fixed InvalidCastException for Task<T> methods
This commit is contained in:
parent
ddc9d77a4c
commit
b319be8d76
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user