Webclient methods not available to my Silverlight application

Trying to make basic web client data in C #, and methods are not available in visualstudio, and the code does not compile.

//snip WebClient client = new WebClient(); byte[] resp = client.DownloadData(url); //snip 

Error 1 "System.Net.WebClient" does not contain a definition for "DownloadData" and does not use the extension method "DownloadData", which takes the first argument of the type "System.Net.WebClient" (do you miss using the directive or link to the assembly?) C: \ Users \ Michael \ Documents \ Visual Studio 2008 \ Projects \ search2 \ search2 \ MainPage.xaml.cs

I am doing this in a C # file for a XAML / Silverlight project, but cannot imagine why this would make a difference. I can’t find a link to this problem on the Internet, and I had something similar to this work last month, but on a regular ASP.NET page, and not in a Silverlight application.

+4
source share
3 answers

Silverlight has a truncated / compact version of the .NET Framework. See the MSDN documentation for WebClient in Silverlight to see that it has very few methods compared to its counterpart in the full .NET Framework.

+3
source

Silverlight has only part of the full .NET functionality. Moreover, it does not contain blocking methods for communicating with web services. This makes life difficult for programmers, but for users it’s a fantastic thing - you cannot (easily) write Silverlight applications that will freeze when the server does not respond quickly.

If you can pass the data as text, you can use the DownloadStringAsync method:

 var wc = new WebClient(); wc.DownloadStringCompleted += (sender, e) => { string data = (string)e.Result; // Process the data here } wc.DownloadStringAsync(new Uri(address)); 

If you need to pass binary data, you probably have to explicitly use the HttpWebRequest class .

+4
source

You can best achieve what you want in Silverlight using WebClient.OpenReadCompleted and OpenReadAsync. This will return the stream. You can use this directly or copy the stream into bytes [], as described here, for example: http://www.yoda.arachsys.com/csharp/readbinary.html

Also, be careful that the Silverlight functionality for WebClient is NOT a subset of .NET. If it were a subset, then what they both did, they would do the same. But this is not so. For example, SL4 OpenReadAsync is truly asynchronous. It does not block the calling thread at all. However, in .NET4, OpenReadAsync and DownloadDataAsync partially block the calling thread and block it while it is running in the debugger. To get a truly non-UI-blocking effect in .NET4, you need to start the download in a separate thread yourself. In addition, in Silverlight, DownloadProgressUpdated works great at boot time for OpenReadAsync. In .NET, this is not so. However, .NET DownloadDataAsync launches DownloadProgressUpdated in a very similar fashion to Silverlight OpenReadAsync.

So, if you want to achieve consistency between the WPF project and Silverlight, you can use OpenReadAsync directly on the SL side. On the WPF side, run DownloadDataAsync in a separate thread:

 Thread downloadThread = new Thread(new ThreadStart(() => wc.DownloadDataAsync(uri))); downloadThread.Start(); 

Then in DownloadDataCompleted, create a MemoryStream from the returned byte [] if you prefer it in streaming form. (I did not find any extra overhead to significantly affect performance.)

0
source

Source: https://habr.com/ru/post/1301996/


All Articles