非异步调用异步方法时出错

我有以下异步方法:


[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;

        }

    }


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


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

{"将值 3 转换为类型 'System.Net.Http.HttpResponseMessage' 时出错。路径 '',第 1 行,位置 1。"}


3 指的是我的 SecurityRoleDeleteResult 枚举上的枚举值之一。


杨__羊羊
浏览 154回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP