bilpor
bilpor

Reputation: 3889

Error calling async method non asynchronously

I have the following async method:

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

It gets used in numerous places, but for this one instance I need to use it non-async, so I have the following code that first wraps it:

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

before getting called with:

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

My issue is that when I try to set EnableDeleteButton = enableButton in the model, it throws the following error back up on the line:

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

{"Error converting value 3 to type 'System.Net.Http.HttpResponseMessage'. Path '', line 1, position 1."}

3 refers to one of my enum values on my SecurityRoleDeleteResult enums.

Upvotes: 1

Views: 64

Answers (1)

Dave
Dave

Reputation: 3017

AllowDeleteRole returns a HttpResponseMessage. The resultobject you pass into your Lambda is of Type Task<IHttpActionResult>. So when you do result.Result the object you are now working with an IHttpActionResult more specifically it is a HttpResponseMessage it appears.

This is the cause of your problem

Upvotes: 1

Related Questions