HttpServerUtilityBase类型没有可访问的构造函数

时间:2015-06-07 03:21:54

标签: c# asp.net dependency-injection asp.net-mvc-5 unity-container

我有一个名为ImageUploadService的服务类,它处理组合图像路径和将图像保存到服务器的一些复杂操作。它将在其构造函数中需要一个Controller.Server对象的实例,该实例最终来自类HttpServerUtilityBase。我使用Unity.MVC5来管理依赖项,它给了我这个错误:

The type HttpServerUtilityBase does not have an accessible constructor.

那么我该如何解决这个问题呢?我认为这是一个更复杂的问题,因为Controller.Server不能直接在Controller的构造函数中访问,只能在控制器的操作中使用。有人知道是否可以在Controller.Server上使用依赖注入?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:0)

使用Unity,您需要注册HttpServerUtilityWrapper,如下所示:

container.RegisterType<HttpServerUtilityBase, HttpServerUtilityWrapper>(
    new TransientLifetimeManager(),
    new InjectionConstructor(HttpContext.Current.Server));

关键是容器配置为使用HttpContext.Current.Server调用传递InjectionConstructor对象的构造函数。

相关问题