您的位置:首页 > 移动开发

Cross-domain calls and server side debugging of Silverlight application

2011-12-15 09:20 441 查看

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:

Create Silverlight project
Create Empty Web project
Link Silverlight project to the Web project
Copy TestPage.html and Silverlight.js from Silverlight into Web project
Make Web project to be default project and TestPage.html to be a default page
Create new Web Handler page in Web project
By using System.Net namespace forward all requests by parsing parameters of current request
Return what you got from external domain (don’t forget to provide content type
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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: