动态更改Profile Provider连接字符串

时间:2012-01-04 14:39:47

标签: asp.net-membership

如果您阅读我以前的帖子,您就知道我可以使用自定义MembershipProvider和RoleProvider在每次调用时使用不同的数据源。我想和Profile一样。

我的个人资料不会存储在网络配置中,而是存储在这样的自定义类中:

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

同样对于Membership和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之间的区别在于我不能自己使用,因为“create”方法在ProfileBase中。有了ILSpy,我看到了一个单身人士,我想知道它是不是问题的根源。 问题是我只在initialize方法中传递一次。我不能再做一次更改数据源了。

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

1 个答案:

答案 0 :(得分:1)

我找到了一个 - 糟糕的 - 解决方案

在CustomProfileBase类中,我添加了一些代码来更改类的单例实例的连接字符串。

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

这不是最美丽的解决方案。

你觉得怎么样?