C# 如何返回ActionResult以及async foreach和IAsyncEnumerable

C# 如何返回ActionResult以及async foreach和IAsyncEnumerable,c#,asp.net-core-webapi,actionresult,request-validation,iasyncenumerable,C#,Asp.net Core Webapi,Actionresult,Request Validation,Iasyncenumerable,我有此签名的控制器方法: public async IAsyncEnumerable<MyDto> Get() public异步IAsyncEnumerable Get() 它工作正常,但我需要进行一些请求验证,并相应地返回401400和其他代码,而它不支持这些代码。 或者,不编译以下签名: public async Task<ActionResult<IAsyncEnumerable<MyDto>>> Get() 公共异步任务Get()

我有此签名的控制器方法:

public async IAsyncEnumerable<MyDto> Get()
public异步IAsyncEnumerable Get()
它工作正常,但我需要进行一些请求验证,并相应地返回401400和其他代码,而它不支持这些代码。 或者,不编译以下签名:

public async Task<ActionResult<IAsyncEnumerable<MyDto>>> Get()
公共异步任务Get() 错误:

无法隐式转换类型 “Microsoft.AspNetCore.Mvc.UnauthorizedResult”到 'MyApi.Responses.MyDto'

完整方法:

public async IAsyncEnumerable<MyDto> Get()
{
    if (IsRequestInvalid())
    {
        // Can't do the following. Does not compile.
        yield return Unauthorized();
    }
    var retrievedDtos = _someService.GetAllDtosAsync(_userId);

    await foreach (var currentDto in retrievedDtos)
    {
        yield return currentDto;
    }
}
public异步IAsyncEnumerable Get()
{
if(IsRequestInvalid())
{
//无法执行以下操作。未编译。
未经授权的收益率();
}
var retrievedDtos=\u someService.GetAllDtosAsync(\u userId);
等待foreach(检索到的DTO中的var currentDto)
{
产生返回电流dto;
}
}
有什么想法吗?似乎无法相信微软设计的iSyncEnumerable能够在没有返回任何其他内容的可能性/灵活性的情况下使用。

这应该是可行的

    public ActionResult<IAsyncEnumerable<MyDto>> Get()
    {
        if(IsRequestInvalid())
        {
            // now can do.
            return Unauthorized();
        }

        return new ActionResult<IAsyncEnumerable<MyDto>>(DoSomeProcessing());

        IAsyncEnumerable<MyDto> DoSomeProcessing()
        {
            IAsyncEnumerable<MyDto> retrievedDtos = _someService.GetAllDtosAsync(_userId);

            await foreach(var currentDto in retrievedDtos)
            {
                //work with currentDto here

                yield return currentDto;
            }
        }
    }
public ActionResult Get()
{
if(IsRequestInvalid())
{
//现在我可以做了。
未经授权返回();
}
返回新的ActionResult(DoSomeProcessing());
IAsynceneumerable DoSomeProcessing()
{
IAsyncEnumerable RetrievedTos=\u someService.GetAllDtosAsync(\u userId);
等待foreach(检索到的DTO中的var currentDto)
{
//在此处使用currentDto
产生返回电流dto;
}
}
}
如果在退回物品之前未对其进行处理:

public ActionResult<IAsyncEnumerable<MyDto>> Get()
    {
        if(IsRequestInvalid())
        {
            // now can do
            return Unauthorized();
        }

        return new ActionResult<IAsyncEnumerable<MyDto>>(_someService.GetAllDtosAsync(_userId));
    }
public ActionResult Get()
{
if(IsRequestInvalid())
{
//现在我能做什么
未经授权返回();
}
返回新的ActionResult(_someService.GetAllDtosAsync(_userId));
}

这与
IAsyncEnumerable
关系不大。如果使用
异步任务
,您也会遇到同样的问题。如果要返回特定的响应,请返回
IActionResult
ActionResult
。下面解释:
在这种情况下,通常会将ActionResult返回类型与基本返回类型或复杂返回类型混合使用。IActionResult或ActionResult都是适应此类操作所必需的。
@PanagiotisKanavos这不是同一个问题,因为在任务的情况下,我可以轻松地将其设置为
任务
,而我无法执行
任务
(如问题中所述)。我需要IAsyncEnumerable在结果到达时将结果传递给序列化程序。这完全是同一个问题-除非返回
ActionResult
IActionResult
,否则无法返回状态。问题是如何回报这一点,并让iSyncene的好处不可估量。查看实际发送对象结果的类,我看到它有代码,您可以尝试返回
ActionResult
,例如:
returnok(retrievedtos)