CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.05k stars 172 forks source link

Error On Register IN DI #328

Closed Mubark-p closed 9 months ago

Mubark-p commented 9 months ago

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Carter.IValidatorLocator Lifetime: Singleton ImplementationType: Carter.DefaultValidatorLocator': Cannot consume scoped service 'System.Collections.Generic.IEnumerable`1[FluentValidation.IValidator]' from singleton 'Carter.IValidatorLocator'.)'

crodriguesbr commented 9 months ago

@Mubark-p Test with the new version. You can now define the scope of validators.

koniecdev commented 5 months ago

Can You explain how? @crodriguesbr Probably because Carter, my FluentValidation is not working. https://github.com/FluentValidation/FluentValidation/issues/2183

carter.ivalidatorlocator lifetime: singleton implementationtype: carter.defaultvalidatorlocator': cannot consume scoped service 'savethekitty.api.databases.interfaces.imaindbcontext' from singleton 'fluentvalidation.ivalidator'.

crodriguesbr commented 5 months ago

@koniecdev A service registered as a singleton cannot invoke a service registered as scoped, which is why a way of defining how validators would be registered was implemented in Carter.

services.AddCarter(configurator: cfg => { cfg.WithValidatorLifetime(ServiceLifetime.Scoped); });

I hope it resolves.

koniecdev commented 5 months ago

It worked @crodriguesbr . Thank You. I kinda wonder why is this not default ngl.

crodriguesbr commented 5 months ago

@koniecdev As Carter already registered validators as singletons, we decided to keep them registered by default to avoid code breakage. Still, we left the developer the option to change this.