MVC 控制器需要空构造器,但未使用接口并抛出错误
MVC controller wants empty contructor but then Interface is not used and throws error
我收到这个错误:
An exception of type 'System.NullReferenceException' occurred in PubStuff.Intern.Web.Internal.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object
public class InternController : BaseController
{
IInternService _internService;
public InternController() { }
public InternController(IInternService internService)
{
_internService = internService;
}
// GET: Intern
public ActionResult Index()
{
object responseObject = null;
responseObject = _internService.GetAllSkills();
return View();
}
}
- 它抱怨如果我没有空构造函数
- 一旦有一个空的构造函数,那么这一行
responseObject = _internService.GetAllSkills();
就会抛出错误。
_internService 为空
我该如何解决? 有什么问题?
更新
无论我添加 IInternUnitOfWork 还是不添加 StructureMap,我最终都会遇到问题。
我将 IInternService
添加到 StructureMap 但没有帮助
抛出错误
protected override object DoGetInstance(Type serviceType, string key)
{
if (string.IsNullOrEmpty(key))
{
return serviceType.IsAbstract || serviceType.IsInterface
? this.Container.TryGetInstance(serviceType)
: this.Container.GetInstance(serviceType);
}
return this.Container.GetInstance(serviceType, key);
}
"StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily PublicHealth.Intern.DataAccess.Contracts.IInternUnitOfWork, PublicHealth.Intern.DataAccess.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
你需要一个空的构造函数,所以试试这个:
public InternController():this(new MyInternService())
{
}
其中 MyInternService
是您的默认 IInternService
实现(即,您将在生产中使用的 IInternService
)。
此模式最常用于向控制器提供 "testing" 数据,并能够在调用时更改某些定义,例如使用测试框架。
看来您已经被 "No default constructor" 错误信息吓坏了。使用 DI 时,NOT 意味着您应该添加一个空构造函数。
public InternController() { }
事实上,using multiple constructors with DI is anti-pattern.
此错误消息表示您的 DI 容器未插入 MVC,因此 MVC 无法通过 DI 容器解析您的构造函数参数。您需要添加行以将其插入,如下所示:
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
或
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
这些行之一需要在您 composition root 内的应用程序启动代码中,就在您使用 StructureMap 注册类型之后。
我收到这个错误:
An exception of type 'System.NullReferenceException' occurred in PubStuff.Intern.Web.Internal.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object
public class InternController : BaseController
{
IInternService _internService;
public InternController() { }
public InternController(IInternService internService)
{
_internService = internService;
}
// GET: Intern
public ActionResult Index()
{
object responseObject = null;
responseObject = _internService.GetAllSkills();
return View();
}
}
- 它抱怨如果我没有空构造函数
- 一旦有一个空的构造函数,那么这一行
responseObject = _internService.GetAllSkills();
就会抛出错误。
_internService 为空
我该如何解决? 有什么问题?
更新 无论我添加 IInternUnitOfWork 还是不添加 StructureMap,我最终都会遇到问题。
我将 IInternService
添加到 StructureMap 但没有帮助
抛出错误
protected override object DoGetInstance(Type serviceType, string key)
{
if (string.IsNullOrEmpty(key))
{
return serviceType.IsAbstract || serviceType.IsInterface
? this.Container.TryGetInstance(serviceType)
: this.Container.GetInstance(serviceType);
}
return this.Container.GetInstance(serviceType, key);
}
"StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily PublicHealth.Intern.DataAccess.Contracts.IInternUnitOfWork, PublicHealth.Intern.DataAccess.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
你需要一个空的构造函数,所以试试这个:
public InternController():this(new MyInternService())
{
}
其中 MyInternService
是您的默认 IInternService
实现(即,您将在生产中使用的 IInternService
)。
此模式最常用于向控制器提供 "testing" 数据,并能够在调用时更改某些定义,例如使用测试框架。
看来您已经被 "No default constructor" 错误信息吓坏了。使用 DI 时,NOT 意味着您应该添加一个空构造函数。
public InternController() { }
事实上,using multiple constructors with DI is anti-pattern.
此错误消息表示您的 DI 容器未插入 MVC,因此 MVC 无法通过 DI 容器解析您的构造函数参数。您需要添加行以将其插入,如下所示:
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory(container));
或
DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
这些行之一需要在您 composition root 内的应用程序启动代码中,就在您使用 StructureMap 注册类型之后。