2012-01-04 12 views
1

私の以前の投稿を読んでいれば、カスタムのMembershipProviderとRoleProviderを使用して、各コールで異なるデータソースをオンザフライで使用できることがわかりました。私はプロフィールで同じことをしたい。 ProfileProvider connectionstringをオンザフライで変更する

ウェブの設定ではなく、このようなカスタムクラスに格納されていない私のプロフィールのpropteries

public class MyProfileProvider : SqlProfileProvider 
{ 
    public MyProfileProvider() 
    { 
    } 

    public MyProfileProvider(string SubDomainInstanceName) 
    { 
     string configPath = "~/web.config"; 
     Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); 
     ProfileSection section = (ProfileSection)config.GetSection("system.web/profile"); 
     ProviderSettingsCollection settings = section.Providers; 
     NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters; 

     Initialize(section.DefaultProvider, membershipParams); 
    } 


    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) 
    { 

     base.Initialize(name, config); 

     if (!string.IsNullOrEmpty(instance)) 
     { 
      // Update the private connection string field in the base class. 
      string connectionString = "";//my connection 
      // Set private property of Membership provider. 
      FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic); 
      connectionStringField.SetValue(this, connectionString); 
     } 
    }   
} 

public class AccountProfile : ProfileBase 
{ 

    public override SettingsProviderCollection Providers 
    { 
     get 
     { 
      return base.Providers; 
     } 
    } 

    static public AccountProfile GetUserProfile(string userName) 
    { 
     return (AccountProfile)(ProfileBase.Create(userName)); 
    } 

    [SettingsAllowAnonymous(false)] 
    public string MobilePhone 
    { 
     get { return ((string)(base["MobilePhone"])); } 
     set { base["MobilePhone"] = value; Save(); } 
    } 
} 

も会員とRoleProviderのために好きな私は、このようなクラスを持っています私のCustomProfileProviderとの違いは、「作成」メソッドがProfileBaseにあるため、自分自身を使用できないことです。そしてILSpyではシングルトンを見たことがあり、それが問題の原因ではないかと思います。 問題は、initializeメソッドで1回だけ渡すことです。私はデータソースを変更する別の時間を行うことができません。

あなたは私の貧しい英語を理解して助けてくれることを願っています。

+0

この質問/回答を見る:http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz

答えて

1

私が見つける - 悪い - ソリューション

をCustomProfileBaseクラスでは、私はクラスのシングルトンインスタンスのたconnectionStringを変更するには、いくつかのコードを追加します。

public class AccountProfile : ProfileBase 
{ 
     string connectionString = myconnstring; 

     //1st call not really use but allow to get an instance of my custom AccountProfile 
     AccountProfile test = (AccountProfile)(ProfileBase.Create(userName));     

     //change connectionstring oh the instance 
     FieldInfo connectionStringField = test.Providers["AspNetSqlProfileProvider"].GetType().BaseType.GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic); 
     connectionStringField.SetValue(test.Providers["AspNetSqlProfileProvider"], connectionString); 

     //new call on the good datasource 
     return (AccountProfile)AccountProfile.Create(userName); 
} 

これは、その作業によって最も美しい解決策ではありません。

あなたはtについてどう思いますか?

+0

これは動作していないようです。この問題を解決するためのより良い解決策を見つけたことはありますか? – fuzz

+0

質問はここで答えられました:http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz

関連する問題