动态更改 ProfileProvider 连接字符串

发布于 2024-12-24 23:11:44 字数 2188 浏览 1 评论 0原文

如果您阅读了我之前的文章,您就会知道我可以使用自定义 MembershipProvider 和 RoleProvider 在每次调用时使用不同的数据源。我想对个人资料做同样的事情。

我的个人资料的属性不存储在 Web 配置中,而是存储在这样的自定义类中:

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 之间的区别是我不能使用自身,因为“创建”方法位于 ProfileBase 中。通过 ILSpy,我看到了一个单例,我想知道这是否不是问题的根源。 问题是我只在初始化方法中传递了一次。我不能再更改数据源了。

我希望你能理解我蹩脚的英语并能帮助我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯狂的代价 2024-12-31 23:11:44

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

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

从它的工作来看,这并不是最漂亮的解决方案。

你觉得怎么样?

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文