AutoFixture cannot create Claim (with .NET Framework 4.6)

拜拜、爱过 提交于 2019-12-12 11:32:43

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/31533739/autofixture-cannot-create-claim-with-net-framework-4-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!