Skip to content

Support generic imports (GenericHost/Options DI) #457

@weltkante

Description

@weltkante

We've had Generic Host for a while now and I've been looking into using vs-mef as DI instead of the inbox implementation, to integrate better with existing codebases which already are MEF based. I've made good progress mapping the builder abstractions and constructing a dynamic vs-mef catalog from ServiceDescriptors during startup, but when I got to the options infrastructure of .NET Core I'm running into the problem that they adopted generic imports, something vs-mef doesn't support yet.

An example for exports added to the builder is here:

services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptions<>), typeof(UnnamedOptionsManager<>)));
services.TryAdd(ServiceDescriptor.Scoped(typeof(IOptionsSnapshot<>), typeof(OptionsManager<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>)));
services.TryAdd(ServiceDescriptor.Transient(typeof(IOptionsFactory<>), typeof(OptionsFactory<>)));
services.TryAdd(ServiceDescriptor.Singleton(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>)));

So .NET is creating generic exports, which is fine, vs-mef supports that. However when we look ad e.g. UnnamedOptionsManager<TOptions> we can see its imports:

public UnnamedOptionsManager(IOptionsFactory<TOptions> factory)

i.e. the import depends on the generic parameter, which is something vs-mef does not support because the TypeRef infrastructure has no strategy to deal with it and just throws.

Conceptually I believe it should be fine if a generic export imports another generic export with a matching parameter? So the question is if there is any desire to have this support added to vs-mef.

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