0

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();  
        }  
     }  
AppDeveloper
  • 927
  • 11
  • 21
  • You said you basically haven't changed anything in provider code. Why do you need custom provider then? Error you're experiencing most probably comes from incorrectly referencing your custom provider implementation class from `Web.Config` – Sergey Kudriavtsev Oct 10 '11 at 16:33
  • I wanted to have dynamic connection strings and couldn't do that without a custom membership provider. as this is a multi tenant SAAS app and depending on host name I get the connection string from other db and set the connection string for this db. – AppDeveloper Oct 11 '11 at 02:24
  • also I'm running on a shared host but that is a moot point since I've seen this happen on my localhost asp.net dev server as well. – AppDeveloper Oct 11 '11 at 02:30

3 Answers3

0

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).

Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32
  • if it is a null pointer exception why would it show up us configuration error anyway to trace this? – AppDeveloper Oct 11 '11 at 02:29
  • Give your stack trace, but i guess that the provider is dynamically created by ASP.NET when the web.config file is parsed. "css" is not initialized there so it throws this exception. So this is "like" a configuration error. – Softlion Nov 17 '11 at 13:55
  • @Softion there is not stack trace (how do I get one?) it only shows up as a configuration error, if "css" is causing issues then why does it happen only randomly? – AppDeveloper Nov 21 '11 at 17:38
  • On the server, if web farm mode is activated for the application pool, the static initialization code will run once per pool instance. This can cause random errors. Random errors also happen if your provider is not correctly thread safe. – Softlion Nov 22 '11 at 22:12
0

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"/> ?

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • I can definitely try that, but I wouldn't know if that would fix the issue, since the issue is intermittent and I can't always reproduce it so any fix would be just waiting until it happens next time unless we know what is truly causing this – AppDeveloper Oct 12 '11 at 00:27
0

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 ?

Softlion
  • 12,281
  • 11
  • 58
  • 88
  • hi, added SiteProvider and BaseController all Controllers are derived by BaseController base controller is basically determining which connection string to use, that connection string comes from a separate database, which is "Meta" and whose connection string is hard coded in web.config. I've seen the error on both shared hosting and on development server so not sure if web farm mode has relevance to it. – AppDeveloper Nov 26 '11 at 16:41
  • This configuration error happens on my local development server (Casini) as well so I don't think it has anything to do with server farm but I'm not sure. – AppDeveloper Nov 28 '11 at 18:48
  • What custom code ? If it does happen on Cassini then you should post a full demo project as this seems to be more complex than described. – Softlion Dec 07 '11 at 12:31