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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Registering modules as Singleton is causing problems

enriquein opened this issue · comments

I'm currently trying to upgrade a web app to use .Net 6 and the pre-release version of Carter. Upgrading was pretty much painless, and the new API is pretty cool, however the changes to module DI registration introduced in 632c7c7 are causing me startup problems when a ICarterModule instance needs to request a scoped dependency.

The error I get is:

Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Carter.ICarterModule Lifetime: Singleton ImplementationType: MyServer.Modules.MyModule': Cannot consume scoped service 'MyServer.SomeScopedService' from singleton 'Carter.ICarterModule'.)

Is the idea now that all dependencies will need to be specified as part of the function that handles the request (ie: the RequestDelegate) instead of the module constructor? Or did I just miss an alternate way to register my modules for my use-case?

I guess as as workaround I can just skip calling AddCarter and register my modules, validators, and response negotiators by hand. But would be very helpful to know the official stance on this.

Thanks!

Looking at the new APIs I was afraid this was going to be the answer. Thanks for clarifying!