0

I have the following code:

public async Task IntiateDataFetchingProcess(string[] args)
{
    try
    {           
        ProcessArgs(args);

        Log.Information("Run Mode: {RunModeID}", RunModeID);

        switch (RunModeID)
        {
            case RunModeType.A:
                await MethodAAsync();
                break;
            case RunModeType.B:
                await MethodBAsync();
                break;
            case RunModeType.C:
                TestMethod();
                break;
            default:                       
                break;
        }
    }
    catch (Exception ex)
    {                
        throw;
    }            
}

private async Task MethodBAsync()
{
   Console.WriteLine(DateTime.Now.ToLongTimeString());

   // Call to webservice to get the data
   var response = await _service.GetDataAsync(input1, request);

   Console.WriteLine(DateTime.Now.ToLongTimeString());  
}

On debugging I found that the execution call comes to the below line (of method: MethodBAsync) and stops there.

var response = await _service.GetDataAsync(input1, request);

Can anyone help me to know is there anything that I am missing here.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • What do you mean by "stops there"? Is the request never completing? – Yuval Itzchakov Sep 12 '16 at 11:34
  • The execution of the code stops there. It is not moving to the next line. – santosh kumar patro Sep 12 '16 at 11:35
  • @santoshkumarpatro, Check to see if you have any blocking method calls within that method ie `.Result`, `.Wait` etc... – Nkosi Sep 12 '16 at 11:39
  • 2
    Perhaps you misunderstand what `async/await` do? `await` means `await`. It releases the current thread, *awaits* for it to finish, unpacks the result and resumes execution *after* the asynchronous call after it completes. It *doesn't* stop the execution of other threads. – Panagiotis Kanavos Sep 12 '16 at 11:40
  • To convince yourself, open the "Parallel Stacks" window from the Debug menu. You'll see that the method with `await` uses a different stack from the rest of the program. – Panagiotis Kanavos Sep 12 '16 at 11:43
  • 1
    Almost always, look for a call to `Result` or `Wait` not just in this method but higher up the "call stack" also. Almost always that's the root cause of the deadlock. – Damien_The_Unbeliever Sep 12 '16 at 11:47
  • Here I am consuming a third party webservice using the proxy classes.I checked thoroughly for the call to Result or Wait and found no where they are used. – santosh kumar patro Sep 12 '16 at 12:35
  • I am trying to call the above method from a console application. Will it make any difference there. – santosh kumar patro Sep 13 '16 at 12:12
  • This is not about the central point of the question, but you should never use `throw ex;`. Use simply `throw;`, unless you don't want to deliberately erase the Exception stack trace. See [this question](http://stackoverflow.com/questions/730250/is-there-a-difference-between-throw-and-throw-ex) for a wider discussion. I corrected it in my edit (visible soon) – Massimiliano Kraus Dec 02 '16 at 10:37

1 Answers1

1

Ah, your code is getting deadlocked! You just need to add .ConfigureAwait(false); to each line that you are awaiting.

Example:

var response = await _service.GetDataAsync(input1, request);

becomes

var response = await _service.GetDataAsync(input1, request).ConfigureAwait(false);

For more information on .ConfigureAwait(), Stephen Cleary wrote an awesome post on it.

ermish
  • 1,160
  • 2
  • 13
  • 26