SilverLight 一日两次碰壁
SilverLight1.1 中,使用 SoaphttpClientProtocol 调用WS有个限制,就是Cross domain calls are not supported by BrowserHttpWebRequest,这是 SilverLight 的安全机制的一部分,证据如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
但对于需要调用其他Site 的WS的需求似乎并不过份,那我们就想其他办法绕过去吧。
我们知道在JavaScript中调用WS是没有这限制的(但还是有验证的),通过JavaScript 实际是JavaScript +XMLhttpRequest +JSon+WS+ JavaScriptSerializer+Silveright的技术组合,我有点怕,但应该能实现。
HttpHandler 也许是个不错的主意,在服务端做重新定向,起码应该能解决调用自己Web site 的WS 的问题 (就是在自己WebSite上做一个HttpHandler ,用来代理WebserService 的调用)。马上试验,又发现一个限制,是HttpHandler的限制,其实也是ASP.net 的限制,就是服务端的重定向方法 Server.Execute 和Server.Transfer 方法只支持重定向到 ASP.net page。明证在 HttpServerUtility 类中
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
文档里也有说明The page transferred to should be another .aspx page. For instance, a transfer to an .asp or .asmx page is not valid.
咣当倒地。
但改进第二种方式, HttpHandler + JavaScriptSerializer + WS应该是可行,而且效率肯定能比第一种高。本人浅薄第认为,只要能不涉及JavaScript就远离它。明天再试。