您如何在不使用.Result的情况下无法在C#中异步的方法中调用异步方法 [英] How would you call async method in a method which cannot be async in C# without using .Result

查看:71
本文介绍了您如何在不使用.Result的情况下无法在C#中异步的方法中调用异步方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重写一个方法HandleRequirementAsync,所以我不能修改该方法的返回类型.但是,我在UserManager中调用了一个异步方法,需要等待,因此我不得不将此代码放入另一个方法中.

I am overriding a method, HandleRequirementAsync, so I can't modify the return type of that method. However, I am calling a method in UserManager which is async and requires an await so I had to put this code in another method.

        protected override Task HandleRequirementAsync(
          AuthorizationHandlerContext context,
          EducationArmRequirement requirement)
        {

            if (!IsEducationGroup(context).Result)
            {
                return Task.FromResult(0);
            }


            context.Succeed(requirement);

            return Task.FromResult(0);
        }

        protected async Task<bool> IsEducationGroup(AuthorizationHandlerContext context)
        {
            var userId = context.User.Identity.Name;
            ApplicationUser u = await _userManager.FindByNameAsync(userId);
            if (u.Group == 3 || u.Group == 4)
            {
                return await Task.FromResult(true);
            }
            return await Task.FromResult(false);
        }

上面的代码似乎可以在测试中工作,但是它使用.Result解包Task以获取布尔值.我已经看过几个地方说要谨慎使用结果.我看到了这个问题,它试图解释为什么,但我不明白.

The code above seems to work in testing but it uses .Result to unwrap the Task to get the bool. I have seen several places that say be careful of using result. I saw this question which tries to explain why but I am not getting it.

我无法在HandleRequirementAsync方法中等待以使其正常解析,因为那样的话,我将不得不修改对于此重写方法无法执行的返回类型.

I can't put an await in the HandleRequirementAsync method to allow this to unrap normally because then I would have to modify the return type which I can't do for this overridden method.

我是使用.Result的正确方法吗?还是在这里遇到一些问题?如果可以,还有什么其他方法可以解决?

Am I approaching this the correct way in using the .Result or am I going to come across some issues here and if so what is another way to approach this which would work?

任何帮助/解释将不胜感激.

Any help/explanations would be much appreciated.

推荐答案

正如注释中所暗示的,async修饰符不会更改方法签名-您可以添加它.您还可以简化两种方法中删除 Task.FromResult 的用法:

As hinted in the comments, the async modifier doesn't change the method signature - you can just add this. You can also simplify to remove usage of Task.FromResult in both methods:

protected override async Task HandleRequirementAsync(
    AuthorizationHandlerContext context,
    EducationArmRequirement requirement)
{
    if (await IsEducationGroup(context))
    {
        context.Succeed(requirement);
    }
}

protected async Task<bool> IsEducationGroup(AuthorizationHandlerContext context)
{
    var userId = context.User.Identity.Name;

    ApplicationUser u = await _userManager.FindByNameAsync(userId);

    return u.Group == 3 || u.Group == 4;
}

这篇关于您如何在不使用.Result的情况下无法在C#中异步的方法中调用异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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