StructureMap 异常 202 未定义默认实例(版本 2.61)
容器初始化:
public override void Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(s =>
{
s.TheCallingAssembly();
s.AssemblyContainingType<IRegistar>();
s.WithDefaultConventions();
});
});
}
Global.asax.xc
var dependencyContainer = new DependencyContainer();
dependencyContainer.Initialize();
ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());
获取实例:
public class DependencyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return base.GetControllerInstance(requestContext, controllerType);
}
try
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
catch (StructureMapException exception)
{
Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
}
控制器
private IBabyRepository _babyReposoitory; {
public BabyController(IBabyRepository babyRepository)
{
_babyReposoitory = babyRepository;
}
类尝试 DI/Ioc
public class BabyRepository : IBabyRepository, IRepository<Baby>, IRegistar
{
Just a Default constructor here();
code...
}
错误:
StructureMap 异常代码:202 没有为 PluginFamily Domain.Repository.IBabyRepository、Domain、Version=1.0.0.0、Culture=neutral、PublicKeyToken=null 定义默认实例
Me
我做错了什么?
Initialization of Container:
public override void Initialize()
{
ObjectFactory.Initialize(x =>
{
x.Scan(s =>
{
s.TheCallingAssembly();
s.AssemblyContainingType<IRegistar>();
s.WithDefaultConventions();
});
});
}
Global.asax.xc
var dependencyContainer = new DependencyContainer();
dependencyContainer.Initialize();
ControllerBuilder.Current.SetControllerFactory(new DependencyControllerFactory());
The Get Instance:
public class DependencyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
return base.GetControllerInstance(requestContext, controllerType);
}
try
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
catch (StructureMapException exception)
{
Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw;
}
}
}
The Controller
private IBabyRepository _babyReposoitory;
{
public BabyController(IBabyRepository babyRepository)
{
_babyReposoitory = babyRepository;
}
Class trying to DI/Ioc
public class BabyRepository : IBabyRepository, IRepository<Baby>, IRegistar
{
Just a Default constructor here();
code...
}
The Error:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily Domain.Repository.IBabyRepository, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Me
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不告诉 StructureMap 如何创建 BabyRepository。
我认为你的扫描需要是这样的:
然后需要编写自定义 RepositoryScanner 的代码:
You don't tell StructureMap how to create BabyRepository.
I think your scan needs to be something like this:
Then need to code up the custom RepositoryScanner: