您的位置:首页 > 编程语言 > ASP

Asp.Net Ajax 学习笔记16 Profile Service扩展方式

2007-12-23 01:23 866 查看
这次的课程首先说明了Ajax技术滥用的危害。

Ajax是一个很好的技术但是象很多事情一样,过度使用就会有一定的危害。

Round-trip增多导致性能降低。Round-trip就是只提交到服务器和服务器回发的次数增多,这样,增大了服务器的响应次数,导致服务器负担增大。

交互性降低导致用户体验变差。主要的问题在于Ajax是无刷新的在处理页面,有的Ajax页面没有提供数据正在加载的提示,导致用户不清楚现在数据是否正在提交或者加载。

无端使用Ajax技术容易增加安全隐患。因为如果不给WebService加上授权访问,会给网站带来安全方面的问题

Ajax技术是天生的搜索杀手。使用了Ajax的页面可能会导致搜索引擎的搜索程序不能找到。

为了减轻服务器的负担,Ajax应用种应该使用预加载,就是说在页面第一次访问服务器的时候,就将应该回发的数据传送到客户端,用户操作使用Ajax调用的时候不是从服务段取得数据,而是从客户端。

Profile Service的预加载

通过ProfileService的LoadProperties属性,进行Profile的预加载。

客户端调用ProfileSerivce的实质就是Asp.Net Ajax调用包装了Profile的WebService.

那么我们可以通过两种手段来扩展Profile。

1、扩展Asp.Net的Profile

自定义一个Profile类CustomProfile,他继承于System.Web.Profile.ProfileProvider类,CustomProfile类,需要实现以下抽象方法

Initialize

ApplicationName

GetPropertyValues

SetPropertyValues

DeleteProfiles

DeleteInactiveProfiles

GetAllProfiles

GetAllInactiveProfiles

FindProfilesByUserName

FindInactiveProfileByUserName

GetNumberOfInactiveProfiles

然后需要在WebConfig文件里指定ProfileProvider由哪个类来提供。如下例


<profile enabled="true" automaticSaveEnabled="true" defaultProvider="CustomProvider">


<providers>


<clear />


<add name="CustomProvider"


type="CustomProvider"


connectionStringName="ProfileDatabase"


applicationName="ProfileSample"


description="Sample for ASP.NET profile and Profile Service" />


</providers>


<properties>


<add name="Name" type="System.String"/>


<add name="Email" type="System.String" />


<add name="Age" type="System.Int32" />


<group name="Address">


<add name="City" type="System.String" />


<add name="Street" type="System.String" />


<add name="PostalCode" type="System.String" />


</group>


</properties>


</profile>

当然这样继承ProfileProvider的方法,十分的繁琐,我们需要重写很多的基类。

那么Asp.Net Ajax访问Profile的时候,外部包装了一层WebService,我们可以重写这个类。

2、重写Ajax客户端访问Profile的WebService。


[WebService(Namespace = "http://tempuri.org/")]


[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]


[System.Web.Script.Services.ScriptService]


public class CustomProfileService : System.Web.Services.WebService




...{




[WebMethod]


public IDictionary<string, object> GetAllPropertiesForCurrentUser()




...{


return null;


}




[WebMethod]


public IDictionary<string, object> GetPropertiesForCurrentUser(string[] properties)




...{


return null;


}




[WebMethod]


public int SetPropertiesForCurrentUser(IDictionary<string, object> values)




...{


return 0;


}


}



GetAllPropertiesForCurrentUser方法:从当前用户得到所有的Properties,返回值是一个字典的集合,包含了由客户端传来的键值对应。

GetPropertiesForCurrentUser:从当前用户根据指定的Properties集合名字得到这些Properties字典已和。

SetPropertiesForCurrentUser:为当前的用户设置Properties集合。参数是一个关于这个用户的字典集合。

那么在实现这些方法之后,需要在ScriptManager里指定ProfileService的路径,告诉客户端调用这个Profile Web Service


<asp:ScriptManager ID="ScriptManager1" runat="server">


<ProfileService Path="CustomProfileService.asmx" />


</asp:ScriptManager>



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