近日開發培訓系統時,大量地使用到了 asp.net 2.0 的 profile 這個好用東西
設定檔是這樣寫
<profile defaultProvider="MyProfileProvider" automaticSaveEnabled="false">
<!--定義配置(profile)-->
<providers>
<add name="MyProfileProvider" type="System.Web.Profile.SqlProfileProvider" applicationName="CX" connectionStringName="CX.Services.CnString"/>
</providers>
<properties>
<add name="UserSelectorGlobalVar" type="Profiles.UserSelectorGlobalVar" allowAnonymous="false"></add>
<add name="UserExamDataSource" type="Profiles.UserExamDataSource" allowAnonymous="false"></add>
<add name="ExamSelectedTimu" type="Profiles.ExamSelectedTimu" allowAnonymous="false"></add>
</properties>
</profile>
其中有一個類的代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace Profiles
...{
public class UserSelectorGlobalVar
...{
public UserSelectorGlobalVar()
...{
}
private static System.Collections.Generic.Dictionary<Guid, Models.UserInfo> _SelectedUsers
= new System.Collections.Generic.Dictionary<Guid, Models.UserInfo>();
public Dictionary<Guid, Models.UserInfo> SelectedUsers
...{
get ...{ return _SelectedUsers; }
set ...{ _SelectedUsers = value; }
}
}
}
呵呵,但是當調用this.profile.Save () 的時候說這個類出錯,我想是沒有將這個類標誌為可以序列化的,於是加上屬性
[Serializable]
但是問題依舊.
後來想起以前做的一個實驗中將一個IList<XXX> 序列化後存入一個檔案,選用SOAP那種方式就出錯,好像是不支援吧,改用Binary 那種方式就好了. 但是profile 的序列化方式在哪裡設定呢
後來想一下因該是在Web.Config 裡面改的,藉助VS2005 的代碼提示功能發現
<add name="ExamSelectedTimu" type="Profiles.ExamSelectedTimu" allowAnonymous="false"></add>
這一句有一個屬性:serializeAs 字面意思就能猜到啦,於是改為這樣:
<add name="ExamSelectedTimu" type="Profiles.ExamSelectedTimu" serializeAs="Binary" allowAnonymous="false"></add>
Debug.... OK