AspNetDependencyInjection icon indicating copy to clipboard operation
AspNetDependencyInjection copied to clipboard

The `IEnumerable<Object> DependencyInjectionMvcDependencyResolver.GetServices(Type serviceType)` causes an NRE

Open djseng opened this issue 5 years ago • 6 comments

The GetServices call causes an NRE to happen when nothing can be resolved from the container.

This issue is similar; coalescing an empty enumerable appears to resolve the issue.

djseng avatar Sep 04 '20 17:09 djseng

Thank you for the contribution!

Looking at my original code I see that closedGenericType is unused - so I wonder if I should have passed that as the parameter argument to GetService instead... or if I left that-in as a reference without commenting it out by mistake.

I do remember (during my development of support for ASP.NET MVC) that I broke DI by returning a non-null, but empty, IEnumerable<T> instead of null.

I need to take a quick look at how ASP.NET MVC consumes the result of IEnumerable<Object> GetServices(Type serviceType) to verify your fix.

I see in the linked SimpleInjector thread that @dotnetjunkie writes (emphasis mine):

Although Simple Injector 3 contains both methods, GetAllInstances does not match the expectations of MVC, because MVC expects that method to return an empty collection in case no collection is registered.

I assume he's correct on this though.

Do you have a bug repro scenario for this issue? And do you have a unit-test or integration-test that proves this fixes it?

Thanks again!

daiplusplus avatar Sep 18 '20 12:09 daiplusplus

To reproduce the issue, #17 would need to be resolved, and then visiting one of the basic routes in the SampleMvcWebApplication will demonstrate this issue. It is happening, if I recall, in the bowels of the Mvc pipeline. I will explore creating an integration test soon.

djseng avatar Sep 21 '20 11:09 djseng

I can confirm that the closedGenericType parameter is necessary and is what needs to be passed to the GetService call, assuming you actually want to take advantage of dependency injection for supplying the providers. Technically passing in the raw type itself works so long as you don't ever register one of the providers via dependency injection, but immediately causes a cast error if you do.

TheJayMann avatar Jul 13 '21 15:07 TheJayMann

fix in fa60c05

rli-dseng avatar Jul 13 '21 15:07 rli-dseng

I've just published version 5.0.0-beta01 which solves this issue. Can you give it a try to confirm it fixed it for you?

https://www.nuget.org/packages/Jehoel.AspNetDependencyInjection/5.0.0-beta01

https://www.nuget.org/packages/Jehoel.AspNetDependencyInjection.Mvc/5.0.0-beta01

daiplusplus avatar Jul 19 '21 04:07 daiplusplus

I would, but I am not on that project anymore. I'm sorry.

rli-dseng avatar Jul 19 '21 12:07 rli-dseng