duskembayev / Enhanced.DependencyInjection

Enhanced.DependencyInjection is a NuGet package for simplified dependency registration in .NET Core DI container. It uses C# attributes and a source generator for explicit registration, making the process more understandable and faster.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Code-time type validation for ContainerEntry attribute

sergey-rubtsov-05 opened this issue · comments

It would be very good to verify types in ContainerEntry attribute. Currently, a mistake can be seen only in runtime when ASP.NET resolves dependency with a wrong interface.

For example:

[ContainerEntry(ServiceLifetime.Scoped, typeof(IOtherService))]
public class MyService { }

public interface IOtherService { }

If you try to resolve IOtherService dependency, you will get the exception:

System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: AspNetWebApp.IOtherService Lifetime: Scoped ImplementationType: AspNetWebApp.MyService': Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService')
 ---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: AspNetWebApp.IOtherService Lifetime: Scoped ImplementationType: AspNetWebApp.MyService': Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService'
 ---> System.ArgumentException: Implementation type 'AspNetWebApp.MyService' can't be converted to service type 'AspNetWebApp.IOtherService'
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite..ctor(ResultCache cache, Type serviceType, ConstructorInfo constructorInfo, ServiceCallSite[] parameterCallSites)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   --- End of inner exception stack trace ---
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(String[] args) in \AspNetPlaygroung\AspNetWebApp\Program.cs:line 13