unitycontainer / microsoft-dependency-injection

Unity.Microsoft.DependencyInjection package
Apache License 2.0
63 stars 36 forks source link

ServiceContainerPicksConstructorWithLongestMatches() #8

Closed ENikS closed 6 years ago

ENikS commented 6 years ago

Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'FakeService'. at Microsoft.Extensions.DependencyInjection.Specification.Fakes.FakeService.Dispose() at Unity.Microsoft.DependencyInjection.Lifetime.InjectionSingletonLifetimeManager.RemoveValue(ILifetimeContainer container) at Unity.Container.Lifetime.LifetimeContainer.Dispose(Boolean disposing) at Unity.UnityContainer.Dispose(Boolean disposing) at Unity.UnityContainer.Dispose() at Unity.Microsoft.DependencyInjection.ServiceProvider.Finalize()

Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object. Object name: 'FakeService'. at Microsoft.Extensions.DependencyInjection.Specification.Fakes.FakeService.Dispose() at Unity.Microsoft.DependencyInjection.Lifetime.InjectionSingletonLifetimeManager.RemoveValue(ILifetimeContainer container) at Unity.Container.Lifetime.LifetimeContainer.Dispose(Boolean disposing) at Unity.UnityContainer.Dispose(Boolean disposing) at Unity.UnityContainer.Dispose() at Unity.Microsoft.DependencyInjection.ServiceProvider.Finalize() ExitCode != 0 ExitCode != 0 No more Benchmark runs will be launched as NO measurements were obtained from the previous run! No more Benchmark runs will be launched as NO measurements were obtained from the previous run!

ENikS commented 6 years ago

using System; using System.Diagnostics; using System.Linq; using System.Threading; using System.Reflection; using System.Runtime.CompilerServices; using BenchmarkDotNet; using BenchmarkDotNet.Characteristics; using BenchmarkDotNet.Environments; using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Extensions; using BenchmarkDotNet.Helpers; using BenchmarkDotNet.Running; using BenchmarkDotNet.Engines; using Microsoft.DependencyInjection.v2.Benchmark;

namespace BenchmarkDotNet.Autogenerated { public class Program {

    public static int Main(string[] args)
    {
        // this method MUST NOT have any dependencies to BenchmarkDotNet and any other external dlls!
        // otherwise if LINQPad's shadow copy is enabled, we will not register for AssemblyLoading event
        // before .NET Framework tries to load it for this method
        HelpAssemblyResolving();
        return AfterAssemblyLoadingAttached(args);
    }

    private static int AfterAssemblyLoadingAttached(string[] args)
    {
        var host = new ConsoleHost(Console.Out, Console.In);

        // the first thing to do is to let diagnosers hook in before anything happens
        // so all jit-related diagnosers can catch first jit compilation!
        host.BeforeAnythingElse();

        try
        {
            // we are not using Runnable here in any direct way in order to avoid strong dependency Main<=>Runnable
            // which could cause the jitting/assembly loading to happen before we do anything
            // we have some jitting diagnosers and we want them to catch all the informations!!

            Type type = typeof(Program).GetTypeInfo().Assembly.GetType("BenchmarkDotNet.Autogenerated.Runnable");

            type.GetMethod("Run", BindingFlags.Public | BindingFlags.Static).Invoke(null, new object[] { host });

            return 0;
        }
        catch(Exception ex)
        {
            host.WriteLine(ex.ToString());
            return -1;
        }
        finally
        {
            host.AfterAll();
        }
    }

    private static void HelpAssemblyResolving()
    {

if SHADOWCOPY

        AppDomain.CurrentDomain.AssemblyResolve += (_, args) =>
        {
            const string shadowCopyFolderPath = @"";

            var guessedPath = System.IO.Path.Combine(shadowCopyFolderPath, $"{new System.Reflection.AssemblyName(args.Name).Name}.dll");

            return System.IO.File.Exists(guessedPath) ? System.Reflection.Assembly.LoadFrom(guessedPath) : null;
        };

endif

    }
}

public class Runnable : global::Microsoft.DependencyInjection.v2.Benchmark.SpecificationV2
{
    public static void Run(IHost host)
    {
        Runnable instance = new Runnable();
        instance.Container = Activator.CreateInstance(Assembly.Load(new AssemblyName(@"Microsoft.DependencyInjection.v2.Benchmark, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")).GetType(@"Microsoft.DependencyInjection.v2.Benchmark.Adapters.UnityAdapter"));

        host.WriteLine();
        foreach (var infoLine in BenchmarkEnvironmentInfo.GetCurrent().ToFormattedString())
        {
            host.WriteLine("// {0}", infoLine);
        }
        var job = new Job();
        BenchmarkDotNet.Jobs.RunMode.LaunchCountCharacteristic[job] = 1;
            BenchmarkDotNet.Jobs.RunMode.TargetCountCharacteristic[job] = 2;
            BenchmarkDotNet.Jobs.RunMode.WarmupCountCharacteristic[job] = 2;
        job.Freeze();
        host.WriteLine("// Job: {0}", job.DisplayInfo);
        host.WriteLine();

        var engineParameters = new BenchmarkDotNet.Engines.EngineParameters()
        {
            Host = host,
            MainAction = instance.MainMultiAction,
            Dummy1Action = instance.Dummy1,
            Dummy2Action = instance.Dummy2,
            Dummy3Action = instance.Dummy3,
            IdleAction = instance.IdleMultiAction,
            GlobalSetupAction = instance.globalSetupAction,
            GlobalCleanupAction = instance.globalCleanupAction,
            IterationSetupAction = instance.iterationSetupAction,
            IterationCleanupAction = instance.iterationCleanupAction,
            TargetJob = job,
            OperationsPerInvoke = 1,
            MeasureGcStats = false
        };

        var engine = new BenchmarkDotNet.Engines.EngineFactory().Create(engineParameters);

        instance?.globalSetupAction();
        instance?.iterationSetupAction();

        if (job.ResolveValue(RunMode.RunStrategyCharacteristic, EngineResolver.Instance).NeedsJitting())
            engine.Jitting(); // does first call to main action, must be executed after globalSetup() and iterationSetup()!

        instance?.iterationCleanupAction();

        var results = engine.Run();

        instance?.globalCleanupAction();

        host.ReportResults(results); // printing costs memory, do this after runs
    }

    public delegate void IdleDelegate();

    public delegate  void TargetDelegate();

    public Runnable()
    {
        globalSetupAction = () => { };
        globalCleanupAction = () => { };
        iterationSetupAction = () => { };
        iterationCleanupAction = () => { };
        idleDelegate = __Idle;
        targetDelegate = Benchmark_ServiceContainerPicksConstructorWithLongestMatches;
    }

    private Action globalSetupAction;
    private Action globalCleanupAction;
    private Action iterationSetupAction;
    private Action iterationCleanupAction;
    private IdleDelegate idleDelegate;
    private TargetDelegate targetDelegate;

    private int dummyVar;

    [MethodImpl(MethodImplOptions.NoInlining)]
    private void Dummy1()
    {
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private void Dummy2()
    {
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    private void Dummy3()
    {
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
        dummyVar++;
    }

    private void __Idle() // __ is to avoid possible name conflict
    {

    }

if RETURNS_CONSUMABLE

    private Consumer consumer = new Consumer();

    private void IdleMultiAction(long invokeCount)
    {
        for (long i = 0; i < invokeCount; i++)
        {
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
            consumer.Consume(idleDelegate());
        }
    }

    private void MainMultiAction(long invokeCount)
    {
        for (long i = 0; i < invokeCount; i++)
        {
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
            consumer.Consume(targetDelegate());
        }
    }

elif RETURNS_NON_CONSUMABLE_STRUCT

    private void IdleMultiAction(long invokeCount)
    {
        void result = default(void);
        for (long i = 0; i < invokeCount; i++)
        {
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
            result = idleDelegate();
        }
        DeadCodeEliminationHelper.KeepAliveWithoutBoxing(result);
    }

    private void MainMultiAction(long invokeCount)
    {
        void result = default(void);
        for (long i = 0; i < invokeCount; i++)
        {
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
            result = targetDelegate();
        }
        NonGenericKeepAliveWithoutBoxing(result);
    }

    // we must not simply use DeadCodeEliminationHelper.KeepAliveWithoutBoxing<T> because it's generic method
    // and stack-only types like Span<T> can not be generic type arguments http://adamsitnik.com/Span/#span-must-not-be-a-generic-type-argument
    [MethodImpl(MethodImplOptions.NoInlining)]
    private void NonGenericKeepAliveWithoutBoxing(void _) { }

elif RETURNS_BYREF

    private void IdleMultiAction(long invokeCount)
    {
        void value = default(void);
        for (long i = 0; i < invokeCount; i++)
        {
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
            value = idleDelegate();
        }
        DeadCodeEliminationHelper.KeepAliveWithoutBoxing(value);
    }

    private void mainDefaultValueHolder = default(void);

    private void MainMultiAction(long invokeCount)
    {
        ref void alias = ref mainDefaultValueHolder;
        for (long i = 0; i < invokeCount; i++)
        {
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
            alias = targetDelegate();
        }
        DeadCodeEliminationHelper.KeepAliveWithoutBoxing(ref alias);
    }

else

    private void IdleMultiAction(long invokeCount)
    {
        for (long i = 0; i < invokeCount; i++)
        {
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
            idleDelegate();
        }
    }

    private void MainMultiAction(long invokeCount)
    {
        for (long i = 0; i < invokeCount; i++)
        {
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
            targetDelegate();
        }
    }

endif

}

}