小编Bel*_*way的帖子

我可以(或应该)在 Web API 控制器中使用 IAsyncEnumerable<T> 而不是 Task<ActionResult<IEnumerable<T>>>

我目前有一个 Web API

  • FromSqlRaw(...).ToListAsync()在存储库中使用获取一行数据
  • Ok(data.ToArray())Task<ActionResult<IEnumerable<MyClass>>>通过控制器一样返回此数据。

现在我想知道我是否应该或可以使用 IAsyncEnumerable 作为返回类型。这个想法是在存储库和控制器中使用它。然而,在这个(现在已经破旧的)线程中,它声明不应该使用它。这里建议的解决方案类似于:

FromSqlRaw(...).AsNoTracking().AsAsyncEnumerable()
Run Code Online (Sandbox Code Playgroud)

至于控制器,我希望将响应包装起来ActionResult以显式设置返回码。然而,目前似乎并没有工作。

我应该只为存储库应用解决方案并将结果作为列表在我的控制器中使用还是保持原样?

entity-framework-core asp.net-core-webapi iasyncenumerable

4
推荐指数
2
解决办法
299
查看次数

在 AuthorizationHandler 中使用 `await`

我有一个 AuthorizationHandler ,它依赖于为 .NET Core 3.1 的授权中间件提供异步方法的服务。我在方法内部调用了其中一些异步方法HandleRequirementAsync。整体代码如下所示:

{
    public class MyAuthorizationHandler : AuthorizationHandler<MyRequirement, Tuple<string, string>>
    {
        private readonly IAuthIntelRepository authIntelRepository;
        public UserAssistanceAuthorizationHandler(IAuthIntelRepository authIntelRepository)
        {
            this.authIntelRepository = authIntelRepository;
        }
        protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, MyRequirement requirement, Tuple<string, string> someRessource)
        {
            //some async calls to authIntelRepository
            if (/*someCondition*/false)
            {
                context.Succeed(requirement);
            }
            return Task.CompletedTask;
        }
    }

    public class MyRequirement : IAuthorizationRequirement { }
}
Run Code Online (Sandbox Code Playgroud)

不过,一旦我使用await语句,我就会收到一个错误,表明签名未明确设置为异步。添加async到继承方法的签名会导致以下错误。 a return keyword must not be followed by an object expression. Did …

c# asp.net-authorization async-await .net-core

4
推荐指数
1
解决办法
1378
查看次数