0

I'm trying to create a simple webrequest for a json, i'm attempting to use the example on MSDN.

// Create a new 'Uri' object with the specified string.
        Uri myUri =new Uri("http://www.contoso.com");
        // Create a new request to the above mentioned URL. 
        WebRequest myWebRequest= WebRequest.Create(**myUri**);
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse myWebResponse= **myWebRequest**.GetResponse();

I'm getting the following error;

A field initializer cannot reference the non-static field, method, or property

on the objects highlighted. (myUri and myWebRequest) Any idea's?

thanks

Nathan
  • 2,461
  • 4
  • 37
  • 48

1 Answers1

2

This will not work because everything in Silverlight must be Async. They force this because all execution on the main thread like a webrequest would lock the UI. This approach provides a better user experience and is a trade-off to having developers master the use of threads for basic development activities.

See this:

How to use HttpWebRequest (.NET) asynchronously?

Community
  • 1
  • 1
Keith Adler
  • 20,880
  • 28
  • 119
  • 189