Saturday, July 25, 2009

How to host Silverlight (.xap) on Google App Engine with Java SDK

Stars: 
...or any other file with a MIME-type that Google App Engine does not treat nice by default.

Problem: Lack of control over MIME-types and static files on Google App Engine

Apparently, it is not possible to host a .xap file as a static file on Google App and register a correct MIME-type for it. If you search around, you might find hints that it is possible to do with the Python SDK (configuring handlers), but you might also find people can't get the MIME-settings to work with the Java SDK. Recently, I was one of the later, so here is a solution for you that hopefully will save you some time.

Solution: Create a servlet that loads the .xap file as a resource and streams it to the client
This part might sound complex but really is simple. Here are the steps to get it working, though you might need to refine it for production:

1) Produce your .xap file, test .html file and silverlight.js file and copy those into your war folder. Here is my test example structure in an Eclipse project:

2) Create a HttpServlet-extending Java class file (that I named NaiveFileServlet.java) with content similar to the following:



3) Register .xap files as a servlet resource in appengine-web.xml:


4) Register your servlet and URL-patterns in file web.xml:


5) Deploy your app as you normally do, visit the test .html page and enjoy your Silverlight app.

Here is a running example: http://yoaws-slogaet.appspot.com/ (click and move your mouse).


No comments:

Post a Comment