Al Pascual



Geo RSS
Geo Twitter Timeline

Blogs I read

<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

Cross domain request in Silverlight 2.0 beta 2

Silverlight Logo

So you started using Silverlight and you wanted to download a RSS feed, talk to a web service or download a few images from a website to realize that many of the requests you made come back to you empty or you get the 404 error. How do I get the error when I know that web page or image is there?

There are 2 methods in Silverlight to make HTTP requests to a server, both methods WebClient and HttpWebRequest check at the destination domain for the file crossdomain.xml

WebClient webClient = new WebClient();               
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.Downloaded);
webClient.DownloadStringAsync(Uri);
and …
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri);

request.BeginGetResponse(new AsyncCallback(ReadCallback), request); 

There is no a restriction on the browser that prevents Silverlight to do a cross domain request, I do not know why Microsoft still keeps that restriction from the Silverlight plugin as just makes the developer having to create a server side proxy in each application to accomplish those requests.

 

<%@ WebHandler Language="C#" Class="SilverlightProxy" %>

using System;
using System.Web;

public class SilverlightProxy : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {

        string url = context.Request.QueryString[0];

        System.Net.WebRequest req = System.Net.WebRequest.Create(new Uri(url));
        req.Method = context.Request.HttpMethod;

        if (context.Request.InputStream.Length > 0)
        {
            byte[] bytes = new byte[context.Request.InputStream.Length];
            context.Request.InputStream.Read(bytes, 0, (int)context.Request.InputStream.Length);
            req.ContentLength = bytes.Length;
            req.ContentType = "application/x-www-form-urlencoded";
            System.IO.Stream outputStream = req.GetRequestStream();
            outputStream.Write(bytes, 0, bytes.Length);
            outputStream.Close();
        }

        System.Net.WebResponse response = req.GetResponse();
        context.Response.ContentType = response.ContentType;

        System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream());

        // Text responses
        if (response.ContentType.Contains("text"))
        {
            string textResponse = stream.ReadToEnd();
            context.Response.Write(textResponse);
        }
        else
        {
            byte[] bResponse = new byte[response.ContentLength];
            stream.BaseStream.Read(bResponse, 0, (int)response.ContentLength);
            context.Response.BinaryWrite(bResponse);        
        }
        context.Response.Flush();
        context.Response.End();       
        
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

Download the Silverlight proxy here

All your request that are cross domain will have to call the silverlightProxy.ashx file before: SilverlightProxy.ashx?http://silverlightme.net/al.jpg

Flex also work in the same way, is that why Microsoft design it that way? I would think that having socket support from the Silverlight plugin, you would be able to create a socket to a server in TCP IP instead of using HTTP as the protocol.

Cheers

Al

Posted from http://weblogs.asp.net/albertpascual

Comments

Haissam Abdul Malak said:

You are absolutely right. I think Microsoft designed it as Adobe flex. Currently i'm working on a project which needs communication between Adobe flex and .NET and we faced the security problem which needs the crossdomain.xml on each server you want to retrieve data from (if you want to call directly through flex), or you can handle it on the ASP.NET part. Regards,
# September 11, 2008 2:53 AM