Unable to upload string using WebClient.UploadStringAsync

Unanswered Unable to upload string using WebClient.UploadStringAsync

  • Tuesday, May 08, 2012 9:57 AM
     
      Has Code

    Hey everyone,

    I'm having an issue using the UploadStringAsync. I'm currently working on a project that requires simultaneous calls to a web service so I figured I'd use async instead of sync calls. The issue is that the service I'm calling either isn't responding or the C# code isn't sending properly. The example code I'm using is

    static void Main(string[] args)
            {
                WebClient client = new WebClient();
                client.Headers["Content-Type"] = "text/xml";
    
                string url = "webService URL";
                string postParams = "randomCode to upload";
                    
                Uri uri = new Uri(url);
                client.UploadStringCompleted += UploadHandler;
                client.UploadStringAsync(uri, postParams);           
            }
    
            static void UploadHandler(object sender, UploadStringCompletedEventArgs e)
            {
                Console.WriteLine("Triggered");
            }

    Simple enough I thought, except regardless of what I did the handler wouldn't trigger. I know through brute force ( running it 500 times in a loop ) that the URL and params should be correct. Over the span of 500 times I can sometimes get it to trigger once or twice. No error is recorded and when nothing happens, the service doesn't even register that anything has touched it.

    On the other hand, if I change it to UploadString instead, the code works perfectly, except then I have issues since I want several web calls happening concurrently.

    Any help is appreciated, thanks in advance!


All Replies

  • Tuesday, May 08, 2012 10:43 AM
     
     

    Hi,

    What if you test in a Windows Forms app rather than in a console app ? It would allow easily to keep your app running (for now I suspect that the app just ends before the async call is completed and the handler could be called).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".


  • Tuesday, May 08, 2012 11:17 AM
     
     

    Hi, 

    When you trigger UploadStringAsync, it uses threadpool thread to execute work item, if you are not seeing 500 times UploadStringCompleted event raise probably Upload is not completed. (may be) dead lock we can find only if we see code in receiving end as well.

    What I would do is, enable output trace (this member allows check) and find out exactly what is going on behind check how?

    http://msdn.microsoft.com/en-us/library/hyb3xww8.aspx

    I hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • Wednesday, May 09, 2012 2:12 AM
     
     

    To my knowledge the receiving server also runs on C# and has a Page_Load method that immediately triggers a log stating that the service has been touched. This has not occurred for the ASYNC calls that I've been making.

    I have also tried using Fiddler to check the network traffic to see what's going on. From that I see that out of 500 requests. all by 10 or so don't even register as having been sent. For the most part, the rest appear to work properly.

  • Wednesday, May 09, 2012 2:14 AM
     
      Has Code

    I found something that might relate to it. If I put a Thread.Sleep(1000) after the Upload, it appears to work properly. Of course, this defeats the purpose of wanting to do it Async to begin with. If I reduce the sleep amount, there's a higher chance of it not triggering. When I drop it down to 100ms it tends to only register around 97~% of the time. Annoyingly, using ManualResetEvent doesn't work since it appears to just ignore it outright.

    static private int numTimes = 0;
    
            static ManualResetEvent reset = new ManualResetEvent(false);
    
            static void Main(string[] args)
            {
                for (int i = 0; i < 100; i++)
                {
                    WebClient client = new WebClient();
                    client.Headers["Content-Type"] = "text/xml";
    
                    string url = "serviceURL";
                    string postParams = string.Format(LOGIN_TOKEN, "randomParams");
    
                    Uri uri = new Uri(url);
                    client.UploadStringCompleted += UploadHandler;
                    client.UploadStringAsync(uri, postParams);
                    reset.WaitOne(); // <--- Doesn't work...
                    //Thread.Sleep(200); //<--- Works
                }
            }
    
            static void UploadHandler(object sender, UploadStringCompletedEventArgs e)
            {
                reset.Set();
                numTimes++;
                Console.WriteLine("Triggered: " + numTimes + " times.");
            }

    EDIT: Well, I managed to fix the reset issue by adding Reset() to the ManualResetEvent, but that doesn't really do much since it pretty much just changes the entirety of the code into a sync operation. Still not sure why it doesn't register the entire thing is separate ASYNC calls.

    • Edited by Kirbyprime Wednesday, May 09, 2012 2:18 AM
    • Edited by Kirbyprime Wednesday, May 09, 2012 2:23 AM
    • Edited by Kirbyprime Wednesday, May 09, 2012 2:25 AM
    • Edited by Kirbyprime Wednesday, May 09, 2012 3:27 AM
    •  
  • Friday, May 11, 2012 8:16 AM
     
     

    Hi Kirbyprime,

    You can consider posting it at the following more appropriate forum for more efficient responses. Thanks!

    http://social.msdn.microsoft.com/forums/en-US/wcf/threads/


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • Friday, May 11, 2012 2:29 PM
     
     
    The WebClient use the AsyncOperation.  It needs a UI thread for proper operation.  Your code has no SyncronizationContext. TheUploadHandler can run on any thread.