为什么结构图没有拿起我的HomeController?

时间:2011-09-13 03:00:05

标签: c# asp.net-mvc structuremap

这是asp.net mvc3。

当我尝试去我的家/索引行动时:

    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;

        }
    }

1 个答案:

答案 0 :(得分:2)

您是否检查了哪个对象为您提供了NullReferenceException

您好像在这里为自己分配container

private IContainer container;
public StructureMapControllerActivator(IContainer container)
{
    container = container;
}

所以永远不会设置成员变量。将构造函数中的行更改为this.container = container,你就可以了。