自定义ASP.NET SqlMembershipProvider-处理连接字符串 [英] Custom ASP.NET SqlMembershipProvider - handling connection string
问题描述
我正在创建一个自定义SqlMembershipProvider类,以向基类添加一些增强的功能.不过,我正在处理连接字符串.如何从配置中读取连接字符串名称,并将其提供给其余方法使用?
I am creating a custom SqlMembershipProvider class to add some enhanced functionality to the base class. I'm getting caught up on handling the connection string, though. How can I read the Connection String Name from the configuration and make that available to the rest of the methods?
现在我有:
public override void Initialize(string name, NameValueCollection config)
{
base.Initialize(name, config);
_ConnectionStringName = config["connectionStringName"];
}
但是在其他方法中,_ConnectionStringName变量为null:
But in other methods, the _ConnectionStringName variable is null:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[_ConnectionStringName].ConnectionString)
什么是正确的存储连接字符串名称的方法,以便它在我的自定义成员资格提供程序中全局可用?
What is the proper way to store the Connection String Name so it is available globally in my custom membership provider?
谢谢!
推荐答案
ProviderBase
如果config集合中有剩余条目,则将抛出ConfigurationException
,因此每个提供程序都将其删除之前的配置条目调用base.Initialize
.
ProviderBase
will throw a ConfigurationException
if there are any entries left in the config collection by the time it get's it so each provider removes it's configuration entries before calling base.Initialize
.
The issue, as you have found as a result of this answer is that you must get your values before calling base.Initialize
.
对不起,我乍看之下没想到.
Sorry, I missed that at first glance.
这篇文章的其余部分是历史性的,尽管技术上正确,但仍漏掉了上面列举的重要问题.
首先-尝试WebConfigurationManager.ConnectionStrings
.
WebConfigurationManager
一直处理从Windows \ microsoft.net \ framework \ 2.0xxxx \ web.config一直应用到应用程序的web.config层次结构.
WebConfigurationManager
handles applying the hierarchy of web.config all the way from your windows\microsoft.net\framework\2.0xxxx\web.config all the way up to your app.
ConfigurationManager
中不存在此行为,通常会从machine.config到app.config.
This behaviour is not present in ConfigurationManager
, which typically deals with machine.config to app.config.
如果这不能解决您的问题,并且确实在Initialize
中正确分配了_ConnectionStringName
,则必须覆盖代码中其他位置的值.
If this does not solve your problem you must be overwriting the value elsewhere in your code, if indeed _ConnectionStringName
is being properly assigned in Initialize
.
首先,设置一个断点,并确保按预期设置了_ConnectionStringName
.
First, set a breakpoint and ensure that _ConnectionStringName
is being set as expected.
然后找到对该字段的所有引用,并确保您没有错误.
Then locate all references to the field and ensure that you do not have a bug.
当然,这假定_ConnectionStringName
是私有字段.如果不是,请这样做,然后查找您的编译错误.
This is assuming, of course, that _ConnectionStringName
is a private field. If it is not, make it so and look for your compile error.
这篇关于自定义ASP.NET SqlMembershipProvider-处理连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!