任务与结果未知类型 [英] Task with result unknown type

查看:149
本文介绍了任务与结果未知类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个异步API CQS

该方法的WebAPI看起来像这样

 公共异步任务<对象>获取([FromUri]合同契约)
{
    返回等待_invoker.Invoke(CreateDto<查询>(合同));
}


  

无法隐式转换类型
  '<$c$c>System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Customer>>'
  以 System.Threading.Tasks.Task&LT;对象&gt;


我怎样才能返回,而不知道从类型结果的任务吗?

这是code调用通用类型类

 公共任务&LT;对象&gt;调用(查询的查询)
{
    VAR queryHandlerType = typeof运算(IQueryHandler&LT;,&GT;);
    变种查询类型= query.GetType();
    VAR queryResultType = queryType.BaseType.GetGenericArguments()(第)。
    VAR处理器= _container.GetInstance(queryHandlerType.MakeGenericType(查询类型,queryResultType))的动态;
    返回handler.Handle(查询动态);
}

编辑:是否有任何理由的WebAPI不支持任务&LT; T&GT; 暴露成任务?如果我从任务改变只是简单的任务这是行不通的。它仍然是一个任务下面这样的WebAPI应该能够解决一些魔法?我想这待办事项

 公共任务获得([FromUri]合同契约)
{
    返回_invoker.Invoke(CreateDto&LT;查询&GT;(合同));
}


解决方案

现在的问题是有点混乱。它特别不清楚的地方错误消息的来源。基于小code你贴出来,我看是明智的唯一的事情是,你实际上得到该错误消息在的收益语句的的invoke()方法。即该 handler.Handle(查询动态)返回类型的对象任务&LT; System.Collections.Generic.IEnumerable&LT;客户&GT;&GT; 。这当然不会是一样的任务&LT;对象&gt; 等将是非法的。

如果我理解正确的话,那么在我看来,你可以通过改变你的的invoke()办法解决这个问题本身是异步,这样就可以重新包装的最终退回 System.Collections.Generic.IEnumerable&LT;客户&GT; 对象的结果任务&LT;的不是任务&LT;对象&gt System.Collections.Generic.IEnumerable&LT;客户&GT;&GT; 当前正在生成:

 公共异步任务&LT;对象&gt;调用(查询的查询)
{
    VAR queryHandlerType = typeof运算(IQueryHandler&LT;,&GT;);
    变种查询类型= query.GetType();
    VAR queryResultType = queryType.BaseType.GetGenericArguments()(第)。
    VAR处理器= _container.GetInstance(queryHandlerType.MakeGenericType(查询类型,queryResultType))的动态;
    返回等待handler.Handle(查询动态);
}



如果不解决您的具体问题,请使其更清晰编辑的问题。请提供一个良好的,的最小完整的code例如的那可靠重现问题。

I'm working on a async CQS API

The WebAPi method looks like this

public async Task<object> Get([FromUri] Contract contract)
{
    return  await _invoker.Invoke(CreateDto<Query>(contract));
}

Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Customer>>' to 'System.Threading.Tasks.Task<object>

How can I return a task from result without knowing the type?

This is the code that invokes the Generic typed class

public Task<object> Invoke(Query query)
{
    var queryHandlerType = typeof(IQueryHandler<,>);
    var queryType = query.GetType();
    var queryResultType = queryType.BaseType.GetGenericArguments().First();
    var handler = _container.GetInstance(queryHandlerType.MakeGenericType(queryType, queryResultType)) as dynamic;
    return handler.Handle(query as dynamic);
}

edit: Is there any reason why WebApi does not support Task<T> exposed as Task? If I change from Task to just plain Task it does not work. Its still a Task underneath so WebApi should be able to resolve that with some magic? I want todo this

public Task Get([FromUri] Contract contract)
{
    return _invoker.Invoke(CreateDto<Query>(contract));
}

解决方案

The question is a little confusing. It's particularly not clear where the error message is coming from. Based on the little code you posted, the only thing I see that would make sense is that you're actually getting that error message at the return statement of your Invoke() method. I.e. that handler.Handle(query as dynamic) returns an object of type Task<System.Collections.Generic.IEnumerable<Customer>>. This, of course, would not be the same as Task<object> and so would be illegal.

If I understand correctly, then it seems to me you could address the issue by changing your Invoke() method itself to be async, so that you can rewrap the eventually-returned System.Collections.Generic.IEnumerable<Customer> object in the Result of a Task<object> instead of the Task<System.Collections.Generic.IEnumerable<Customer>> that is currently being generated:

public async Task<object> Invoke(Query query)
{
    var queryHandlerType = typeof(IQueryHandler<,>);
    var queryType = query.GetType();
    var queryResultType = queryType.BaseType.GetGenericArguments().First();
    var handler = _container.GetInstance(queryHandlerType.MakeGenericType(queryType, queryResultType)) as dynamic;
    return await handler.Handle(query as dynamic);
}


If that does not address your specific concern, please edit the question so that it is more clear. Please provide a good, minimal, complete code example that reliably reproduces the problem.

这篇关于任务与结果未知类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆