Search code examples
c#sqlprofileprovider

Extended SqlProfileProvider, how do I call my custom method?


I'm using MySqlProfileProvider which extends the SqlProfileProvider class. In my class I am overriding Initialize in order to change the connection string of the base. This all works absolutely fine.

My problem is that I need to make a call after the profile provider has been initialized to change the connection string (when it's initializing I don't have the required information to create the correct connection string), and I can't seem to access the method.

It works in my custom SqlMembershipProvider class, in order to call the function I call

((MySqlMembershipProvider)Membership.Providers).ChangeConnectionString(sRequiredData);

The class:

public class MySqlMembershipProvider : SqlMembershipProvider
{
    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) {...}

    public virtual void ChangeConnectionString(string sRequiredData)
    {
        // get db name 
        string sDATABASE_NAME = ManageDBs.GetCompanyDBName(sRequiredData);

        // Set private property of Membership provider. 
        FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);

        string connectionString = connectionStringField.GetValue(this).ToString();

        if (!connectionString.Contains(sDATABASE_NAME))
        {
            connectionString = connectionString.Replace("DATABASE_NAME", sDATABASE_NAME);

            connectionStringField.SetValue(this, connectionString);
        }
    }
}

I have the same method in my profile provider but I cannot find how to call it. I've tried accessing it in ProfileBase, ProfileProvider and Profile with no luck and there doesn't seem to be anyone on google with the solution.

EDIT

I'm looking for something that looks akin to

((MySqlProfileProvider)Profile.Provider).ChangeConnectionString(sRequiredData);

but unfortunately you can't access Provider through profile.


Solution

  • I found the solution by trial and error:

    ((MySqlProfileProvider)ProfileBase.Properties["ANY_PROFILE_PROPERTY"].Provider).ChangeConnectionString(sRequiredData);