K
kevin cline
I am seeing sporadic SocketExceptions being raised when calling
Stream.EndWrite on the request stream of an HttpWebRequest. The
message is:
An operation was attempted on something that is not a socket
at WriteConnectStream.NetworkWrite()
at System.Net.HttpWriteStream.Write()
at System.Net.HttpWriteStream.asyncWrite()
at WorkItem.doWork()
at System.Threading.Timer.ring()
The code looks like this:
private HttpWebRequest request = null;
private Stream requestStream = null;
...
private void StartRequest(...)
{
request = (HttpWebRequest) WebRequest.Create(...);
...
request.BeginGetRequestStream
(GetRequestStreamComplete, null);
}
private void GetRequestStreamComplete(IAsyncResult result)
{
try
{
requestStream = request.EndGetRequestStream(result);
Write PostData();
}
catch (Exception ex)
{
Cleanup();
}
}
private void WritePostData()
{
try
{
...
requestStream.BeginWrite(data, 0,
data.Length, RequestStreamWriteComplete, null);
}
catch (Exception ex)
{
Cleanup();
}
}
private void RequestStreamWriteComplete(IAsyncResult result)
{
try
{
requestStream.EndWrite(result);
// EndWrite sometimes throws SocketException
requestStream.Close();
...
}
catch (Exception ex)
{
Cleanup();
}
}
private void Cleanup()
{
try
{
...
if (requestStream != null)
{
requestStream.Close();
}
if (request != null)
{
request.Abort();
}
}
catch { }
}
Has anyone else seen this? It happens after some tens of requests have
been created and completed in short order.
Stream.EndWrite on the request stream of an HttpWebRequest. The
message is:
An operation was attempted on something that is not a socket
at WriteConnectStream.NetworkWrite()
at System.Net.HttpWriteStream.Write()
at System.Net.HttpWriteStream.asyncWrite()
at WorkItem.doWork()
at System.Threading.Timer.ring()
The code looks like this:
private HttpWebRequest request = null;
private Stream requestStream = null;
...
private void StartRequest(...)
{
request = (HttpWebRequest) WebRequest.Create(...);
...
request.BeginGetRequestStream
(GetRequestStreamComplete, null);
}
private void GetRequestStreamComplete(IAsyncResult result)
{
try
{
requestStream = request.EndGetRequestStream(result);
Write PostData();
}
catch (Exception ex)
{
Cleanup();
}
}
private void WritePostData()
{
try
{
...
requestStream.BeginWrite(data, 0,
data.Length, RequestStreamWriteComplete, null);
}
catch (Exception ex)
{
Cleanup();
}
}
private void RequestStreamWriteComplete(IAsyncResult result)
{
try
{
requestStream.EndWrite(result);
// EndWrite sometimes throws SocketException
requestStream.Close();
...
}
catch (Exception ex)
{
Cleanup();
}
}
private void Cleanup()
{
try
{
...
if (requestStream != null)
{
requestStream.Close();
}
if (request != null)
{
request.Abort();
}
}
catch { }
}
Has anyone else seen this? It happens after some tens of requests have
been created and completed in short order.