CustomSqlMembership throws server error intermittently
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" minRequiredNonalphanume开发者_运维问答ricCharacters="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();
}
}
Where is initialized this static variable ?
static public ConnectionStringSettings css {get; set;}
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).
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"/>
?
精彩评论