Silverlight 3 and RIA Service – Creating a Proxy for Cross Domain HttpRequests

One of the first things I thought about after writing my first two articles on RIA Services (Basic and Advanced) was how I could take advantage of this new technology.

The first thing I could thing of was a solution that helps overcoming the Cross Domain Issues when accessing resources on the internet. What about something like a proxy service to overcome cross domain issues? Access our known proxy server which will forward the request to the actual resource on the internet.

image

Let’s first define the interface to communicate through. A very simple ProxyRequest that contains the resource to access on the ‘Any resource’ side, and an Id to make sure we can correctly associate the Request and the Response later on.

namespace MM.Ria.CrossDomainProxy
{
    public class ProxyRequest
    {
        [Key]
        public Guid Id { get; set; }

        public string Url { get; set; }
    }
}

The ProxyResponse is very simple as well. Almost the same as the ProxyRequest a RequestId associated with the Id in the ProxyRequest, the Content and of course something to store any error that occurred.

namespace MM.Ria.CrossDomainProxy
{
    public class ProxyResponse
    {
        [Key]
        public Guid RequestId { get; set; }

        public string Content { get; set; }

        public string Error { get; set; }
    }
}

And we need an Interface with an Operation, let’s call it process.

namespace MM.Ria.CrossDomainProxy
{
    public interface IProxy
    {
        ProxyResponse Process(ProxyRequest request);
    }
}

We now create the RIA Service implementation, by just delegating everything to a RegularProxy implementation. So this one is simple as well.

namespace MM.Ria.CrossDomainProxy
{
    [EnableClientAccess]
    public class DomainProxy : DomainService, IProxy
    {
        private readonly RegularProxy m_proxy = new RegularProxy();

        [ServiceOperation]
        public ProxyResponse Process(ProxyRequest request)
        {
            return m_proxy.Process(request);
        }
    }
}

And the magic is in the RegularProxy, don’t think we should call it magic though. Just an usage of the WebClient class to download the data from the Url.

namespace MM.Ria.CrossDomainProxy
{
    public class RegularProxy : IProxy
    {
        public ProxyResponse Process(ProxyRequest request)
        {
            var response = new ProxyResponse {RequestId = request.Id};
            try
            {
                using (var client = new WebClient())
                {
                    string data = client.DownloadString(request.Url);
                    response.Content = data;
                }
            }
            catch (Exception exception)
            {
                response.Error = exception.Message;
            }

            return response;
        }
    }
}

Don’t forget this is a very basic Proxy implementation and doesn’t implement all the features we can think of. But this first basic set up can easily be enhanced with for example Credentials support, Caching and other things as well.

But how can we use it in our Silverlight application? Simple as well.

var domainProxy = new DomainProxy();
domainProxy.ProcessCompleted += (sender, e) => Debug.WriteLine((e.ReturnValue as ProxyResponse).Content);
domainProxy.Process(new ProxyRequest
                        {
                            Id = Guid.NewGuid(), 
                            Url = "http://www.silverlight.net/"
                        });

Looks like the keyword is simple when we’re creating RIA Services. I really like it.

Ps. This article is cross posted on: Mark Monster’s blog and Silverlight Help.