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.08k stars 175 forks source link

Should assembly scanning provide the entryAssembly even if Carter is not a direct dependency for it? #171

Closed jussimattila closed 2 years ago

jussimattila commented 5 years ago

How would you feel if assembly scanning also returned the assembly given as parameter to new DependencyContextAssemblyCatalog(Assembly entryAssembly), even if the entry assembly didn't have a direct dependency on Carter?

The change would modify DependencyContextAssemblyCatalog, which currently filters out all assemblies that don't reference Carter or FluentValidation directly.

The reason I'm asking is that I've put some common initialization code in a library and referencing/calling it from several web API projects. Whenever a new project is created using this library, I first get 404s on my routes because the modules are in an assembly that is not taking a direct dependency on Carter (or FluentValidation). Then, I'm scratching my head for a while and have to remind myself why don't the routes get registered. Of course, having anyone else use this library would also require documentation around "Remember to reference Carter in your project even though you are not relying on it directly". I hoped calling services.AddCarter(DependencyContextAssemblyCatalog(Assembly.GetCallingAssembly())) in my library would help, but turns out it doesn't.

I'm open to submitting a PR for this if this if the proposed change is acceptable. I don't know if there are any downsides to always returning the entry assembly, so you probably need to consider this before saying "do it".

jchannon commented 5 years ago

Maybe @tomzo can help you as I believe he does something similar?

samatstariongroup commented 4 years ago

I'm having the same problem, i have some code in a different library as well, my DTO's are in a shared project (so i can make a nuget of it and use it on the client side as well). When I start the applicaiton i get the following exception:

System.Reflection.ReflectionTypeLoadException: 'Unable to load one or more of the requested types. at services.AddCarter(..)

My workaround for now is to add FluentValidation as a dependency to any libraries I write that I want to use in my Carter project, then it works.. Would be nice that this extra dependency is not necessary.

jzabroski commented 4 years ago

Can you replace assembly scanning entirely with Scrutor

samatstariongroup commented 4 years ago

if at the end we can still have dependency injection with another DI (i am a fan of Autofac) so we can have for instance property injection, then that could work

Dreamwalker666 commented 3 years ago

Can you replace assembly scanning entirely with Scrutor

I'm actually against this. (I work in a regulated industry and each dependency has to be independently scanned and approved before inclusion in a project and the slightest issue with it then its a no go. That whole process takes ages.)

If anything allow replacement of the scanner don't bring in another dependency.

That also would solve this issue meaning the OP could have just replaced the scanning routine with something that worked for them? So maybe that's a possible solution to this?

Dreamwalker666 commented 3 years ago

@jzabroski I just realised you can override DependencyContextAssemblyCatalog and put your own implementation already!

So isn't this issue already covered in the existing AddCarter method specifying an overridden DependencyContextAssemblyCatalog?