为什么结构图没有获取我的 HomeController?
这是asp.net mvc3。
当我尝试转到我的 home/index 操作时:
public class HomeController : Controller
{
private IBar bar;
public HomeController(IBar bar)
{
this.bar = bar;
}
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.Message = "hello world yo: " + bar.SayHi();
return View();
}
}
public interface IBar
{
string SayHi();
}
public class Bar : IBar
{
public string SayHi()
{
return "Hello from BarImpl!";
}
}
我收到错误:
System.NullReferenceException: Object reference not set to an instance of an object.
public IController Create(RequestContext requestContext, Type controllerType)
Line 98: {
Line 99: return container.GetInstance(controllerType) as IController;
Line 100:
Line 101: }
我是否必须以某种方式手动连接每个控制器类?
我的 global.asax.cs 有:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IContainer container = new Container(
x =>
{
x.Scan(s =>
{
s.AssembliesFromApplicationBaseDirectory();
s.WithDefaultConventions();
s.LookForRegistries();
}
);
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.For<IBar>().Use<Bar>();
}
);
DependencyResolver.SetResolver(new StructuredMapDependencyResolver(container));
}
以及我的结构化地图相关课程:
public class StructuredMapDependencyResolver : IDependencyResolver
{
private IContainer container;
public StructuredMapDependencyResolver(IContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return container.TryGetInstance(serviceType);
}
return container.GetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type servicesType)
{
//return container.GetAllInstances(servicesType) as IEnumerable<object>;
return container.GetAllInstances<object>()
.Where(s => s.GetType() == servicesType);
}
}
public class StructureMapControllerActivator : IControllerActivator
{
private IContainer container;
public StructureMapControllerActivator(IContainer container)
{
container = container;
}
public IController Create(RequestContext requestContext, Type controllerType)
{
return container.GetInstance(controllerType) as IController;
}
}
This is asp.net mvc3.
When I try and go to my home/index action:
public class HomeController : Controller
{
private IBar bar;
public HomeController(IBar bar)
{
this.bar = bar;
}
//
// GET: /Home/
public ActionResult Index()
{
ViewBag.Message = "hello world yo: " + bar.SayHi();
return View();
}
}
public interface IBar
{
string SayHi();
}
public class Bar : IBar
{
public string SayHi()
{
return "Hello from BarImpl!";
}
}
I get the error:
System.NullReferenceException: Object reference not set to an instance of an object.
public IController Create(RequestContext requestContext, Type controllerType)
Line 98: {
Line 99: return container.GetInstance(controllerType) as IController;
Line 100:
Line 101: }
Do I have to somehow manually wire up each and every controller class?
My global.asax.cs has:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IContainer container = new Container(
x =>
{
x.Scan(s =>
{
s.AssembliesFromApplicationBaseDirectory();
s.WithDefaultConventions();
s.LookForRegistries();
}
);
x.For<IControllerActivator>().Use<StructureMapControllerActivator>();
x.For<IBar>().Use<Bar>();
}
);
DependencyResolver.SetResolver(new StructuredMapDependencyResolver(container));
}
And my structured map related classes:
public class StructuredMapDependencyResolver : IDependencyResolver
{
private IContainer container;
public StructuredMapDependencyResolver(IContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
if (serviceType.IsAbstract || serviceType.IsInterface)
{
return container.TryGetInstance(serviceType);
}
return container.GetInstance(serviceType);
}
public IEnumerable<object> GetServices(Type servicesType)
{
//return container.GetAllInstances(servicesType) as IEnumerable<object>;
return container.GetAllInstances<object>()
.Where(s => s.GetType() == servicesType);
}
}
public class StructureMapControllerActivator : IControllerActivator
{
private IContainer container;
public StructureMapControllerActivator(IContainer container)
{
container = container;
}
public IController Create(RequestContext requestContext, Type controllerType)
{
return container.GetInstance(controllerType) as IController;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否检查过哪个对象给您带来了 NullReferenceException ?
看起来您在这里将
container
分配给其自身:因此从未设置成员变量。将构造函数中的行更改为
this.container = container
就可以了。Have you checked which object is giving you the
NullReferenceException
?It looks like you're assigning
container
to itself here:So the member variable is never set. Change the line in the constructor to
this.container = container
and you'll be good to go.