[英]Can't using StructureMap 4.5.2 in Asp.net MVC
I'm new to StructureMap and i'm using version 4.5.2. 我是StructureMap的新手,正在使用4.5.2版。 I registered my DataContext with StructureMap in Global.asax like this: 我像这样在Global.asax中用StructureMap注册了DataContext:
void Application_Start(object sender, EventArgs e)
{
SetupIcoContainer();
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
private void SetupIcoContainer()
{
var container = new StructureMap.Container(_ =>
{
_.For<Data.IUnitOfWork>().Singleton().Use(x => new Data.Database.DataContext());
});
}
and I'm using the dependency in my repository class like this: 我在我的存储库类中使用依赖项,如下所示:
var uow = new StructureMap.Container().GetInstance<Data.IUnitOfWork>();
but i get this error: 但我得到这个错误:
No default Instance is registered and cannot be automatically determined for type 'Data.IUnitOfWork' 没有注册默认实例,并且无法自动确定类型'Data.IUnitOfWork'
The Data.IUnitOfWork
, Data.Database.DataContext
and my Repository class are in another assembly. Data.IUnitOfWork
, Data.Database.DataContext
和我的存储库类在另一个程序集中。 The StructureMap doesn't register the DataContext. StructureMap不注册DataContext。
Thanks 谢谢
var uow = new StructureMap.Container();
instantiates a new empty container that does not know IUnitOfWork. 实例化一个不知道IUnitOfWork的新空容器。
You can try to set a global static variable in global.asax.cs 您可以尝试在global.asax.cs中设置全局静态变量
The best way is to use StructureMap as MVC DependencyResolver 最好的方法是将StructureMap用作MVC DependencyResolver
Here there is a way How to get the Structuremap IContainer instance from Asp.Net MVC 5 Dependency Resolver 这里有一种方法如何从Asp.Net MVC 5依赖关系解析器获取Structuremap IContainer实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.