Skip to content
This repository was archived by the owner on Nov 9, 2021. It is now read-only.
This repository was archived by the owner on Nov 9, 2021. It is now read-only.

Resolve throws an exception if CreateChildContainer is called multiple times with Unity 5.11.1 #1

Open
@kosta-arnorsky

Description

@kosta-arnorsky

The second Resolve call throws "ContainerControlledLifetimeManager can only be set once" exception in the following example:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using Unity;
using Unity.Microsoft.Options;

class Program
{
    static void Main(string[] args)
    {
        using (var container = new UnityContainer())
        {
            var configBuilder = new ConfigurationBuilder()
                .AddInMemoryCollection(new Dictionary<string, string>
                {
                    {"SomeValue", "value1"}
                });
            container
                .AddExtension(new OptionsExtension())
                .Configure<SomeOptions>(configBuilder.Build());

            //container.Resolve<IOptions<SomeOptions>>();

            using (var scope = ((IUnityContainer)container).CreateChildContainer())
            {
                var value = scope.Resolve<IOptions<SomeOptions>>().Value.SomeValue;
                Console.WriteLine(value);
            }
            using (var scope = ((IUnityContainer)container).CreateChildContainer())
            {
                var value = scope.Resolve<IOptions<SomeOptions>>().Value.SomeValue;
                Console.WriteLine(value);
            }
        }
    }

    class SomeOptions
    {
        public string SomeValue { get; set; }
    }
}

The issue can be reproduced only with Unity 5.11.1, with Unity version 5.10.0 it works fine. It also works if "container.Resolve<IOptions>()" is called first, all the following Resolve<IOptions>() calls return an IOptions instance. The issue doesn't depend on the configuration provider.

Expected result:
All Resolve<IOptions>() calls return an IOptions instance.

Actual result:
A Resolve<IOptions>() call on the second child container throws "ContainerControlledLifetimeManager can only be set once" exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions