2012-01-04 87 views
1

如果您阅读了我以前的文章,您可以使用自定义MembershipProvider和RoleProvider在每次通话中使用不同的数据源。我想和Profile一样。动态更改ProfileProvider连接字符串

我的个人资料propteries在网络配置,但在自定义类这样不存储:

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我有这样一个类:

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); 
     } 
    }   
} 

我的CustomProfileProvider之间的差异是我不能使用自己,因为“创建”方法在ProfileBase中。和ILSpy一样,我看到了一个单例,我不知道它是不是问题的根源。 问题是我只在初始化方法中传递一次。我不能再花时间改变数据源。

我希望你能理解我可怜的英语,能帮助我。

+0

看到这个问题/答案︰http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz 2012-04-13 01:18:39

回答

1

我找到了 - 坏 - 解

在CustomProfileBase I类添加一些代码来改变类的单一实例的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 2012-04-12 00:42:25

+0

问题已在这里得到解答:http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – fuzz 2012-04-13 01:18:29