1
0
mirror of synced 2024-12-22 09:13:07 +01:00

Fixed Tapeti trying to publish return types of type Task

This commit is contained in:
Mark van Renswoude 2017-02-13 15:47:57 +01:00
parent 28cd486b50
commit 52ad9a4fc6
3 changed files with 22 additions and 15 deletions

View File

@ -54,11 +54,11 @@ namespace Tapeti.Flow.Default
private static void RegisterYieldPointResult(IBindingContext context)
{
bool isTask;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(typeof(IYieldPoint), out isTask))
bool isTaskOf;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(typeof(IYieldPoint), out isTaskOf))
return;
if (isTask)
if (isTaskOf)
{
context.Result.SetHandler(async (messageContext, value) =>
{
@ -92,8 +92,8 @@ namespace Tapeti.Flow.Default
if (request?.Response == null)
return;
bool isTask;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t == request.Response || t == typeof(IYieldPoint), out isTask))
bool isTaskOf;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t == request.Response || t == typeof(IYieldPoint), out isTaskOf))
throw new ResponseExpectedException($"Response of class {request.Response.FullName} expected in controller {context.Method.DeclaringType?.FullName}, method {context.Method.Name}");
}
}

View File

@ -18,13 +18,13 @@ namespace Tapeti.Default
return;
bool isTask;
bool isTaskOf;
Type actualType;
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTask, out actualType))
if (!context.Result.Info.ParameterType.IsTypeOrTaskOf(t => t.IsClass, out isTaskOf, out actualType))
return;
if (isTask)
if (isTaskOf)
{
var handler = GetType().GetMethod("PublishGenericTaskResult", BindingFlags.NonPublic | BindingFlags.Static).MakeGenericMethod(actualType);

View File

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