我试图弄清楚如何为 ASP.NET MVC3 配置 StructureMap 我已经在使用 NuGet 并且我注意到它使用名为 StructuremapMVC 的 cs 文件创建了 App_Start 文件夹,所以我检查它并注意到它是相同的代码但简化了将在 Global.asax 上的 App_Start 部分手动编写...
这是我来自 IoC 类的代码
public static class IoC
{
public static IContainer Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.AddAllTypesOf<IController>();
});
x.For<OpcionDB>().Use(() => new DatabaseFactory().Get());
});
return ObjectFactory.Container;
}
}
我的问题是为什么当我在 Controller 上注入(inject)一些 IoC 时会出现异常,如下所示(我使用这种模式:Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable):
private readonly IAsambleaRepository _aRep;
private readonly IUnitOfWork _uOw;
public AsambleaController(IAsambleaRepository aRep, IUnitOfWork uOw)
{
_aRep = aRep;
this._uOw = uOw;
}
public ActionResult List(string period)
{
var rs = _aRep.ByPeriodo(period).ToList<Asamblea>();
return View();
}
异常显示:
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
最佳答案
要处理 Controller 构造函数中的参数,必须配置依赖解析器。
查看以下帖子,了解如何将 StructureMap 与 ASP.NET MVC3 连接起来:
http://stevesmithblog.com/blog/how-do-i-use-structuremap-with-asp-net-mvc-3/
http://codebetter.com/jeremymiller/2011/01/23/if-you-are-using-structuremap-with-mvc3-please-read-this/
关于asp.net-mvc - 首先通过 NuGet 尝试 StructureMap 和 MVC3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5319539/