错误异步调用异步方法

毕尔波

我有以下异步方法:

[HttpGet]
[Route("api/SecurityRoles/AllowDeleteRole/{securityRoleId}")]
public async Task<IHttpActionResult> AllowDeleteRole(int securityRoleId)
{
    _systemLogger.LogInfo($"Method api/SecurityRoles/AllowDeleteRole (Get) called with securityRoleId: {securityRoleId}");

    var securityRolePermission =
            await _securityRolePermissionService.SecurityRolePermissionsCountBySecurityRoleId(securityRoleId);
    if (securityRolePermission != SecurityRoleDeleteResult.Success) return Ok(SecurityRoleDeleteResult.RolePermissionExists);

    var securityRolePageEntity =
            await  _securityRolePageEntityService.SecurityRolePageEntityCountBySecurityRoleId(securityRoleId);
    return Ok(securityRolePageEntity != SecurityRoleDeleteResult.Success ? SecurityRoleDeleteResult.RolePageEntityExists : SecurityRoleDeleteResult.Success);
    }

它在很多地方都得到了使用,但是对于这个实例,我需要以非异步方式使用它,因此我首先包装了以下代码:

public async Task<SecurityRoleDeleteResult> AllowDeleteRole(int securityRoleId)
    {
        string url = $"{_housingDataSecurityConfiguration.HousingDataSecurityWebApiUrl}SecurityRoles/AllowDeleteRole/{securityRoleId}";
        var message =  _apiClientService.Retrieve<HttpResponseMessage>(url);

        if (message.StatusCode == HttpStatusCode.InternalServerError)
        {
            return SecurityRoleDeleteResult.ErrorOccurred;
        }

        int intResult = 0;
        var apiResult = await message.Content.ReadAsStringAsync();
        if (int.TryParse(apiResult, out intResult))
        {
            return (SecurityRoleDeleteResult)intResult;
        }
        else
        {
            return SecurityRoleDeleteResult.ErrorOccurred;
        }
    }

在被致电之前:

public  SecurityRoleViewModel BuildViewModelForEdit(int id)
{

    var enableButton = false;
    _securityRoleService.AllowDeleteRole(id).ContinueWith(result =>
        {
            enableButton = (result.Result == SecurityRoleDeleteResult.Success) ? true : false;

    });

    SecurityRoleViewModel model = new SecurityRoleViewModel()
    {
        SecurityRole = _securityRoleService.SecurityRoleRetrieve(id),
        RolePermissions = _securityRoleService.SecurityRolePermissionsRetrieve(id),
        EnableDeleteButton = enableButton 
    };
    return model;
}

我的问题是,当我尝试EnableDeleteButton = enableButton在模型中进行设置时,它会在行上返回以下错误:

enableButton = (result.Result == SecurityRoleDeleteResult.Success) ? true : false;

{“将值3转换为类型'System.Net.Http.HttpResponseMessage'的错误。路径”,第1行,位置1。“}

3引用我的SecurityRoleDeleteResult枚举中的枚举值之一。

戴夫

AllowDeleteRole返回HttpResponseMessageresult您传递给Lambda对象是Type Task<IHttpActionResult>因此,当您result.Result处理对象时,现在IHttpActionResult将更具体地使用HttpResponseMessage它。

这是造成您问题的原因

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章