为什么结构图没有获取我的 HomeController?

发布于 2024-12-04 06:39:27 字数 3254 浏览 2 评论 0原文

这是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落成白 2024-12-11 06:39:27

您是否检查过哪个对象给您带来了 NullReferenceException ?

看起来您在这里将 container 分配给其自身:

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

因此从未设置成员变量。将构造函数中的行更改为 this.container = container 就可以了。

Have you checked which object is giving you the NullReferenceException?

It looks like you're assigning container to itself here:

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

So the member variable is never set. Change the line in the constructor to this.container = container and you'll be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文