基于,Silverlight,的,RIA,架构,及百度应用,杨丹,资深.NET架构师,微软中国,陈广琛,Web,前端工程师,百度,议题,RIA与应用平台趋势,Silverlight 应用架构,界面模型,逻辑分层,网络访问,平安机制,百度Silverlight应用,RIA,与应用平台趋势,纯,Web,在,Web,上实现,通过,Web,部署,Web,Desktop,RIA,HTML,Richness,Reach,DHTML,AJAX,Silverlight,Win Form,WPF,Plug-in,界面更美观,像桌面应用,动画 多媒体,开发更高效,面向对象语言,美工技术协同,Silverlight,架构,纯,Web,界面更美观,开发更高效,Silverlight Runtime,Presentation,Core,Core CLR,Small BCL,Garbage Collector,Security,Exception,Loader,Debugging,XAML,Media,DRM,Silverlight,应用架构,Browser,Silverlight,Silverlight,Web Server,B/L,B/L,B/L,逻辑分层,平安机制,网络访问,界面模型,界面模型,如何与,HTML,页面结合?,三种选择,RIA,与,HTML,RIA,与,RIA,单体,RIA,模块粒度,松散耦合,本地通讯,MVC/MVP,RIA,与,RIA,本地访问,Silverlight,与,HTML,对象:,JavaScript,接口,Silverlight,应用之间:本地消息,Domain 1,本地消息接受者,Local Message Receiver,本地消息发送者,Local Message Sender,Domain 2,本地消息接受者,Local Message Receiver,本地消息发送者,Local Message Sender,单体,RIA,按需加载,Silverlight,应用,应用,Package(.xap),In-Package,文件,应用程序集,(.dll),Application Class,Application Class,资源文件,Library,程序集,Application Class,Application Class,资源文件,ExternalPart,程序集,Library,程序集,Application Class,Application Class,资源文件,On-Demand,程序集,Library,程序集,Application Class,Application Class,资源文件,Entry Point,缓存,延后,界面模型 控制流转,面向页面,vs.,面向,GUI,页面,GUI,Silverlight,导航:相对于,Application,的状态,Navigation Framework,:,Frame,Page,逻辑分层,多层架构,VS.C/S,架构,Silverlight,应用定位,逻辑写在何处?,与,ASP.NET MVC,的关系?,简单展现逻辑,包含较多逻辑,安全性,暴露展现数据,暴露业务逻辑和数据,耦合性,服务可以复用,逻辑在客户端,灵活性,简单展现,前台灵活,业务类型,业务处理在后台,业务处理在前台,逻辑分层,与,ASP.NET MVC,结合,仅传递用于显示的对象,以,REST,调用为主,与,ASP.NET MVC,结合,public class CategoryController:,Controller,public ActionResult Products(int id),return Json(prods);,/return View();,ASP.NET MVC,业务逻辑层,SOAP,REST,网络访问,Web Service,访问方式不同,资源,/REST,vs.,调用,/RPC,REST,具象状态传输,URI,资源的状态,Http,标准操作,Get/Post,RPC,简单对象访问协议,SOAP,方法调用,复杂语义,Http-Based,Web Service,SOAP,REST,网络访问,RPC/,SOAP,方式,效劳端处理,WCF 支持 SOAP,客户端调用 Proxy,使用Visual Studio菜单工具,Add Service Reference,命令行工具,SLsvcutil.exe,Silverlight 版本的 svcutil.exe,基于Channel Model开发,最灵活的方式,网络访问,REST,方式,效劳端处理,WCF 支持 REST,OperationContract,WebGet(UriTemplate=Northwind/Order/orderId),OrderInfoMsg GetOrderInfo(string orderId);,客户端调用,WebClient,数据处理,XML:XmlReader,Linq to XML,XmlSerializer,JSON:Linq toJSON,DataContractJsonSerializer,RSS/Atom Feeds:,网络访问 REST 与 SOAP 比较,REST,SOAP,互操作性,更优,是,伸缩性,容易,是,复杂度,简单,较复杂,协议,仅,HTTP,多种协议,安全性,传输层加密,消息层加密,事务,不支持,支持,处理逻辑,面向资源,面向调用,平安机制,认证,(Authentication),授权,(Authorization),身份传递,(Credential),消息加密,(Encryption),平安机制 身份传递,身份信息如何传递给后台效劳?,Browser-Based(自动),Windows Authentication,ASP.NET Form Authentication/Cookies,Message-Based(手工),URL 参数,消息头包含Username/Password 或Token,Browser-Based Authentication,Example with Cookies+Forms Auth,Browser,E.g.:ASP.NET login,User:Password:,YourDomain,Credentials,Auth info(cookie),Service calls+Auth info,Browser-Based Authentication,Login through Silverlight,User:Password:,YourDomain,Call with credentials toASP.NET Auth Service,Reply contains cookie,Service calls+Auth info,ASP.NET Auth Service,Browser,Browser-Based Authentication,Using Windows Authentication,Windows login,User:Password:,YourDomain,Service calls+Creds,Browser,MyBank LoginUser:Password:,MyBank,Credentials,Auth info(e.g.cookie),恶意请求,+,Auth info,恶意网站,恶意程序,Could steal orchange dataif protection wasnt in place,Browser-Based Authentication:,Cross-Domain Threat,Message-Based Authentication,Identity managed by Silverlight,not the Browser,User:Password:,YourDomain,Creds are added by Silverlight,not browser,No creds,Browser,恶意网站,Message-Based Authentication:,选择 1:修改接口,OperationContract,public decimal GetActBal(int actID,string user,string pwd);,选择 2:通过WCF 的WS-Security 在SOAP包头中自动插入身份信息,平安机制 认证和授权,效劳端如何认证和授权?,Message-Based 身份,if(!OperationContext.Current.ServiceSecurityContext.,PrimaryIdentity.IsAuthenticated),throw new SecurityException();,/标准 WCF方法,Browser-Based 身份,if(!.Identity.IsAuthenticated),throw new SecurityException();,/ASP.NET Membership,平安机制 消息加密,传输层加密,消息层加密,点到点安全,从,Brower,到,Server,端到端安全,从,App,到,App,对整个消息加密,可以对消息的局部加密,支持单一传输协议,例如,HTTP,支持混合传输协议,例如,HTTP+TCP,常用实现:,HTTPS,SSL,常用实现:,SOAP,WS-Security,完整,真实,防篡改,两种选择:传输层加密,消息层加密,Silverlight 应用架构,界面模型,逻辑分层,网络访问,平安机制,百度应用,Silverlight,版百度,Hi,界面模型,选择单体RIA,源自Web版的风格,Silverlight 3.0暂时缺乏多窗口模型,单体RIA的问题,耦合度高,难以维护来自Web版的经验,利用MVC别离来解决,Code-Behind,ASPX/XAML,(View),CS/VB,(Code-Behind),从,Win/Web Forms,到,MVC,private void,Button_OnClick(),从,Win/Web Forms,到,MVC,MVC,ASPX,(View),CS/VB,(Controller),public ActionResult,Search(),ViewData=;,return View();,ViewData,Silverlight MVC,实践,Binding,XAML,(View),CS/VB,(Controller),private void,Button_Click(),Counter.Value+;,Binding,Silverlight MVC,实践,Builder,XAML,(View),CS/VB,(Controller),private void,Button_Click(),Counter.Value+;,builder.Update();,Builder,逻辑分层,统一调用百度,IM API,隐藏后端逻辑,百度,IM API,Silverlight,版,百度,Hi,网页版百度,Hi,第三方客户端,方案支持,异步模型,调用单个异步函数,RetrieveUserAsync(username).AddCallback(user =ProcessUser(user);,多个异步函数组成工作流,Async.Chain().Next(context=FirstStep(context).Next(context=SecondStep(context).Next(context=ThirdStep(context).Go(initialContext);,网络访问,JSON-RPC,JSON,易于被不同的客户端解释,JavaScript,Silverlight,IM,接口更像是调用而非资源,平安机制,百度Passport,API式认