public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
{
if (NetEventSource.IsEnabled)
{
NetEventSource.Enter(this);
NetEventSource.Info(this, $"Method: {_methodInfo.Method}");
}
ContextAwareResult asyncResult;
try
{
if (_ftpWebResponse != null)
{
asyncResult = new ContextAwareResult(this, state, callback);
asyncResult.InvokeCallback(_ftpWebResponse);
return asyncResult;
}
if (_getResponseStarted)
{
throw new InvalidOperationException(SR.net_repcall);
}
_getResponseStarted = true;
CheckError();
RequestStage prev = FinishRequestStage(RequestStage.RequestStarted);
asyncResult = new ContextAwareResult(true, true, this, state, callback);
_readAsyncResult = asyncResult;
if (prev >= RequestStage.RequestStarted)
{
// To make sure the context is flowed
asyncResult.StartPostingAsyncOp();
asyncResult.FinishPostingAsyncOp();
if (prev >= RequestStage.ReadReady)
asyncResult = null;
else
{
lock (_syncObject)
{
if (_requestStage >= RequestStage.ReadReady)
asyncResult = null; ;
}
}
if (asyncResult == null)
{
// need to complete it now
asyncResult = (ContextAwareResult)_readAsyncResult;
if (!asyncResult.InternalPeekCompleted)
asyncResult.InvokeCallback();
}
}
else
{
// Do internal processing in this handler to optimize context flowing.
lock (asyncResult.StartPostingAsyncOp())
{
SubmitRequest(true);
asyncResult.FinishPostingAsyncOp();
}
FinishRequestStage(RequestStage.CheckForError);
}
}
catch (Exception exception)
{
if (NetEventSource.IsEnabled) NetEventSource.Error(this, exception);
throw;
}
finally
{
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
}
return asyncResult;
}