asp.net 2.0 Profile 的一些注意事項(序列化)

來源:互聯網
上載者:User
近日開發培訓系統時,大量地使用到了 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.