Monday, March 10, 2008

Quick Silverlight Tip: Networking in Silverlight 2.0 or my I'm getting HttpStatusCode.NotFound

If you're trying to implement sample XSS networking with Silverlight as it explained in QuickStart guide, but with your own remote host, you'll probably get strange "Not Found" error. You'll check your host, firewall setting, etc and everything looks ok. So what's the problem?

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(new Uri(""));
           req.BeginGetResponse(getResponse, req);

void getResponse(IAsyncResult ar)
           HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
            HttpWebResponse res = (HttpWebResponse)req.EndGetResponse(ar);
            if (res.StatusCode == HttpStatusCode.OK)


WebClient client = new WebClient(new Uri(""));
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

       void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
           //Update progress

The reason for it, two small lines in the guide: "Note that the server hosting the feed must opt-in to cross-domain access by providing a Clientaccesspolicy.xml or Crossdomain.xml file at the root of the domain."

Oh, baby, how can I missed it? Create your own Crossdomain.xml now and put it into root directory of your web server. Remember to register XAP MIME type too :)

Have a nice day

No comments: