AutoMapper: InvalidOperationException : ValueFactory attempted to access the Value property of this instance.

This issue was introduced to us after updating to version 5.2.0.

We have a test helper which creates our mapper and adds profiles for us via reflection (we also tried to wrap it with a lock).

private static IMapper MapAutoMapper()
{
	lock (new object())
	{
		var profiles = from assembly in AppDomain.CurrentDomain.GetAssemblies().Where(x => x.FullName.StartsWith("ProjectName."))
					   from profileType in assembly.GetExportedTypes()
					   where typeof(Profile).IsAssignableFrom(profileType) && !profileType.GetTypeInfo().IsAbstract
					   select (Profile)Activator.CreateInstance(profileType);

		var config = new MapperConfiguration(cfg =>
		{
			foreach (var profile in profiles)
			{
				cfg.AddProfile(profile);
			}
		});

		return config.CreateMapper();
	}
}

This often throws the exception down below on our build server (TeamCity). This should be related to a multi-threading issue as Lazy<> is involved which is not thread-safe, right?

System.InvalidOperationException : ValueFactory attempted to access the Value property of this instance.
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at AutoMapper.Mappers.ConvertMapper.MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
   at AutoMapper.Execution.TypeMapPlanBuilder.MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, TypePair typePair, Expression sourceParameter, Expression contextParameter, PropertyMap propertyMap, Expression destinationParameter)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(PropertyMap propertyMap)
   at AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap(PropertyMap propertyMap)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(Expression destinationFunc, Boolean constructorMapping)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateMapperLambda()
   at AutoMapper.TypeMap.Seal(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider)
   at AutoMapper.MapperConfiguration.Seal()
   at AutoMapper.MapperConfiguration..ctor(MapperConfigurationExpression configurationExpression, IEnumerable`1 mappers)
   at AutoMapper.MapperConfiguration..ctor(Action`1 configure)
   at Shared.TestHelper.WithFakes.MapAutoMapper() in C:\Something\Something\Project\Path\WithFakes.cs:line xx

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 27 (16 by maintainers)

Most upvoted comments

We’ve got a pending PR for this one.

On Thu, Dec 15, 2016 at 9:58 PM, Cindy notifications@github.com wrote:

I am having the same error. It only happens when I try to run all our tests at once. I create a new mapper in each test class, but if all of the tests are running at the same time, then we have multiple threads, each creating a mapper in the test class, so we get this collision. This didn’t happen before upgrading to 5.2.0 - we were able to run all of them at one time just fine. Each of our test classes has code in the constructor that looks like this:

` // create the mapping configuration var config = new MapperConfiguration ( map => { map.AddProfile(); map.AddProfile(); });

        // assert that the configuration is valid
        config.AssertConfigurationIsValid();

        this.mapper = config.CreateMapper();

`

We are quite dependent on AutoMapper in our application, and having it not work in test or on a Build Server when it runs all of the tests for an application at one time, is not really acceptable.

What is the correct way to solve this problem?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/AutoMapper/AutoMapper/issues/1847#issuecomment-267511730, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGYMgDcZwtagZxJpo8Wffnq_3gUb5Nhks5rIgx2gaJpZM4LJCMf .

Updating to the latest stable release 6.0.2 fixed my solution. Just wanted to say great work.

Try the MyGet build.