1

If you read my previous post, you know I'm able to use a custom MembershipProvider and RoleProvider to use different datasource on each call on the fly. I want to to the same thing with Profile.

My profile's propteries are not store in the web config but in a custom class like this :

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

also like for the Membership and RoleProvider I have a class like this :

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

the difference betwen my CustomProfileProvider is that I can't use itself because the "create" method is in the ProfileBase. And with ILSpy I have seen a singleton and I wonder if it's not the source of the problem. The issue is that I only pass one time in the initialize method. I can't do another time to change the datasource.

I hope you can understand my poor english and can help me.

seb49
  • 107
  • 7
  • See this question/answer: http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – gotnull Apr 13 '12 at 01:18

1 Answers1

1

I find a - bad - solution

In the CustomProfileBase class I add some code to change the connectionstring of the singleton instance of the class.

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

It's not the most beautifull solution by it's work.

What do you think of t ?

seb49
  • 107
  • 7
  • This doesn't seem to work. Did you ever find a better solution to fix this problem? – gotnull Apr 12 '12 at 00:42
  • Question has been answered here: http://stackoverflow.com/questions/10116626/modifying-profilebase-connectionstring-dynamically#comment12964962_10116626 – gotnull Apr 13 '12 at 01:18