Using a simple injector with an Umbraco controller

I am trying to inject a dependency into a controller that inherits from Umbraco RenderMvcController and gets an error

"No registration for type RenderMvcController can be found and implicit registration cannot be done. For a container to create a RenderMvcController, it must have only one public constructor: it has 3. See https://simpleinjector.org/one-constructor for more information. "

Below is my code for connecting DI

var container = new Container(); container.Options.DefaultScopedLifestyle = new WebRequestLifestyle(); InitializeContainer(container); container.RegisterMvcControllers(Assembly.GetExecutingAssembly()); container.Verify(); DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container)); private static void InitializeContainer(Container container) { container.Register<ICacheProvider, CacheProvider>(Lifestyle.Transient); container.Register<ICacheService, CacheService>(Lifestyle.Transient); } 

This is an example of a class receiving a dependency; it inherits from the base class that I wrote

 public class NewsroomController : BaseRenderMvcController { public NewsroomController(ICacheService cacheService) : base(cacheService) { } 

The base class extends the RenderMvcController, which is the Umbraco controller

 public class BaseRenderMvcController : RenderMvcController { public ICacheService CacheService { get; set; } public BaseRenderMvcController(ICacheService cacheService) { CacheService = cacheService; } } 

As you can see, the Umbraco base controller actually has 3 different constructors

 public class RenderMvcController : UmbracoController, IRenderMvcController, IRenderController, IController { public RenderMvcController(); public RenderMvcController(UmbracoContext umbracoContext); public RenderMvcController(UmbracoContext umbracoContext, UmbracoHelper umbracoHelper); 

I'm not sure how to get SimpleInjector to fit well with this controller, inherited from Umbraco.

Thanks in advance!

+5
source share
1 answer

The exception message "No registration for the RenderMvcController type can be found and implicit registration cannot be performed" means that the RenderMvcController type RenderMvcController requested directly from the Simple Injector, while it was not registered. The controller type is usually requested only by DefaultControllerFactory , and it only requests a specific type when it receives a request with the name of the controller in its URL, for example: http:\\localhost\RenderMvc\3 .

Since the comments indicate that the RenderMvcController is for use only as the base controller, I find it suspicious that it is actually requested by MVC. I think you should study this.

But using this controller is really necessary, you can simply register it with Simple Injector as follows:

 container.Register<RenderMvcController>(() => new RenderMvcController()); 

There are ways to override Simple Injector constructor permission behavior , but I would advise against doing this because it is an anti-pattern for components to have multiple constructors . Of course, you should not use the behavior of automatically posting containers to frame types (as described here ), so registering them using lambda is the recommended practice.

+5
source

Source: https://habr.com/ru/post/1247265/


All Articles