CustomSqlMembership 间歇性地引发服务器错误
我有一个 asp.net MVC 应用程序部署到服务器,它使用表单身份验证并使用 CustomSqlMembership 提供程序基本上我没有更改 SqlMembershipPRovider 中的任何内容,只是从 MS 复制源并将源包含在我的项目中并不时重命名它出现这个错误。
这种情况发生在本地主机和远程部署的系统上,我无法弄清楚其原因是什么。
“/”应用程序中的服务器错误。 配置错误 描述:处理服务此请求所需的配置文件期间发生错误。请查看下面的具体错误详细信息并适当修改您的配置文件。
解析器错误消息:未将对象引用设置为对象的实例。
源错误:
Line 50: <clear/>
Line 51: <!--<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>-->
Line 52: `<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" connectionStringName="AcmeCorpConnectionString" enablePasswordRetrieval="False" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="True" passwordFormat="Hashed" maxInvalidPasswordAttempts="6" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />`
Line 53: </providers>
Line 54: </membership>
这是 web.config 中成员资格对象的完整列表
<membership defaultProvider="CustomSqlMembershipProvider">
<providers>
<clear />
<!--<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />-->
<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="False" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="True" passwordFormat="Hashed" maxInvalidPasswordAttempts="6" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
public class CustomSqlMembershipProvider : AcmeCorp.SqlMembershipProvider
{
static public ConnectionStringSettings css {get; set;}
public override void Initialize(string name, NameValueCollection config)
{
config.Add("connectionString", css.ConnectionString);
base.Initialize(name, config);
}
}
public class CustomSqlRoleProvider : AcmeCorp.SqlRoleProvider
{
static public ConnectionStringSettings css { get; set; }
public override string GetConnectionString()
{
return css.ConnectionString;
}
public override void Initialize(string name, NameValueCollection config)
{
//config.Add("connectionString", css.ConnectionString);
base.Initialize(name, config);
}
}
public interface ISiteProvider
{
bool Initialise(string host);
Site GetCurrentSite();
}
public class SiteProvider : ISiteProvider
{
SystemMetaDataContext mDB;
Site mSite;
public SiteProvider(SystemMetaDataContext db)
{
mDB = db;
}
public bool Initialise(string host)
{
mSite = mDB.Sites.SingleOrDefault(s => s.Host == host);
if (null != mSite)
{
CustomSqlMembershipProvider.css = new ConnectionStringSettings();
CustomSqlMembershipProvider.css.ConnectionString = mSite.Connection;
CustomSqlMembershipProvider.css.ProviderName = "System.Data.SqlClient";
CustomSqlMembershipProvider.css.Name = "dq_systemConnectionString";
CustomSqlMembershipProvider.css.ConnectionString = mSite.Connection;
CustomSqlRoleProvider.css = new ConnectionStringSettings();
CustomSqlRoleProvider.css.ConnectionString = mSite.Connection;
CustomSqlRoleProvider.css.ProviderName = "System.Data.SqlClient";
CustomSqlRoleProvider.css.Name = "dq_systemConnectionString";
CustomSqlRoleProvider.css.ConnectionString = mSite.Connection;
return true;
}
else
{
return false;
}
}
public Site GetCurrentSite()
{
return mSite;
}
}
public class BaseController : Controller
{
ISiteProvider mSiteProvider;
protected IRepository mRepository { get; private set; }
protected int DefaultPageSize { get; set; }
public BaseController()
{
DefaultPageSize = 10;
mSiteProvider = new SiteProvider(new SystemMetaDataContext());
}
public BaseController(IDQRepository repository)
{
mRepository = repository;
DefaultPageSize = 10;
if (Session["ActiveView"] == null)
{
IList<RoleViewModel> roles = mRepository.GetAllRoles();
foreach (RoleViewModel rvm in roles)
{
if (Roles.IsUserInRole(rvm.Name))
{
Session["ActiveView"] = rvm.Name;
break;
}
}
}
}
protected override void Initialize(RequestContext requestContext)
{
string[] host = requestContext.HttpContext.Request.Headers["Host"].Split(':');
MetaInfo.PopulateMeta(host[0]);
if (!mSiteProvider.Initialise(host[0]))
RedirectToRoute("Default");
if (null == mRepository)
mRepository = new DQRepository();
base.Initialize(requestContext);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewData["Site"] = Site;
base.OnActionExecuting(filterContext);
}
public Site Site {
get {
return mSiteProvider.GetCurrentSite();
}
}
I've a asp.net MVC app deployed to server it uses forms authentication and uses CustomSqlMembership provider basically I've not changed anything in the SqlMembershipPRovider just copied the source from MS and included the source in my project and renamed it from time to time this error comes up.
This happens on localhost and remotely deployed system and I can't figure out what could be the cause of it.
Server Error in '/' Application.
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Object reference not set to an instance of an object.
Source Error:
Line 50: <clear/>
Line 51: <!--<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>-->
Line 52: `<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" connectionStringName="AcmeCorpConnectionString" enablePasswordRetrieval="False" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="True" passwordFormat="Hashed" maxInvalidPasswordAttempts="6" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />`
Line 53: </providers>
Line 54: </membership>
this is the complete listing of membership object in web.config
<membership defaultProvider="CustomSqlMembershipProvider">
<providers>
<clear />
<!--<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />-->
<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" connectionStringName="dq_systemConnectionString" enablePasswordRetrieval="False" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="True" passwordFormat="Hashed" maxInvalidPasswordAttempts="6" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
public class CustomSqlMembershipProvider : AcmeCorp.SqlMembershipProvider
{
static public ConnectionStringSettings css {get; set;}
public override void Initialize(string name, NameValueCollection config)
{
config.Add("connectionString", css.ConnectionString);
base.Initialize(name, config);
}
}
public class CustomSqlRoleProvider : AcmeCorp.SqlRoleProvider
{
static public ConnectionStringSettings css { get; set; }
public override string GetConnectionString()
{
return css.ConnectionString;
}
public override void Initialize(string name, NameValueCollection config)
{
//config.Add("connectionString", css.ConnectionString);
base.Initialize(name, config);
}
}
public interface ISiteProvider
{
bool Initialise(string host);
Site GetCurrentSite();
}
public class SiteProvider : ISiteProvider
{
SystemMetaDataContext mDB;
Site mSite;
public SiteProvider(SystemMetaDataContext db)
{
mDB = db;
}
public bool Initialise(string host)
{
mSite = mDB.Sites.SingleOrDefault(s => s.Host == host);
if (null != mSite)
{
CustomSqlMembershipProvider.css = new ConnectionStringSettings();
CustomSqlMembershipProvider.css.ConnectionString = mSite.Connection;
CustomSqlMembershipProvider.css.ProviderName = "System.Data.SqlClient";
CustomSqlMembershipProvider.css.Name = "dq_systemConnectionString";
CustomSqlMembershipProvider.css.ConnectionString = mSite.Connection;
CustomSqlRoleProvider.css = new ConnectionStringSettings();
CustomSqlRoleProvider.css.ConnectionString = mSite.Connection;
CustomSqlRoleProvider.css.ProviderName = "System.Data.SqlClient";
CustomSqlRoleProvider.css.Name = "dq_systemConnectionString";
CustomSqlRoleProvider.css.ConnectionString = mSite.Connection;
return true;
}
else
{
return false;
}
}
public Site GetCurrentSite()
{
return mSite;
}
}
public class BaseController : Controller
{
ISiteProvider mSiteProvider;
protected IRepository mRepository { get; private set; }
protected int DefaultPageSize { get; set; }
public BaseController()
{
DefaultPageSize = 10;
mSiteProvider = new SiteProvider(new SystemMetaDataContext());
}
public BaseController(IDQRepository repository)
{
mRepository = repository;
DefaultPageSize = 10;
if (Session["ActiveView"] == null)
{
IList<RoleViewModel> roles = mRepository.GetAllRoles();
foreach (RoleViewModel rvm in roles)
{
if (Roles.IsUserInRole(rvm.Name))
{
Session["ActiveView"] = rvm.Name;
break;
}
}
}
}
protected override void Initialize(RequestContext requestContext)
{
string[] host = requestContext.HttpContext.Request.Headers["Host"].Split(':');
MetaInfo.PopulateMeta(host[0]);
if (!mSiteProvider.Initialise(host[0]))
RedirectToRoute("Default");
if (null == mRepository)
mRepository = new DQRepository();
base.Initialize(requestContext);
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewData["Site"] = Site;
base.OnActionExecuting(filterContext);
}
public Site Site {
get {
return mSiteProvider.GetCurrentSite();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个静态变量在哪里初始化的?
静态变量不是线程安全的。
您必须以线程安全的方式初始化它们,特别是在网络场模式下运行时。
如果服务器使用的应用程序池在其配置中设置了超过 1 个处理器,则它在 Web Farm 模式下运行。在 Web Farm 模式下,您将运行 2 个 Web 应用程序,但您的静态变量只会创建一次,因为该内存空间是共享的(实际上还有更多,但您可以写有关此主题的书籍)。
您可以通过将应用程序池中的处理器计数设置为 1 来禁用 Web Farm 模式。
初始化这个 css 变量的代码在哪里?
可以写在这里吗?
从哪里称呼它?
Where is initialized this static variable ?
Static variables are not thread safe.
You must initialize them in a thread safe way, especially if running in web farm mode.
If the server uses an application pool with more than 1 processor set in its config, it is running in web farm mode. In web farm mode, you will have 2 web applications running, but your static variable will be created only once as this memory space is shared (in fact there is more than that but you can write books on this subject).
You can disable web farm mode by setting the processor count to 1 in the application pool.
Where is the code initializing this css variable ?
Could you write it here ?
From where is it called ?
检查您的自定义代码。最有可能的是,您尝试访问属性而不检查对象的可为空性。这可能是由于在会员未找到相关人员时询问用户名或其他原因。在代码中添加空引用检查至少可以消除错误,这样您就可以引发适当的异常(然后向用户提供友好的错误消息)。
Check your custom code. Most likely, you are attempting to access a property without checking nullability of the object. This is probably due to asking for user name, or something, when membership has not found the person in question. Adding a null ref check to the code will at least rid the error so you can raise an appropriate exception (and then give the user a friendly error message).
您对该提供程序的配置确实有许多冗余字段。如果更改
gt; 会发生什么情况
到
?Your configuration for this provider does have many redundant fields. What happens if you change
<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" ... />
to<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider"/>
?