I am caught into this very weird situation when I can only use await one time. See the example below:
private async Task RegisterInstruments(byte clientId, RegisterInstrumentRequest registerInstrumentRequest, bool isPriority = false)
{
if (registerInstrumentRequest != null && registerInstrumentRequest.RegisterIntrstuments != null)
{
var r1 = await instrumentManager.GetInstrumentAsync("AMZN");
logger.Debug("reached r1");
var r2 = await instrumentManager.GetInstrumentAsync("AMZN");
logger.Debug("reached r2");
}
}
Serilog output:
reached r1
When I start the debugger and execute line by line, I can execute till line var r1 = ...
and gives the expected result into r1
then I press F10 and the debugger will just finish as I would have clicked Continue and my winform UI will appear.
Debugger never returns back to the code when the call is complete. GetInstrumentAsync
method is just fetching record from the database using EntityFramework (code below). And that really doesn't take much time.
Also, it doesn't print anything unusual in Output window under Debug section.
What I have tried:
- Use
try/catch
but it never gets inside thecatch
block too. - Calling
GetInstrumentAsync
using.Result
and withoutawait
. It is just same. - I developed the normal version of this method (without
async/await
) and use it works as expected. - Try to run without debugger (Ctrl + F5) to make sure if it isn't a Debugger related issue. But the behaviour remains the same.
I cannot understand why this is happening.
The code for GetInstrumentAsync
method:
public async Task<DsInstrument> GetInstrumentAsync(string fullName)
{
return await _dbContext.Instruments
.AsNoTracking()
.SingleOrDefaultAsync(m => m.FullName == fullName);
}
Rest of the code (some statements removed for brevity) :
public SocketManager(Framework framework)
{
socket.ReceiveReady += SocketServer_ReceiveReady;
}
// I cannot make this `async Task` as this is an even listener. It must return void not Task. So I am using .Wait()
private void SocketServer_ReceiveReady(object sender, NetMQSocketEventArgs e)
{
ProcessMessage(e.Socket.ReceiveMultipartMessage()).Wait();
}
private async Task ProcessMessage(NetMQMessage netMQFrames, bool isPriority = false)
{
RequestMessageType messageType = (RequestMessageType)netMQFrames[2].ConvertToInt32();
byte clientId = netMQFrames[0].Buffer[0];
byte[] messagBuffer = netMQFrames.FrameCount > 3 ? netMQFrames[3].Buffer : null;
switch (messageType)
{
// Remved other cases for brevity
case RequestMessageType.RegisterInstruments:
RegisterInstrumentRequest registerInstrumentRequest = MessageParser.Deserialize<RegisterInstrumentRequest>(messagBuffer);
await RegisterInstruments(clientId, registerInstrumentRequest, isPriority);
break;
}
}