5

我收到以下错误,并且仅当多个用户点击同一个按钮时才会出现此错误。任何帮助/想法将不胜感激:

System.InvalidOperationException:集合已修改;枚举操作可能无法执行。生成时间:格林威治标准时间 2015 年 6 月 10 日星期三 07:29:06

AutoMapper.AutoMapperMappingException:

映射类型:用户 -> 用户 ApplicationSecurityManager.Service.User -> ApplicationSecurityManager.Models.User

目标路径:用户

源值:ApplicationSecurityManager.Service.User ---> System.InvalidOperationException:集合已修改;枚举操作可能无法执行。在 System.Collections.Generic.List 1.Enumerator.MoveNextRare() at AutoMapper.TypeMap.<get_AfterMap>b__1(Object src, Object dest) at AutoMapper.Mappers.TypeMapObjectMapperRegistry.PropertyMapMappingStrategy.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) --- End of inner exception stack trace --- at AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) at AutoMapper.MappingEngine.Map[TDestination](Object source, Action1 opts) 在 ApplicationSecurityManager.UserManager.LoadUser(String username) 在 ApplicationSecurityManager.UserManager.get_AuthenticatedUser() 在 ApplicationSecurityManager.UserManager.IsAuthenticated() 在 ApplicationSecurityManager.Infrastructure.ApplicationSecurityAttribute.OnAuthorization(AuthorizationContext filterContext) 在System.Web.Mvc.ControllerActionInvoker.InvokeAuthorizationFilters(ControllerContext 控制器上下文,IList1 filters, ActionDescriptor actionDescriptor) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__1e(AsyncCallback asyncCallback, Object asyncState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.在 System.Web.Mvc.Async.AsyncResultWrapper.Begin[TResult] 开始(AsyncCallback 回调,对象状态,Int32 超时)(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate 1 endDelegate, Object tag, Int32 timeout) at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TResult](AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate1 endDelegate,对象标记)在 System.Web。 Mvc.Controller.<>c__DisplayClass1d.b__17(AsyncCallback asyncCallback,Object asyncState)在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TResult](AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate1 endDelegate,对象标记,Int32 超时)在 System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback 回调, 对象状态) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TResult](AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate1 endDelegate,对象标记,Int32 超时)在 System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback 回调,对象状态,BeginInvokeDelegate beginDelegate,EndInvokeDelegate endDelegate,对象标记)在 System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext , AsyncCallback 回调, 对象状态) 在 System.Web.Mvc.MvcHandler.<>c__DisplayClass8.b__2(AsyncCallback asyncCallback, Object asyncState) 在 System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.Begin(AsyncCallback callback, Object state, Int32 timeout) at System.Web.Mvc.Async.AsyncResultWrapper.Begin[TResult](AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate1 endDelegate, Object tag, Int32 timeout) at System.Web.Mvc.Async.AsyncResultWrapper.Begin(AsyncCallback callback, Object state, BeginInvokeDelegate beginDelegate, EndInvokeDelegate endDelegate, Object tag) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext , AsyncCallback 回调, 对象状态) 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤, Boolean& completedSynchronously)

这是我认为后映射是问题的构造函数,但是在调试时我没有收到错误。

Public Sub New(environmentCode As String, applicationCode As String)
    MyBase.New(environmentCode, applicationCode)

    SOBaseUrl = System.Configuration.ConfigurationManager.AppSettings(Enums.AppSettingKeys.SOBaseUrl.ToString())
    If Not String.IsNullOrEmpty(SOBaseUrl) Then
        SOBaseUrl = SOBaseUrl.TrimEnd("/")
    End If

    'Setup mapping.
    Mapper.CreateMap(Of Service.User, Models.User)() _
        .ForMember(Function(dest As Models.User) dest.ENumber, Sub(opt) opt.MapFrom(Function(src As Service.User) src.INumber)) _
        .AfterMap(Sub(src As Service.User, dest As Models.User)

            dest.Groups = New List(Of String)

            Using service = ApplicationSecurityManager.Service.Factory.GetService()

                Dim applicationPermissions = service.LoadPermissionsForUser(dest.Username, MyBase.EnvironmentCode)

                If (Not applicationPermissions Is Nothing AndAlso applicationPermissions.Any(Function(x) x.Code = MyBase.ApplicationCode)) Then

                    dest.Groups = applicationPermissions.Single(Function(x) x.Code = MyBase.ApplicationCode).GroupNames.ToList()

                End If

            End Using

        End Sub)

依赖注入映射:

container.RegisterType(Of IUserManager, UserManager)(New PerThreadLifetimeManager(),
    New InjectionConstructor(
      ConfigurationManager.AppSettings(Common.Enums.AppSettingKeys.Environment.ToString()),
      ConfigurationManager.AppSettings(Common.Enums.AppSettingKeys.ApplicationCode.ToString()))
    )

在 中saveUserResponse,错误被抛出。

 Public Function Create(user As Common.Models.User, approve As Boolean) As SO.Common.Models.User Implements IUserProvider.Save

    Dim saveUserResponse = UserManager.SaveUser(Mapper.Map(Of ApplicationSecurityManager.Service.User)(user))

    If Not String.IsNullOrEmpty(saveUserResponse.ErrorMessage) Then

        'The Security system returned an error.

        Throw New Exception("Security Service returned error: " & saveUserResponse.ErrorMessage)

    End If

    'Return the username.
    Return Mapper.Map(Of Common.Models.User)(saveUserResponse.User)

End Function
4

2 回答 2

1

这是在迭代集合时修改集合的常见错误。你有没有可能在这里留下一些我们看不到的东西?无论如何,在您提供的代码中,从我所看到的情况来看,没有任何这种情况。这意味着您可能在多线程环境中调用它,并且正在其他线程中修改集合。

你可以尝试什么?

考虑锁定您的enumeration,以便一次只能访问一个线程。当用户单击按钮时,可能会多次访问它。

这是一个很好的链接来帮助你:http ://weblogs.asp.net/leftslipper/mvc-locking-the-routecollection

于 2015-06-15T14:48:08.937 回答
1

当多个用户在映射的 AfterMap() 方法中修改同一个 user.Groups 集合时,就会出现这种情况。为避免这种情况,请锁定正在处理集合的代码。例如:

'class level object
private object _myLock = New object()

'Setup mapping.
Mapper.CreateMap(Of Service.User, Models.User)() _
    .ForMember(Function(dest As Models.User) dest.ENumber, Sub(opt) opt.MapFrom(Function(src As Service.User) src.INumber)) _
    .AfterMap(Sub(src As Service.User, dest As Models.User)
                  SyncLock _myLock
                      dest.Groups = New List(Of String)

                      Using service = ApplicationSecurityManager.Service.Factory.GetService()

                          Dim applicationPermissions = service.LoadPermissionsForUser(dest.Username, MyBase.EnvironmentCode)

                          If (Not applicationPermissions Is Nothing AndAlso applicationPermissions.Any(Function(x) x.Code = MyBase.ApplicationCode)) Then

                              dest.Groups = applicationPermissions.Single(Function(x) x.Code = MyBase.ApplicationCode).GroupNames.ToList()

                          End If

                      End Using
                  End SyncLock

              End Sub)
于 2015-06-17T10:38:48.337 回答