CustomSqlMembership间歇性地抛出服务器错误

时间:2011-10-10 16:28:52

标签: asp.net asp.net-mvc sqlmembershipprovider

我有一个asp.net MVC应用程序部署到服务器它使用表单身份验证并使用CustomSqlMembership提供程序基本上我没有改变SqlMembershipPRovider中的任何东西只是从MS复制源并包含我的项目中的源并重命名为这个错误不时出现。

这发生在localhost和远程部署的系统上,我无法弄清楚它可能是什么原因。

'/'应用程序中的服务器错误。 配置错误 描述:处理为此请求提供服务所需的配置文件时发生错误。请查看下面的具体错误详细信息并适当修改配置文件。

分析器错误消息:对象引用未设置为对象的实例。

来源错误:

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();  
        }  
     }  

3 个答案:

答案 0 :(得分:0)

检查您的自定义代码。最有可能的是,您试图在不检查对象的可空性的情况下访问属性。这可能是由于在成员资格未找到相关人员时询问用户名或其他内容。在代码中添加一个空的ref检查将至少消除错误,这样你就可以引发一个适当的异常(然后给用户一个友好的错误信息)。

答案 1 :(得分:0)

此提供程序的配置确实包含许多冗余字段。如果您将<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider, AcmeCorp, Version=1.0.0.0, Culture=neutral" ... /> 更改为<add name="CustomSqlMembershipProvider" type="AcmeCorp.CustomSqlMembershipProvider"/>会怎样?

答案 2 :(得分:0)

在哪里初始化这个静态变量?

 static public ConnectionStringSettings css {get; set;}

静态变量不是线程安全的。 您必须以线程安全的方式初始化它们,尤其是在Web场模式下运行时。

如果服务器使用其配置中设置了多个处理器的应用程序池,则它将以Web场模式运行。在Web场模式下,您将运行2个Web应用程序,但是您的静态变量将仅在共享此内存空间时创建一次(事实上,除此之外,您还可以编写有关此主题的书籍)。

您可以通过在应用程序池中将处理器计数设置为1来禁用Web场模式。

初始化此css变量的代码在哪里? 你能在这里写吗? 它在哪里被称为?