Control Not Coming Back to Parent Method Even after Using Aync and Await

Ali Basha 21 Reputation points
2022-08-20T17:03:53.55+00:00

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

   } }  }  

 
Long Running Task 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,

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,403 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.7K Reputation points
    2022-08-21T08:16:27.367+00:00

    If you are not interested in the result (savemsg), then try this:

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


0 additional answers

Sort by: Most helpful