If you are not interested in the result (savemsg), then try this:
Task<string> t = _masterDAL.SaveProjectUserSecurity(_UserJobFunctionTeamList[0].JobFunctionGUID, oMaster.RequestId);
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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,
If you are not interested in the result (savemsg), then try this:
Task<string> t = _masterDAL.SaveProjectUserSecurity(_UserJobFunctionTeamList[0].JobFunctionGUID, oMaster.RequestId);
Try this modification:
Task<string> t = _masterDAL.SaveProjectUserSecurity(_UserJobFunctionTeamList[0].JobFunctionGUID, oMaster.RequestId);
Remove the await on line two in front of calling SaveProjectUserSecurity() This also implies the message returned from SaveProjectUserSecurity() will not be available.
Dear Viorel,
Thanks for the help. I have modified the code like below, it worked.
But can you please tell me, why it its not working if we use await key word like below
Sign in to comment