**Note I had asked this question which got marked as Duplicate but the Duplicate question did not help in solving the issue I am having below.
Newbie to async programming in C# and I am having some difficulty in getting the below working.
I am using an External WebService to Obtain CarInfo - they have provided Async methods which in the example below returns a string for a Number of Cars objects passed in. I have my own abstraction of this WebService in an interface something like below:
Task<string> GetTicketIdAsync(Cars[] cars);
My Implementation of interface method is as below:
public async Task<string> GetTicketIdAsync(Ccars[] cars)
{
try
{
if (_externalServiceClient == null)
{
_externalServiceClient = new ExternalServiceClient("WSHttpBinding_IExternalService");
}
string ticketID =
await _externalServiceClient .GetCarInfosAsync(cars)
return ticketID;
}
catch (Exception ex)
{
//TODO log 4 net
throw new Exception("Failed" + ex.Message);
}
finally
{
//WCF - Dispose and close client and then set to null
CloseClient(_externalServiceClient );
_externalServiceClient = null;
}
In another class then I have a method as below which add some cars to DB and then calls a private method in same class to get ticket it from webservice.
public void AddCars(List<Cars> cars)
{
var ticketId = UpdateCarWithTicketId(cars);
string test = "Hello-World";
}
My UpdateCars with TicketId method is as below:
private async Task<string> UpdateCarWithTicketId(List<Cars> cars)
{
//Call my abstraction of external web service method
string ticketId = await _myService.GetTicketIdAsync(cars);
foreach (var car in cars)
{
cars.TicketId = ticketId;
}
//Update DB
_carRepository.Update(cars);
return ticketId;
}
A few things - if I set a breakpoint on the foreach loop var car in cars it never seems to get hit so the DB does not get updated. And then if I set a breakpoint on my AddCars method on the string test = "hello world" line - the value of ticketId shows status WaitingForActivation when I was expecting a unique string returned from my call to external web service.
I tried adding ConfigureAwait(false) to both of the calls where await was used but still getting same result. Is there something I am missing in configuring async method to run correctly?
EDIT -
Currently this is being call from MVC Controller when User hits button on screen -
[AcceptVerbs(HttpVerbs.Post)]
public async Task<ActionResult>Upload(CarImport viewModel)
{
//list of cars uploaded by user in Excel sheet - code to extract that removed
await _CarInfoService.AddCars(cars);
}
return RedirectToAction("Home");
}
However I get a error on Build saying cannot await void - the AddCars method signature on my _CarInfoService is void - should that be changed to something else?