类型HttpServerUtilityBase没有可访问的构造函数
本文关键字:访问 构造函数 HttpServerUtilityBase 类型 | 更新日期: 2023-09-27 17:53:49
我有一个名为ImageUploadService的服务类,它处理组合图像路径和将图像保存到服务器的一些复杂操作。它需要Controller的一个实例。Server对象的构造函数,它来自类HttpServerUtilityBase。我使用Unity。MVC5管理依赖关系,它给了我这个错误:
The type HttpServerUtilityBase does not have an accessible constructor.
那么我该如何解决这个问题呢?我觉得这是一个更复杂的问题作为控制器。Server不能在Controller的构造函数中直接访问,只能在Controller的action中使用。有人知道是否可以在Controller.Server上使用依赖注入吗?如果有,怎么做?
使用Unity,你需要注册HttpServerUtilityWrapper
如下:
container.RegisterType<HttpServerUtilityBase, HttpServerUtilityWrapper>(
new TransientLifetimeManager(),
new InjectionConstructor(HttpContext.Current.Server));
InjectionConstructor
调用传递HttpContext.Current.Server
对象的构造函数