MVC 3 中的构造函数注入
我的控制器中有这段代码:
public class IssueController : BaseController, IIssueController
{
#region Members
IPublicationsManagementService publicationService;
#endregion
#region Constructors
public IssueController(IPublicationsManagementService publicationService)
{
this.publicationService = publicationService;
}
#endregion
public ActionResult IssueSearch()
{
return View(new IssueSearchViewModel()
{
Magazines
= new SelectList(publicationService.GetAllProducts(), "Id", "Name")
});
}
并且 Web 配置中的注入依赖项是:
<register type="Infoquality.PSMS.Presentation.Web.MVC.Client.Controllers.IIssueController, PSMS.Presentation.Web.MVC.Client"
mapTo="Infoquality.PSMS.Presentation.Web.MVC.Client.Controllers.IssueController, PSMS.Presentation.Web.MVC.Client">
<lifetime type="PerWebRequest" />
<constructor>
<param name="publicationService">
<dependency name="sd"/>
</param>
</constructor>
<register type="Infoquality.PSMS.Application.Publications.PublicationsManagement.IPublicationsManagementService, PSMS.Application.Publications"
mapTo="Infoquality.PSMS.Application.Publications.PublicationsManagement.PublicationsManagementService, PSMS.Application.Publications"
name="sd">
<lifetime type="singleton" />
</register>
当我运行应用程序时,错误是:
源错误:
执行当前 Web 请求期间生成未处理的异常。有关异常来源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[MissingMethodException:否 无参数构造函数定义为 这个对象。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType 类型、布尔值 publicOnly、布尔值 noCheck、布尔值&可以缓存, RuntimeMethodHandleInternal&演员, 布尔& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(布尔值 publicOnly,布尔值skipCheckThis, 布尔值 fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(布尔值 仅限公共,布尔值 跳过可见性检查,布尔值 SkipCheckThis,布尔值 fillCache) +241 System.Activator.CreateInstance(类型 类型,布尔非公共)+69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext,类型控制器类型) +67
我需要 ASP.NET MVC 3 和 razor 引擎来调用参数化构造函数。
I have this code in my controller:
public class IssueController : BaseController, IIssueController
{
#region Members
IPublicationsManagementService publicationService;
#endregion
#region Constructors
public IssueController(IPublicationsManagementService publicationService)
{
this.publicationService = publicationService;
}
#endregion
public ActionResult IssueSearch()
{
return View(new IssueSearchViewModel()
{
Magazines
= new SelectList(publicationService.GetAllProducts(), "Id", "Name")
});
}
and the injection dependencies in web config is:
<register type="Infoquality.PSMS.Presentation.Web.MVC.Client.Controllers.IIssueController, PSMS.Presentation.Web.MVC.Client"
mapTo="Infoquality.PSMS.Presentation.Web.MVC.Client.Controllers.IssueController, PSMS.Presentation.Web.MVC.Client">
<lifetime type="PerWebRequest" />
<constructor>
<param name="publicationService">
<dependency name="sd"/>
</param>
</constructor>
<register type="Infoquality.PSMS.Application.Publications.PublicationsManagement.IPublicationsManagementService, PSMS.Application.Publications"
mapTo="Infoquality.PSMS.Application.Publications.PublicationsManagement.PublicationsManagementService, PSMS.Application.Publications"
name="sd">
<lifetime type="singleton" />
</register>
When I run the application the error is :
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No
parameterless constructor defined for
this object.]
System.RuntimeTypeHandle.CreateInstance(RuntimeType
type, Boolean publicOnly, Boolean
noCheck, Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor,
Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean
publicOnly, Boolean skipCheckThis,
Boolean fillCache) +98
System.RuntimeType.CreateInstanceDefaultCtor(Boolean
publicOnly, Boolean
skipVisibilityChecks, Boolean
skipCheckThis, Boolean fillCache) +241
System.Activator.CreateInstance(Type
type, Boolean nonPublic) +69
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
requestContext, Type controllerType)
+67
I need the ASP.NET MVC 3 and razor engine to call the parametrized constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要实现 IDependencyResolver 接口。一旦执行此操作,您就可以在 MVC 3 应用程序中的任何位置使用依赖项注入,甚至是视图(尽管仅限属性注入)。
You need to implement the
IDependencyResolver
interface. Once you do this you can use dependency injection anywhere within a MVC 3 application, even views (although property injection only).