banderbe

Hi everyone, I am having a problem. (Using .NET 2.0)

If I call WebClient.DownloadDataAsync from a method called by ThreadPool.QueueUserWorkItem, the DownloadDataComplete is called with an error reported from HttpWebRequest.

If I make the same call to WebClient.DownloadDataAsync from a method called on the main UI thread, the DownloadDataComplete is called with success and contains the data of the webpage I am trying to load.

You can run the code below and click button 1, and watch the console (or VS.NET IDE output window), and see "Unable to connect to remote server." Then if you click button 2, you will see the data from the webpage come back.

Oddly, if you run the program and click button 2 before button 1, button 1 gets the webpage data successfully.

What's going on here!

Here is the MainForm.cs code:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender, EventArgs e )
{
System.Threading.ThreadPool.QueueUserWorkItem( new System.Threading.WaitCallback( Do ) );
}

private void button2_Click( object sender, EventArgs e )
{
Do( null );
}

private void Do(object o)
{
WebClient client = new WebClient();
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler( client_DownloadDataCompleted );
client.DownloadDataAsync( new Uri("http://www.google.com" ) );
}

void client_DownloadDataCompleted( object sender, DownloadDataCompletedEventArgs e )
{
if( e.Error == null )
{
Console.WriteLine( Encoding.UTF8.GetString( e.Result ) );
}
else
{
Console.WriteLine( e.Error.Message );
}
}
}



Re: .NET Framework Networking and Communication Help with WebClient (code snippet included)

banderbe

A few things I have already tried that didn't make a difference:

1) Moving the WebClient object to class-level scope.

2) Using a System.Threading.AutoResetEvent in the 'Do' method and in the callback so that the threadpool thread can't terminate until the DownloadDataCompleted event is handled completely.


Those were my two theories about what could be going wrong.. but they didn't make a difference..

HELP!!




Re: .NET Framework Networking and Communication Help with WebClient (code snippet included)

banderbe

Here is the Network trace output from the .NET Framework:

System.Net Verbose: 0 : [10564] MyWebClient#41622463::DownloadDataAsync(http://www.google.com/#793723296)
System.Net Verbose: 0 : [10564] WebRequest::Create(http://www.google.com/)
System.Net Verbose: 0 : [10564] HttpWebRequest#31364015::HttpWebRequest(http://www.google.com/#793723296)
System.Net Verbose: 0 : [10564] Exiting HttpWebRequest#31364015::HttpWebRequest()
System.Net Verbose: 0 : [10564] Exiting WebRequest::Create() -> HttpWebRequest#31364015
System.Net Verbose: 0 : [10564] HttpWebRequest#31364015::BeginGetResponse()
System.Net Information: 0 : [10564] Associating HttpWebRequest#31364015 with ServicePoint#28379535
System.Net Information: 0 : [10564] Associating Connection#64109423 with HttpWebRequest#31364015
System.Net.Sockets Verbose: 0 : [10564] Socket#35236192::Socket(InterNetwork#2)
System.Net.Sockets Verbose: 0 : [10564] Exiting Socket#35236192::Socket()
System.Net.Sockets Verbose: 0 : [10564] Socket#35236192::BeginConnectEx()
System.Net.Sockets Verbose: 0 : [10564] Socket#35236192::InternalBind(0:0#0)
System.Net.Sockets Verbose: 0 : [10564] Exiting Socket#35236192::InternalBind()
System.Net.Sockets Verbose: 0 : [10564] Socket#35236192::Dispose()
System.Net Error: 0 : [10564] Exception in the HttpWebRequest#31364015:: - Unable to connect to the remote server
System.Net Error: 0 : [10564] at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
System.Net Verbose: 0 : [10564] HttpWebRequest#31364015::EndGetResponse()
System.Net Error: 0 : [10564] Exception in the HttpWebRequest#31364015::EndGetResponse - Unable to connect to the remote server
System.Net Error: 0 : [10564] at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback)
at System.Net.Connection.CompleteStartConnection(Boolean async, HttpWebRequest httpWebRequest)
System.Net Verbose: 0 : [10564] HttpWebRequest#31364015::Abort()
System.Net Error: 0 : [10564] Exception in the HttpWebRequest#31364015:: - The request was aborted: The request was canceled.
System.Net Verbose: 0 : [10564] Exiting HttpWebRequest#31364015::Abort()
System.Net Verbose: 0 : [10564] Exiting HttpWebRequest#31364015::BeginGetResponse() -> ContextAwareResult#21943666
System.Net Verbose: 0 : [10564] Exiting MyWebClient#41622463::DownloadDataAsync()




Re: .NET Framework Networking and Communication Help with WebClient (code snippet included)

banderbe

I'm still looking for help on this issue.




Re: .NET Framework Networking and Communication Help with WebClient (code snippet included)

Indian Ocean

Hi,

I have checked above code many times, there is no logical or practical problem i found.

I have tested the code but both buttons worked fine so many times regardless of which button called first.

The Trace msgs you sent shows that Socket gets disposed due to some problem.

You can try after stopping firewall if any or even stopping the antivirus programs which runs continously in backgroup.

Give it a try after that and please inform me the result so we can diagnose it more.

If still not solved, please post your InnerException by catching it through code.

HTH,