如何修复“对象引用未设置为对象的实例”?

时间:2012-07-19 19:28:27

标签: c# asp.net-mvc dependency-injection ninject asp.net-mvc-4

我在visual studio 2012 RC中创建了一个mvc4项目,并使用nuget添加了ninject.mvc3包。它创建了标准的NinjectWebCommon.cs文件,我编辑了Re​​gisterServices方法,如下所示:

private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IProfileRepository>().To<ProfileRepository>().InSingletonScope(); 
        }    

这是我的界面和配置文件存储库类:

public interface IProfileRepository
    {
        void CreateProfile(UserProfile profile);
    }



public class ProfileRepository : IProfileRepository
    {
        private EFDbContext context = new EFDbContext();

        public void CreateProfile(UserProfile userProfile)
        {
            context.UserProfiles.Add(userProfile);
            context.SaveChanges();
        }

    }

我想在我的帐户控制器中访问这个我的IProfileRepository,如下所示:

        private readonly IProfileRepository profileRepository;

        public AccountController(IProfileRepository repo){
            profileRepository = repo;
        }
        [AllowAnonymous]
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus;
                Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    profileRepository.CreateProfile(new UserProfile
                    {
                        UserId = (Guid)Membership.GetUser(HttpContext.User.Identity.Name).ProviderUserKey,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        School = model.School,
                        Major = model.Major
                    });
                    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

调用Object reference not set to an instance of an object对象时出现profileRepostory错误,因此可能没有注入。有谁知道什么是错的?谢谢!

编辑: 这是我的global.asax文件:

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }

1 个答案:

答案 0 :(得分:4)

除非您更改了配置,否则Ninject将抛出ActivationException而不是为依赖项注入null。您应该检查哪个对象确实是null

E.g。允许匿名访问是一个巨大的提示,HttpContext.User是null。