Monday, June 25, 2007

Cross-domain calls and server side debugging of Silverlight application

Well, in this post, I'll touch two of most major problems with Silverlight development: Cross-Domain remote calls and Server-Side development and debugging. Two of those problem are related, but still happens one without an other. So, let's start with the first one. How to perform denied cross domain class in Silverlight. Probably all of you tried to get Silverlight sample of RSS reader work with external rss feed and got an exception: "Cross domain calls are not supported by BrowserHttpWebRequest." Well, that's security restriction of XmlHttpRequest. But wait, there is special hidden property of XmlHttpRequest, that makes it able to use for cross domain environment (not in IE, indeed). You are right, but, actually, this problem prevented  by Silverlight developers by putting special hidden small test constraint, so if uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) not equal HtmlPage.DocumentUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped), Silverlight will not even try to process your request. Bad news, but I heard, they going to enable it somehow. Anyway, not now. And until this type of calls are disabled, we'll you local calls. Let's do it.

 BrowserHttpWebRequest req = new BrowserHttpWebRequest(new Uri(@"http://localhost/MyLocalService/rss.aspx")); IAsyncResult res = req.BeginGetResponse(new AsyncCallback(ParseResponse), req); status.Text = "Acquiring feed information..."; 

 






Bad news - the same error. Why? You are using local calls. This not cross-domains, this is sweet localhost. Well, actually, X:\Documents\Visual Studio Codename Orcas\Projects\CrossDomainTest\CrossDomainTest\TestPage.html, the location of your silverlight project is not actually localhost. More, then this, that's even not http:// uri. What to do?



Here we'll touch the second item, mentioned in the title of this post and move a Silverlight application to work (and, of cause, being debugged) remotely (even with localhost)



For this propose we'll create new Empty Web Project (I do not like all this stuff added by smart templates for Websites, webservices etc., are you?). Now you have two projects - your old Silverlight and new glossy empty website with address like http://localhost:4885/MyGlossyWeb - exactly, what we need. Now we should put our Silverlight project into web one, but we should also be able to debug it without maintaining it twice. Hit right mouse button on your web project. You'll notice, that Visual Studio 2008 (formerly Orcas) beta with Silverlight Tools Alpha has great new menu item named "Link to Silverlight..." - great. Hit it. Put your web project as start one and press F5. Nothing, except error message? That's right, you have not start page for your web project. Silverlight stuff can not be start page, due to fact, that it is not page, so just copy TestPage.html together with Silverlight.js (else silverlight will ne run) into web project. Click TestPage.html and mark it as start page. Wallaby :), you can run your silverlight application from absolute web uri. It also debuggable. To check it put break point in your Silverlight project and run - the application stopped on it.



Now, when we have such cool stuff, that works on localhost, let's make it work cross domain. Add new Web Handler (just add new item and choose web handler type) to your web project. It'll generate new ashx file with two methods. Look into it - it give you all you need for rerouting all requests into external web address. Just a tip in order to make handler know about what you want it to do generate requests from silverlight following way: http://localhost/myhandler.ashx?url=http://externalurl/page.aspx&att1=something&att2=something&method=GET inside web handler translate all necessary information, you already passed into real request and return the external response. If you do not know, what I mean by this sentence, you probably should lean programming a bit :), 'cos web handler makes you able to perform cross domain requests and responses with regular dot net methods.



Are you still need Google Gears? You do not, for real. Danny, you too :)



Shorten steps:




  1. Create Silverlight project


  2. Create Empty Web project


  3. Link Silverlight project to the Web project


  4. Copy TestPage.html and Silverlight.js from Silverlight into Web project


  5. Make Web project to be default project and TestPage.html to be a default page


  6. Create new Web Handler page in Web project


  7. By using System.Net namespace forward all requests by parsing parameters of current request


  8. Return what you got from external domain (don't forget to provide content type


  9. Have a nice day with fluent debugging only one copy of original project and cross-domain calls in Silverlight



We done, folks. Have a nice programming day. A moony and filed by silver light day :)

8 comments:

Unknown said...

I can't link my Silverlight applicaiton to the new web site. The Link to Silverlight option does not display any of my Silverlight projects and there is no way to browse to one. Am I missing a step?

Tamir Khason said...

You should use VS2008 (Orcas) + Silverlight toolkit to have this option

Unknown said...

I am using VS2008. I have the SDK for Silverlight alpha but I don't know about any other "Silverlight toolkit". Where is it?

Tamir Khason said...

http://go.microsoft.com/fwlink/?LinkID=89149&clcid=0x409

Unknown said...

Thanks for your help but it still does not show any of my Silverlight projects in the selection box.

Is there another way to read an xml file that is local to the Silverlight control while using VS Orcas?

Unknown said...

Do the Silverlight projects have to be in a spcial directory in order to show up in the Add Link selection box? Mine are in the Projects directory by default.

Or is there some VS option that needs to be configured?

Thanks you.

Unknown said...

Success!

Here are the steps I was missing:
1. Need to place the Silverlight project in the solution of the empty website. Then it is possible to Add Silverlight link.

2. Use the localhost:xxxx in the url pathname.

With these changes, I was able to get the QuickStart rss reader example to work and I am sure that I can now continue with my application.

Thanks for the blog which set me on the right course.

Unknown said...

Tamir, could you please give me a code example how you would use the generic handler to get the requests from Silverlight. you put in the blog post that if a person did not know how to do this then they need to work on programming. well I need work, help me learn here. Thank you.