Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child Containers and Singleton Open Generic Registrations cannot be external #10

Closed
dazinator opened this issue Sep 25, 2020 · 0 comments

Comments

@dazinator
Copy link
Owner

The native Microsoft ServiceProvider doesn't allow a registration of a singleton open generic type to be an instance that can be provided "externally". So when the child container backed by ServiceProvider is built, and the ServiceDescriptors from the parent are examined, I'll by default throw an exception when these are encountered. You can pass in an additional enum to specify how you want these services to be dealt with:

 public enum ParentSingletonOpenGenericRegistrationsBehaviour
    {
        /// <summary>
        /// If there are singleton open generic registerations in the parent container then an exception will be thrown when creating the child container. This is because there is currnetly no way to have the child container resolve the same instance of those as when resolved through the parent container. From the exception you can see a list of these unsupported registrations and then work out how to handle them.
        /// </summary>
        ThrowNotSupportedException = 0,
        /// <summary>
        /// If there are singleton open generic registerations in the parent container, they will also be registered again in the child container as seperate singletons. This means resolving an open generic type with the same type parameters in the parent and child container will yield two seperate instances of that service.
        /// </summary>
        DuplicateSingletons = 1,
        /// <summary>
        /// If there are singleton open generic registerations in the parent container, they will be omitted from the child container. In other words you'll have to register them into the child container yourself otherwise they will fail to resolve from the child container.
        /// </summary>
        Omit = 2
    }

There might be a solution for this in future, mostly depending on the outcome of issue: dotnet/runtime#41050

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant