Home > C#, REST, Windows Phone 7 > Adding synchronous methods to WebRequest on Windows Phone 7

Adding synchronous methods to WebRequest on Windows Phone 7

In general, I really like developing for Windows Phone 7. The tools like Visual Studio and Blend are of excellent quality and both Silverlight and XNA are very good frameworks. I only have one big annoyance and that’s the fact WebRequest does not support synchronous method calls. I am completely aware of the fact that you should not do heavy operations on the UI thread and I perform all my REST calls on a background thread, but I don’t like the fact that Microsoft forces me to make these calls asynchronously. As a developer, I would prefer to make this choice myself.

I have created 2 simple extension methods to add synchronous behavior to WebRequest.

public static class WebRequestExtensions
{
	public static WebResponse GetResponse(this WebRequest request)
	{
		AutoResetEvent autoResetEvent = new AutoResetEvent(false);

		IAsyncResult asyncResult = request.BeginGetResponse(r => autoResetEvent.Set(), null);

		// Wait until the call is finished
		autoResetEvent.WaitOne();

		return request.EndGetResponse(asyncResult);
	}

	public static Stream GetRequestStream(this WebRequest request)
	{
		AutoResetEvent autoResetEvent = new AutoResetEvent(false);

		IAsyncResult asyncResult = request.BeginGetRequestStream(r => autoResetEvent.Set(), null);

		// Wait until the call is finished
		autoResetEvent.WaitOne();

		return request.EndGetRequestStream(asyncResult);
	}
}

Please note: using an AutoResetEvent comes with a performance penalty, so this code is less efficient than a real synchronous method implementation.

Plase note 2: I know that you normally can also retrieve a WaitHandle from an IAsyncResult object instead of creating an AutoResetEvent manually, but this is not possible on WP7.

About these ads
Categories: C#, REST, Windows Phone 7
  1. May 24, 2011 at 14:21

    Hi Pieter. If you test your code, you will find that it deadlocks if you execute it on the UI thread. It’s not possible to work around the async nature of WP7/Silverlight with such threading constructs. Because the UI thread is required by the runtime to actually execute the network communication, your code will only work if it’s executed on a separate thread.

    Regards,
    -Peter

  2. Bryan
    November 10, 2011 at 19:06

    Thanks. This has been helpful. I have a bunch of calls working in a background thread where the next GetResponse call needs to wait till the first call is complete. This extension makes my code easy and clean. I agree with Pieter, I don’t like being forced to use asynchronous calls.

  3. January 10, 2012 at 13:38

    Thanks. I tried to send request in different thread but then also I’m not able to achieve synchronous call. It hangs on waitOne call (if no timeout provided). Can you share the sample application you have prepared?

  4. September 17, 2013 at 05:45

    Hi Pieterdery

    Indeed its a nice code. I checked your open source and found you have been using GetResponse() in HttpCommunicationChannel.cs. How did you do that? as per the following post

    http://social.msdn.microsoft.com/Forums/en-US/be33d8e0-31e6-4da5-a073-adb65f7828dd/windows-phone-with-c-webrequestgetresponse

    Please let me know how did you get access to GetResponse().

    Thanks in advance.

  1. September 9, 2011 at 09:09

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: