There are many scenarios where you need to communicate with a Web server from your Silverlight application.For example you might want to lazy load files from your server to your application when the application is idle. Another common situation is during file uploads or when calling REST services.In these circumstances you need to use the Silverlight networking stacks to gain programmatic access to the remote computers.
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
Developers are often surprised to learn that Silverlight has two networking stacks.The original networking stack, known as the browser stack (BrowserHttp), first appeared in Silverlight 2. This was easy for the Silverlight team to create as it simply piggy backs on top of the browsers' HTTP API. But as nice as it is to have this framework it has some drawbacks. At the top of the list are two complaints.
First, the browser stack swallows HTTP exceptions and republishes them as either HTTP 200 or HTTP 404 errors. As you can guess this limits your ability to troubleshoot problems. It was particularly annoying when making SOAP networking calls, as the SOAP error codes were lost.
Second, the browser stack only uses the HTTP GET and POST methods. This becomes a limitation when you want to use REST services.
To solve these problems the Silverlight team created the client networking stack (ClientHttp). As the name implies, this stack bypasses the browser and allows you to do your networking through the operating system. This client stack is separate from the browser stack but is invoked in a similar fashion. If you have code written against the older version it will be unaffected by the addition of this more modern API.
Using the client stack is easy. Call the RegisterPrefix method to inform Silverlight that all subsequent calls to addresses that begin with “http://” should use the client stack rather than the browser stack.
You can drill down into a subsection of a site with the following.
Now all requests to the subdomain are processed by the client stack.
Making a Service call
Here is an example of calling a Web service using the client stack. Take note of the error handling code in the wc_DownloadStringCompleted event procedure. Notice how I'm getting the status code and status description from the Web exception.
You can call the RegisterPrefix anywhere in your code. There is no harm in calling this method more than once though according to the docs you can only do it once per domain. In fact your Silverlight application could have some network calls using the browser stack and others using the client stack. Let's say you want the HTTPS traffic to use browser stack and your HTTP traffic the client.
Each networking stack gives you different benefits. Learn more details at MSDN.