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

Registration of open generics #80

Open
Tasteful opened this issue Nov 23, 2023 · 0 comments
Open

Registration of open generics #80

Tasteful opened this issue Nov 23, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@Tasteful
Copy link

I was adding an open generic class as below that

[RegisterSingleton(Registration = RegistrationStrategy.Self)]
public class DataProtector<TSource, TDestination>
{
}

created the following registration inside the DiscoveredServicesExtensions class

global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
    serviceCollection,
    global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
        typeof(global::PublicWeb.DataProtector<TSource, TDestination>),
        typeof(global::PublicWeb.DataProtector<TSource, TDestination>), 
        global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
    )
);

that resulted in msbuild error

error CS0246: The type or namespace name 'TSource' could not be found (are you missing a using directive or an assembly reference?)
error CS0246: The type or namespace name 'TDestination' could not be found (are you missing a using directive or an assembly reference?)

To solve this I was needing to add the ServiceType and ImplementationType as

[RegisterSingleton(Registration = RegistrationStrategy.Self, ServiceType = typeof(DataProtector<,>), ImplementationType = typeof(DataProtector<,>))]
public class DataProtector<TSource, TDestination>
{
}

to get the correct registration

global::Microsoft.Extensions.DependencyInjection.Extensions.ServiceCollectionDescriptorExtensions.TryAdd(
    serviceCollection,
    global::Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(
        typeof(global::PublicWeb.DataProtector<,>),
        typeof(global::PublicWeb.DataProtector<,>), 
        global::Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton
    )
);

It should be good if the library automatic detecting this open generic and creating correct mapping directly, or has an validation error that describes that open generics not is supported to map.

Using version 2.7.0 of Injectio.

@pwelter34 pwelter34 added the enhancement New feature or request label Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants