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

MEF and open generics using ConventionBuilder #18047

Closed
TMiNus opened this issue Aug 6, 2016 · 7 comments
Closed

MEF and open generics using ConventionBuilder #18047

TMiNus opened this issue Aug 6, 2016 · 7 comments
Labels
area-System.Composition bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@TMiNus
Copy link
Contributor

TMiNus commented Aug 6, 2016

I am trying to use MEF with open generics without markups attributes, this is an example of the problem

public class Logger<TLog>
{
      private ISetting settings;

      public Logger(ISetting settings)
      {
          this.settings = settings;
      }

      public TLog Fetch()
      {
          return default(TLog);
      }
}

To create the container I do

var conventions = new ConventionBuilder();

conventions.ForType<Setting>()
           .Export<ISetting>();
conventions.ForType(typeof(Logger<>))
           .Export();

var configuration = new ContainerConfiguration()
      .WithAssembly(typeof(Program).Assembly, conventions);

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

With that code I am getting a No importing constructor was found on type 'Logger`1[System.Object]' exception

If I mark the constructor with the attribute it works as intended but this should be possible without using markups

I have also tried this on the MEF version that ships with .NET40 and it works without the markups

My findings about the issue

@TMiNus TMiNus changed the title MEF and open generics using CompositionBuilder MEF and open generics using ConventionBuilder Aug 6, 2016
@TMiNus
Copy link
Contributor Author

TMiNus commented Aug 9, 2016

I think I got the bug solved, unit test enhanced. Sorry for the commit with the short message, fixed that :)

@nblumhardt
Copy link

SelectConstructor() should cover this scenario:

conventions.ForType(typeof(Logger<>))
    .SelectConstructor(c => c.Single())
    .Export();

But, as you've pointed out, it's not working here. The delegate is invoked with the list of constructors from Logger<>, but the returned value isn't being applied when constructors from Logger<object> are searched. This looks like a bug.

@TMiNus
Copy link
Contributor Author

TMiNus commented Oct 3, 2016

I already found the source of the problem for the SelectConstructor although the fix would require a little more work.

To be honest the problem is not rooted in SelectConstructor it derives from the open closed generics.

@karelz
Copy link
Member

karelz commented Nov 23, 2016

@TMiNus do you have a fix? We would be happy to take it ...

@karelz
Copy link
Member

karelz commented Jan 27, 2017

I assume it was fixed by dotnet/corefx#15315.

@karelz karelz closed this as completed Jan 27, 2017
@danielmeza
Copy link

I have this bug, I'm using System.Composition v 1.2 in a netcopreapp2.1

@karelz
Copy link
Member

karelz commented Nov 26, 2018

@danielmeza it was supposed to be fixed in 2.0.
Please create minimal repro and file a new issue if you think something is still left.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
@dotnet dotnet locked as resolved and limited conversation to collaborators Dec 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Composition bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

No branches or pull requests

6 participants