动态更改 ProfileProvider 连接字符串
如果您阅读了我之前的文章,您就会知道我可以使用自定义 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个糟糕的解决方案
在 CustomProfileBase 类中,我添加了一些代码来更改该类的单例实例的连接字符串。
从它的工作来看,这并不是最漂亮的解决方案。
你觉得怎么样?
I find a - bad - solution
In the CustomProfileBase class I add some code to change the connectionstring of the singleton instance of the class.
It's not the most beautifull solution by it's work.
What do you think of t ?