没有为此对象MVC4定义的无参数构造函数

时间:2013-10-17 19:14:24

标签: c# c#-4.0 dependency-injection inversion-of-control castle-windsor

这个问题已经问了这么多次,还试过并为我的模型类创建了默认的构造函数,但仍然得到了错误。我的代码行是多少,我想知道是否有人可以帮我修改代码..

public class RecordController: Controller
    {
        private readonly IRecord  _DataService = null;

        public RecordController(IRecord dataService )
        {
            _DataService = dataService ;
        }

        [HttpGet]
        public ActionResult TestRecord()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TestRecord(TestRecordModel model)
        {         

            return View();
        }

    }

以下是我的TestRecordModel类

public class TestRecordModel 
        {
            [Required]
            [Display(Name = "UserNo #:)]
            public string UserNo { get; set; }

        }

下面是我的引导程序,WindsorControllerActivator和ControllerInstaller

 public class Bootstrapper
    {
        #region Properties

        public static IWindsorContainer Container { get; private set; }

        #endregion

        /// <summary>
        /// Initialises this instance.
        /// </summary>
        public static void RegisterAllTypes()
        {
            // adds and configures all components using WindsorInstallers from executing assembly.
            Container = new WindsorContainer().Install(FromAssembly.InThisApplication());

            Container.Register(Component.For<IViewEngine>().ImplementedBy<RazorViewEngine>());
            Container.Register(Component.For<IControllerFactory>().ImplementedBy<WindsorControllerFactory>());
            Container.Register(Component.For<IControllerActivator>().ImplementedBy<WindsorControllerActivator>());
            Container.Register(Component.For<IHttpControllerActivator>().ImplementedBy<WindsorHttpControllerActivator>());

            DependencyResolver.SetResolver(new WindsorDependencyResolver(Container.Kernel));

            GlobalConfiguration.Configuration.DependencyResolver = new WindsorHttpDependencyResolver(Container.Kernel);
        }

        public static void RegisterType<TContract, TImplementation>(params KeyValuePair<string, string>[] parameters)
            where TContract : class
            where TImplementation : TContract
        {
            var dependencies = parameters
                .Select(parameter => Parameter.ForKey(parameter.Key).Eq(parameter.Value))
                .Select(dummy => (Dependency)dummy).ToArray();

            Container.Register(Component.For<TContract>()
                                   .ImplementedBy<TImplementation>()
                                   .DependsOn(dependencies));
        }

        public static TContract Resolve<TContract>()
        {
            return Container.Resolve<TContract>();
        } 

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        public static void Dispose()
        {
            // clean up, application exits.
            if (Container != null)
                Container.Dispose();
        }
    }





 public class WindsorControllerActivator : IControllerActivator
    {
        #region Private Members

        private readonly IKernel _kernel;

        #endregion

        #region Constructor(s)

        /// <summary>
        /// Initializes a new instance of the <see cref="WindsorControllerActivator" /> class.
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        public WindsorControllerActivator(IKernel kernel)
        {
            _kernel = kernel;
        }

        #endregion

        #region IControllerActivator Members

        /// <summary>
        /// When implemented in a class, creates a controller.
        /// </summary>
        /// <param name="requestContext">The request context.</param>
        /// <param name="controllerType">The controller type.</param>
        /// <returns>
        /// The created controller.
        /// </returns>
        public IController Create(RequestContext requestContext, Type controllerType)
        {
            return (IController)_kernel.Resolve(controllerType);
        }

        #endregion
    }







public class ControllerInstaller : InstallerBase
    {
        /// <summary>
        /// Performs the installation in the <see cref="T:Castle.Windsor.IWindsorContainer" />.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <param name="store">The configuration store.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public override void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Classes.FromAssemblyInDirectory(new AssemblyFilter(GetExecutingDirectory()))
                    .BasedOn<IController>()
                    .LifestyleTransient());
        } 
    }

2 个答案:

答案 0 :(得分:0)

也许如果您尝试使用serialize或Activator.CreateInstance(t),那么您将收到有关无参数构造函数的错误。

答案 1 :(得分:0)

当mvc框架创建控制器时,您获得的错误是标准错误。我认为你的注册/引导程序没有被正确调用。 在WindsorControllerActivator中设置一个断点,看它是否被调用。