Using the client networking stack in Silverlight

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.

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.

HttpWebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

You can drill down into a subsection of a site with the following.

HttpWebRequest.RegisterPrefix("http://blog.wpfwonderland.com", WebRequestCreator.ClientHttp);

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.

WebRequest.RegisterPrefix("http://":, WebRequestCreator.ClientHttp);

WebRequest.RegisterPrefix("https://", WebRequestCreator.BrowserHttp);

Each networking stack gives you different benefits. Learn more details at MSDN.

This was first published in April 2011

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.