AutoFixture cannot create Claim (with .NET Framework 4.6)
Asked Answered
O

1

6

I've just installed VS2015, which at the same time installed .NET Framework 4.6, and suddenly AutoFixture 3.30.8 is unable to create a Claim object. I'm guessing having the new .NET Framework version 4.6 is causing some issue inside AutoFixture.

I created a test project (targeting .NET Framework 4.5.1) containing the following code in VS2013:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ploeh.AutoFixture;

namespace AutoFixtureTester
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void CanCreateClaim()
        {
            var fixture = new Fixture();
            fixture.Behaviors.Add(new OmitOnRecursionBehavior());

            var claim = fixture.Create<System.Security.Claims.Claim>(); // exception here

            Assert.IsNotNull(claim);
        }
    }
}

The error is as follows:

Ploeh.AutoFixture.ObjectCreationException: AutoFixture was unable to create an instance from System.IO.Stream, most likely because it has no public constructor, is an abstract or non-public type.

Request path:
         System.Security.Claims.Claim --> 
          System.IO.BinaryReader reader --> 
           System.IO.BinaryReader --> 
            System.IO.Stream input --> 
             System.IO.Stream
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.SeedIgnoringRelay.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.ParameterRequestRelay.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.MethodInvoker.<>c__DisplayClass2.<Create>b__0(ParameterInfo pi)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Ploeh.AutoFixture.Kernel.MethodInvoker.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.SeedIgnoringRelay.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.ParameterRequestRelay.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.MethodInvoker.<>c__DisplayClass2.<Create>b__0(ParameterInfo pi)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Ploeh.AutoFixture.Kernel.MethodInvoker.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.Kernel.SeedIgnoringRelay.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.AutoPropertiesTarget.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.Postprocessor`1.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TracingBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.BehaviorRoot.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.RecursionGuard.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Fixture.Create(Object request, ISpecimenContext context)
   at Ploeh.AutoFixture.Kernel.SpecimenContext.Resolve(Object request)
   at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context, T seed)
   at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenContext context)
   at Ploeh.AutoFixture.SpecimenFactory.Create[T](ISpecimenBuilder builder)
   at AutoFixtureTester.UnitTest1.CanCreateClaim() in D:\Sandbox\AutoFixtureTester\AutoFixtureTester\UnitTest1.cs:line 16

Before installing VS2015, this way of creating a Claim object used to work. Actually I sent this solution to my colleague who only has VS2013 installed and it works for him.

I noticed that F12-navigating to the Claim code shows metadata from the 4.5.1 DLLs. Is there an issue with the VS2015 installer screwing up 4.5.1 DLLs? Or could this be a bug in AutoFixture that installing 4.6 has unearthed?


I compared DotPeek output for System.Security.Claims.Claim from my machine with my colleague's one. His shows:

// Assembly location: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\mscorlib.dll

where mine shows:

// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll

His has 5 constructors taking either strings or ClaimsIdentity.
Mine has 7 constructors, including 2 new ones that look like this:

public Claim(BinaryReader reader) { ... }

and

public Claim(BinaryReader reader, ClaimsIdentity subject) { ... }

I guess the workaround for this case would be to somehow force AutoFixture to use a specific constructor. But it does make me nervous that this breaking change has slipped through Microsoft's net. Are there any other changes like this?

Overcome answered 21/7, 2015 at 8:16 Comment(2)
According to the docs, Stream might be in System.IO.dll for 4.6 perhaps. It is very unclear.Emotional
Related: #12722257Emotional
O
6

As a workaround for getting AutoFixture to work as before, I customized the way it creates a Claim to choose the constructor with the most parameters.

fixture.Customize<System.Security.Claims.Claim>(
    c => c.FromFactory(new MethodInvoker(new GreedyConstructorQuery())));

The reason for this issue seems to be that installing 4.6 has affected the 4.5.1 DLLs. Claim now has 7 constructors, and the 2 new constructors take a BinaryReader argument which depends on Stream which cannot be constructed by AutoFixture. Luckily, AutoFixture is nice and configurable :)

As mentioned above, it does make me nervous that this breaking change has slipped through Microsoft's net. Also it worries me that if they decide to add another constructor in the future to Framework 4.7, taking 20 arguments including a BinaryReader, then we will be up the creek again.

Overcome answered 21/7, 2015 at 9:8 Comment(3)
I'm convinced that Microsoft doesn't consider this a breaking change. Normal code that used claim before will keep compiling and working as before; addition of new members to a class doesn't constitute a breaking change. The reason AutoFixture breaks is because it never knew how to create a Claim instance. Apart from a handful of well-known BCL types, AutoFixture doesn't know how to create objects of various types, but instead uses best-effort heuristics to create objects. When the framework changes, occasional hiccups like this one are expected; sometimes, it may go the other way.Olivas
Thanks Mark. I suppose I do agree that adding these constructors should not have caused anything to break. What caught me out was installing 4.6 changing the behaviour of 4.5.1. But I guess that could happen at any time with Windows Update, it's just something we need to watch out for.Overcome
@Overcome I know it has been a while, is this causing you any issues?Ninety

© 2022 - 2024 — McMap. All rights reserved.