通过Umbraco控制器使用简单

通过Umbraco控制器使用简单

本文介绍了通过Umbraco控制器使用简单进样器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将依赖项注入到从Umbraco的RenderMvcController继承的控制器中并得到错误

I'm trying to Inject a dependency into a controller which inherits from Umbraco's RenderMvcController and getting the error

下面是我连接DI的代码

Below is my code to wire up the 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 the dependency, it inherits from a base class I wrote

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

基类扩展了作为Umbraco控制器的RenderMvcController

The base class extends RenderMvcController which is an Umbraco Controller

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

您可以看到基本的Umbraco控制器实际上有3个不同的构造函数

As you can see the base Umbraco controller does in fact have 3 different constructors

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

我不确定如何通过继承自Umbraco的控制器使SimpleInjector很好地放置。

I'm not sure how to get SimpleInjector to place nicely with this controller inherited from Umbraco.

预先感谢!

推荐答案

异常消息类型未注册可以找到RenderMvcController并且无法进行隐式注册,这意味着 RenderMvcController 类型是直接从Simple Injector请求的,而尚未注册。控制器类型通常仅由 DefaultControllerFactory 请求,并且仅当它在URL中获得带有控制器名称的请求时,才会请求特定类型,例如: http:\\localhost\RenderMvc\3

The exception message "No registration for type RenderMvcController could be found and an implicit registration could not be made" means that the RenderMvcController type is requested directly from Simple Injector, while it hasn't been registered. A controller type is usually only requested by the DefaultControllerFactory and it will only request a specific type when it gets a request that has the name of the controller in its url, as in: http:\\localhost\RenderMvc\3.

由于您在评论中指出 RenderMvcController 仅打算用作基本控制器,我怀疑它是MVC实际请求的。我认为您应该研究一下。

Since you stated in the comments that the RenderMvcController is only meant to be used as base controller, I find it suspicious that it is actually requested by MVC. I think you should look into that.

但是确实需要使用此控制器,您可以像这样在Simple Injector中简单地注册它:

But it the use of this controller is really required, you can simply register it in Simple Injector like this:

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

有一些方法可以覆盖Simple Injector的,但我建议您不要这样做,因为它是。明智的做法是不要在框架类型上使用容器的自动装配行为(如),因此建议使用lambda注册它们。

There are ways to override Simple Injector's constructor resolution behavior, but I would advise against doing this, because it is an anti-pattern for components to have multiple constructors. It's wise to don't use a container's auto-wiring behavior on framework types (as explained here), so registering them using a lambda is the advised practice.

这篇关于通过Umbraco控制器使用简单进样器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:56