Cross-domain calls and server side debugging of Silverlight application

本文探讨了Silverlight开发中遇到的跨域远程调用问题及服务器端开发与调试方法,介绍了如何进行本地调用、跨域调用以及将Silverlight应用远程运行和调试的技术细节。

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 :)


混合动力汽车(HEV)模型的Simscape模型(Matlab代码、Simulink仿真实现)内容概要:本文档介绍了一个混合动力汽车(HEV)的Simscape模型,该模型通过Matlab代码和Simulink仿真工具实现,旨在对混合动力汽车的动力系统进行建模与仿真分析。模型涵盖了发动机、电机、电池、传动系统等关键部件,能够模拟车辆在不同工况下的能量流动与控制策略,适用于动力系统设计、能耗优化及控制算法验证等研究方向。文档还提及该资源属于一个涵盖多个科研领域的MATLAB仿真资源包,涉及电力系统、机器学习、路径规划、信号处理等多个技术方向,配套提供网盘下载链接,便于用户获取完整资源。; 适合人群:具备Matlab/Simulink使用基础的高校研究生、科研人员及从事新能源汽车系统仿真的工程技术人员。; 使用场景及目标:①开展混合动力汽车能量管理策略的研究与仿真验证;②学习基于Simscape的物理系统建模方法;③作为教学案例用于车辆工程或自动化相关课程的实践环节;④与其他优化算法(如智能优化、强化学习)结合,实现控制策略的优化设计。; 阅读建议:建议使用者先熟悉Matlab/Simulink及Simscape基础操作,结合文档中的模型结构逐步理解各模块功能,可在此基础上修改参数或替换控制算法以满足具体研究需求,同时推荐访问提供的网盘链接获取完整代码与示例文件以便深入学习与调试。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值