ASP.Net Core Web API: Control not coming to Parent Method when using Async and Await

alibasha
 
on Aug 21, 2022 10:52 PM
585 Views

Hi,

I have Web API method (in .net Core), under which I am calling a long running method, but control is coming back to Parent method, only after executing the logic of Long Running method.

Below is my code.

[HttpPost]
[Authorize(AuthenticationSchemes = Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerDefaults.AuthenticationScheme)]
public async Task<IActionResult> Post([FromBody] Master oMaster)
{
    try
    {
        if (ModelState.IsValid)
        {
             case MasterHelper.Tables.JobFunctionTeamLink:
                        var oJobFunctionTeamLink = Common.GetConversionJsonObject<JobFunctionTeamLink>(oMaster.Data.ToString());
                        var _oJobFunctionTeamLinkObject = Common.GetConvertionObjectByType<JobFunctionTeamLink>(oJobFunctionTeamLink);
                        if (Validator.TryValidateObject(_oJobFunctionTeamLinkObject, new ValidationContext(_oJobFunctionTeamLinkObject), validationresults, true))
                        {
                            var data = new JobFunctionTeamLink();
                            var missingattributs = Common.GetPropertiesNameOfClass(data, oJobFunctionTeamLink);
                            if (missingattributs != null && missingattributs.Count > 0)
                            {
                                return StatusCode(StatusCodes.Status400BadRequest, Common.ResponseFailure(oMaster.RequestId.ToString(), string.Join(",", missingattributs)));
                            }
                            var _UserJobFunctionTeamList = Common.GetPropertiesNameOfClassUserTeam(_oJobFunctionTeamLinkObject);
                            message = _masterDAL.SaveJobFunctionTeamLinkDetails(_UserJobFunctionTeamList, oMaster.RequestId);
                           string savemsg= await _masterDAL.SaveProjectUserSecurity(_UserJobFunctionTeamList[0].JobFunctionGUID, oMaster.RequestId);
                            return string.IsNullOrEmpty(message)
                            ? StatusCode(StatusCodes.Status200OK, Common.ResponseSucess(oMaster.RequestId.ToString()))
                            : StatusCode(StatusCodes.Status400BadRequest, Common.ResponseFailure(oMaster.RequestId.ToString(), message));
                        }
                    }
                } 
            }

LongRunningTask which would take more time,is shown below.

public async Task<string> SaveProjectUserSecurity(Guid JobFunctionGuid, Guid reqId)
{
    try
    {
        string dbmessage=string.Empty;
        await Task.Run(() =>
        {
            var sql = new SqlHelper(ConnectionString);
            sql.ExecuteNonQuery("sp_CRM_SaveProjectUserSecurity", "@reqId", "@JobFunctionGUID", reqId, JobFunctionGuid, out dbmessage, CommandType.StoredProcedure, "@strComments");
 
        }).ConfigureAwait(true);
        return dbmessage;
    }
    catch (Exception ex)
    {
        throw;
    }
}

Kindly note, control is coming back to parent method only after executing the method "SaveProjectUserSecurity", I tried without ConfigureAwait, with ConfigureAwait(True) and also ConfigureAwait(false), all 3 not working as expected.

Any help or clue would be highly appreciated. Thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
alibasha
 
on Aug 23, 2022 05:16 AM

Hi Shivam, 

I was talking about the ConfigureAwait available at line no.11 above.

Anyway, I was able to resolve this issue, by changing the code below.

 Task<string> savemsg = _masterDAL.SaveProjectUserSecurity(_UserJobFunctionTeamList[0].JobFunctionGUID, oMaster.RequestId);

 

public async Task<string> SaveProjectUserSecurity(Guid JobFunctionGuid, Guid reqId)
{
    //Logic removed to save space
    return dbmessage;
}