无法解析控制器

发布于 2024-10-08 16:38:50 字数 1313 浏览 2 评论 0原文

以前我没有为我的通用存储库使用接口。当我从通用存储库中提取接口时,我添加了两个构造函数:无参数构造函数和参数化构造函数,我收到以下错误:

{"Resolution of the dependency failed, type = \"NascoBenefitBuilder.Controllers.ODSController\", name = \"(none)\".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, ControllerLib.Models.Generic.IGenericRepository, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving NascoBenefitBuilder.Controllers.ODSController,(none)
Resolving parameter \"repo\" of constructor NascoBenefitBuilder.Controllers.ODSController(ControllerLib.Models.Generic.IGenericRepository repo)
Resolving ControllerLib.Models.Generic.IGenericRepository,(none)"}

开头的我的控制器:

public class ODSController : ControllerBase
{   
    IGenericRepository _generic = new GenericRepository();
}

提取接口并在控制器中使用它后:

public class ODSController : ControllerBase
{
    IGenericRepository _generic;
    public ODSController() : this(new GenericRepository())
    {
    }

    public ODSController(IGenericRepository repo)
    {
        _generic = repo;
    }
}

当我使用参数化构造函数时正在抛出上面提到的错误。

谁能帮助我克服这个问题?

Previously I was not using an interface for my generic repository. When I extracted the interface from my generic repository, I added two constructors: a parameterless and a parameterized constructors I am getting the following error:

{"Resolution of the dependency failed, type = \"NascoBenefitBuilder.Controllers.ODSController\", name = \"(none)\".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, ControllerLib.Models.Generic.IGenericRepository, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving NascoBenefitBuilder.Controllers.ODSController,(none)
Resolving parameter \"repo\" of constructor NascoBenefitBuilder.Controllers.ODSController(ControllerLib.Models.Generic.IGenericRepository repo)
Resolving ControllerLib.Models.Generic.IGenericRepository,(none)"}

My Controller at the beginning:

public class ODSController : ControllerBase
{   
    IGenericRepository _generic = new GenericRepository();
}

After extracting the interface and use it in controller:

public class ODSController : ControllerBase
{
    IGenericRepository _generic;
    public ODSController() : this(new GenericRepository())
    {
    }

    public ODSController(IGenericRepository repo)
    {
        _generic = repo;
    }
}

When I use parameterized constructor it is throwing error mentioned above.

Can anyone help me to overcome this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-10-15 16:38:51

您不再需要默认构造函数:

public class ODSController : ControllerBase
{
    private readonly IGenericRepository _repository;
    public ODSController(IGenericRepository repository)
    {
        _repository = repository;
    }
}

然后确保您已正确配置 Unity 容器:

IUnityContainer container = new UnityContainer()
    .RegisterType<IGenericRepository, GenericRepository>();

并且您在 Application_Start 中使用 Unity 控制器工厂:

ControllerBuilder.Current.SetControllerFactory(typeof(UnityControllerFactory));

You no longer need the default constructor:

public class ODSController : ControllerBase
{
    private readonly IGenericRepository _repository;
    public ODSController(IGenericRepository repository)
    {
        _repository = repository;
    }
}

And then make sure you've properly configured your Unity container:

IUnityContainer container = new UnityContainer()
    .RegisterType<IGenericRepository, GenericRepository>();

And that you are using the Unity controller factory in Application_Start:

ControllerBuilder.Current.SetControllerFactory(typeof(UnityControllerFactory));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文