2

我正在尝试编写一些使用 CustomMembershipProvider 创建/删除/更改用户的测试。CustomMembershipProvider 是 WebApp 程序集的一部分。

当我运行测试时,出现以下异常:

System.TypeLoadException : Could not load type 'WebApp.Framework.CustomMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
   at System.Web.Security.Membership.InitializeSettings(Boolean initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
   at System.Web.Security.Membership.Initialize()
   at System.Web.Security.Membership.get_Providers()
   at System.Web.Security.MembershipUser..ctor(String providerName, String name, Object providerUserKey, String email, String passwordQuestion, String comment, Boolean isApproved, Boolean isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
   at BusinessLayer.Users.UserRepository.Add(User user, ref MembershipCreateStatus status) in UserRepository.cs: line 43
   at WebApp.Framework.CustomMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, ref MembershipCreateStatus status) in CustomMembershipProvider.cs: line 86
   at Tests.Framework.CustomMembershipProviderTests.CreateUser() in CustomMembershipProviderTests.cs: line 22

当我执行这行代码时抛出异常:

        return new MembershipUser(
            "MyMembershipProvider",
            user.Name,
            null,
            user.EmailAddress,
            string.Empty,
            string.Empty,
            true,
            false,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue,
            DateTime.MinValue);

在我的测试项目中,将以下行添加到我的 app.conf 中:

成员定义:

  <system.web>
    <membership defaultProvider="MyMembershipProvider">
      <providers>
        <clear/>
        <add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider" connectionStringName="DefaultConnection"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>
  </system.web>

数据库连接:

  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=WS050129\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True;" providerName="System.Data.SqlClient" />
  </connectionStrings>

对我来说看起来不错,但显然有些不对劲:)。有人能指出我正确的方向吗?这里有点不知所措。

非常感谢。

4

1 回答 1

9

尝试改变这个:

<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider"       connectionStringName="DefaultConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />

...对此:

<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider, WebApp" connectionStringName="DefaultConnection"
         enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
         maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
         applicationName="/" />

You may need to tweak that a little. But it looks like the problem is that, lacking information about what assembly the type is in, the framework is looking in System.Web. Flesh out the type definition attribute on that tag.

于 2013-01-14T18:51:31.830 回答