使用.Net RIA Service实现虚拟实验室的用户登陆过程
近年来,Google的扩展意图真的好猛啊,尤其是在传统的互联网方面。Google自身的GWT,加上ExtJs的支持,轻而易举地进入企业RIA市场。那些熟悉Java的开发人员,几乎不需要具备任何Javascript、AJAX的知识,只要调用GWT包,就能编写出华丽的RIA应用程序。反观Adobe的Flex,虽然起步早,但由于缺乏后台服务语言的支持,现今依旧停留在无限美好的YY中。微软在经历了Silverlight 1.0、2.0时代和Flex的缠斗后,终于意识到,它在RIA领域最大的敌人其实是Google,为此,及时地在3.0里增加了.Net RIA Service的功能。自此,Silverlight终于进入企业RIA的行业,不再沦为类似“Flex”般的玩具。
废话少说,让我们进入本节,利用RIA Service,实现虚拟实验室的用户登陆过程。
添加 Domain Service
首先安装RIA Service,目前最新版本为 March 09’Preview 。相信微软很快就会发布正式版本。
完成后,我们可以在Silverlight应用程序属性的Silverlight选项卡上看到如下下拉框,这里我选择项目中的Web应用程序:
首先,在该Web应用程序中添加一个Domain Service:
单击Add,在弹出的对话框中,选中Enable client access对话框:
打开刚添加的MembershipDomainService.cs文件,将基类修改为:AuthenticationBase<UserBase>
[EnableClientAccess()] public class MembershipService : AuthenticationBase<UserBase> { }
启用Asp.Net Membership API
在Web.app里添加数据库链接:
<connectionStrings> <clear/> <add name="InfoConnectionString" connectionString="Data Source=serverip;Initial Catalog=db;User ID=sa" providerName="System.Data.SqlClient"/> </connectionStrings>
添加Membship Provider节点:
<authentication mode="Forms"/> <membership defaultProvider="InfoMembershipProvider"> <providers> <clear/> <add name="InfoMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web" connectionStringName="InfoConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="true" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/> </providers> </membership>
打开Silverlight承载页面,用System.Web.Ria里的SilverlightApplication代替<asp:Silverlight />
<%@ Register Assembly="System.Web.Ria" Namespace="System.Web.Ria" TagPrefix="ria" %> <ria:SilverlightApplication ID="Silverlight1" runat="server" …… />
修改Silverlight应用程序
在Silverlight应用程序里,添加System.ComponentModel、System.ComponentModel.DataAnnotatons、System.Runtime.Serialization、System.Windows.Ria等引用:
编译整个工程后,可以看到编译器已经帮我们生成了MembershipDomainService的代理 :
这是编译器生成的Login方法 :
public void Login(string userName, string password, bool isPersistent) { Dictionary<string, object> parameters = new Dictionary<string, object>(); parameters.Add("userName", userName); parameters.Add("password", password); parameters.Add("isPersistent", isPersistent); base.InvokeServiceOperation("Login", typeof(UserBase), parameters, this.OnLoginCompleted, null); }
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:appsvc="clr-namespace:System.Windows.Ria.ApplicationServices;assembly=System.Windows.Ria" x:Class="Newinfosoft.Rhinoceros.VirtualLab.App"> <!--……--> <Application.Services> <appsvc:WebUserService x:Name="UserService" /> </Application.Services> </Application>
最后,完成登录代码,添加一个编译器生成的MembershipContext:
protected MembershipContext m_Membership = new MembershipContext();
修改Login方法:
public void Login() { LoginWindow lw = new LoginWindow(); lw.Closed += (s2, e2) => { if (lw.DialogResult == true) { m_Membership.LoginCompleted += OnLoginCompleted; m_Membership.Login(lw.UserName, lw.UserPassword,false); } }; lw.Show(); } protected void OnLoginCompleted(object sender, System.Windows.Ria.Data.InvokeEventArgs e) { if (e.ReturnValue != null) { //…… } }