I’m trying to learn C#/Silverlight/Windows Phone 7. What’s going on here: When I try to use examples straight off of MS’s MSDN site, and I get all kinds of errors:
For example:
using System;
using System.IO;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Device.Location;
using Microsoft.Phone.Reactive;
private void registerPhone(object sender, RoutedEventArgs e)
{
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
}
It tells me
1) The name 'Encoding' does not exist in this context
2) System.Net.WebRequest does not contain a definition for GetStreamRequest and no extension method GetRequestStream accepting a first argument of type System.Net.WebRequest could be found (are you missing a using directive... etc
3) a similar message for ContentLength
4) a similar message for GetResponse
I have no idea what libraries I need to be “using” and even when I think I’m “using” the right ones, it gives me errors. What am I doing wrong?
The
Encodingclass resides in theSystem.Textnamespace. You have to add that to the “usings”.As far as for the other messages, this has to do with the limitations of Silverlight. The MSDN samples, I presume, were taken from the “big” version of .NET Framework, weren’t they?
In Silverlight, you see, some things are not allowed in order to prevent undesirable application behavior (such as blocking calls), and some things are not supported due to resources limitation.
In particular,
ContentLengthis one of the latter. The library will determine the content length based on the actual amount of data you write into the request. Just remove that line, you’ll be ok.But
GetResponseStream()is actually one of the former. It has to do with the fact that this operation implies actually opening network connection, which may take a while. And since blocking calls are not allowed in Silverlight, theGetResponseStream()method also has to go.Instead, you’re supposed to use the so-called “asynchronous pattern” – that is, the pair of methods
BeginGetResponseStream/EndGetResponseStream. You do it by calling theBeginmethod and providing a callback that will be called once the operation is complete. Then, inside that callback, you use theEndmethod to get the result of the operation. Like so:Same goes for the
GetResponsemethod.