Skip to content

ConventionsBuilder and open generic export with constructor dependencies (see also #10627) #30514

@ioancrisan

Description

@ioancrisan

See this test:

        [Test]
        public void SystemComposition_generic_export_with_ctor_dependency()
        {
            var conventions = new ConventionBuilder();

            conventions
                .ForType<Dependency>()
                .Export<Dependency>();
            conventions
                .ForType(typeof(MoreOpenWithDependency<>))
                .ExportInterfaces(
                    i => i.GetGenericTypeDefinition() == typeof(IOpen<>),
                    (type, builder) => builder.AsContractType(typeof(IOpen<>)))
                .SelectConstructor(ctors => ctors.First());

            var configuration = new ContainerConfiguration()
                .WithParts(new[] { typeof(IOpen<>), typeof(MoreOpenWithDependency<>) })
                .WithAssembly(this.GetType().Assembly, conventions);

            using (var container = configuration.CreateContainer())
            {
                var myService = container.GetExport(typeof(IOpen<object>));
            }
        }

        public class MoreOpenWithDependency<T> : IOpen<T>
        {
            // [ImportingConstructor]
            public MoreOpenWithDependency(Dependency dep) { }
        }

        public class Dependency { }

When explicitly adding the [ImportingConstructor] attribute, everything works fine, but without it, even if indicating the constructor in the conventions, it throws an:

System.Composition.Hosting.CompositionFailedException : No importing constructor was found on type 'Kephas.Tests.Composition.Mef.MefAmbientServicesBuilderExtensionsTest+MoreOpenWithDependency`1

Due to an automatic parts registration using implicit conventions it is not possible to add the [ImportingConstructor] attribute.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions