Ninject - System.Missingmethodexception: No constructor without parameters has been defined for this object

Asked

Viewed 253 times

0

I’m in the wrong;

No constructor without parameters has been defined for this object.

Description: An untreated exception occurred during the execution of current web request. Examine stack tracking to get more information about the error and where it originated in the code.

Exception Details: System.Missingmethodexception: No constructor no parameters has been set for this object.

Error of Origin:

Untreated exception was generated during the current execution web request. The information related to the origin and location of the exception can be identified by using stack tracking exception below.

Cell Trace:

[Missingmethodexception: No constructor without parameters has been defined for this object.]
System.RuntimeTypeHandle.Createinstance(Runtimetype type, Boolean publicOnly, Boolean noCheck, Boolean & canBeCached, Runtimemethodhandleinternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.Createinstanceslow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Stackcrawlmark & stackMark) +119
System.RuntimeType.Createinstancedefaultctor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Stackcrawlmark & stackMark) +247 System.Activator.Createinstance(Type type, Boolean nonPublic) +83 System.Activator.Createinstance(Type type) +11 System.Web.Mvc.DefaultControllerActivator.Create(Requestcontext requestContext, Type controllerType) +55

[Invalidoperationexception: An error occurred when trying to create a controller of type 'Cartdecompras.UI.Controllers.Orderscontroller'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(Requestcontext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.Getcontrollerinstance(Requestcontext requestContext, Type controllerType) +76
System.Web.Mvc.DefaultControllerFactory.Createcontroller(Requestcontext requestContext, String controllerName) +88
System.Web.Mvc.MvcHandler.Processrequestinit(Httpcontextbase httpContext, Icontroller & controller, Icontrollerfactory & Factory) +191 System.Web.Mvc.MvcHandler.Beginprocessrequest(Httpcontextbase httpContext, Asynccallback callback, Object state) +50
System.Web.Mvc.MvcHandler.Beginprocessrequest(Httpcontext httpContext, Asynccallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.Ihttpasynchandler.Beginprocessrequest(Httpcontext context, Asynccallback cb, Object extraData) +16
System.Web.Callhandlerexecutionstep.System.Web.HttpApplication.Iexecutionstep.Execute() +103 System.Web.Httpapplication.Executestep(Iexecutionstep step, Boolean& completedSynchronously) +155

I’m using Ninject to do Ioc, but I don’t know what I did wrong in the settings.

  [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(CarrinhoDeCompras.UI.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(CarrinhoDeCompras.UI.App_Start.NinjectWebCommon), "Stop")]

namespace CarrinhoDeCompras.UI.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using CarrinhoDeCompras.Infra.CrossCutting.IoC.Modulos;

    public static class NinjectWebCommon
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start()
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Load(new ModulosNinject());
        }
    }
}

And in Modulosninject I have configured all dependencies..

using Ninject.Modules;
using CarrinhoDeCompras.Application.Interfaces;
using CarrinhoDeCompras.Application.Services;
using CarrinhoDeCompras.Domain.Interfaces.Repositorios;
using CarrinhoDeCompras.Domain.Interfaces.Services;
using CarrinhoDeCompras.Domain.Services;
using CarrinhoDeCompras.Infra.Data.EF.Repositorios;

namespace CarrinhoDeCompras.Infra.CrossCutting.IoC.Modulos
{
    public class ModulosNinject : NinjectModule
    {
        public override void Load()
        {
            Bind(typeof(IAppServiceBase<>)).To(typeof(AppServiceBase<>));
            Bind<IAppServiceCategories>().To<AppServiceCategories>();
            Bind<IAppServiceCustomerDemographics>().To<AppServiceCustomerDemographics>();
            Bind<IAppServiceCustomers>().To<AppServiceCustomers>();
            Bind<IAppServiceEmployees>().To<AppServiceEmployees>();
            Bind<IAppServiceOrderDetails>().To<AppServiceOrderDetails>();
            Bind<IAppServiceOrders>().To<AppServiceOrders>();
            Bind<IAppServiceProducts>().To<AppServiceProducts>();
            Bind<IAppServiceRegion>().To<AppServiceRegion>();
            Bind<IAppServiceShippers>().To<AppServiceShippers>();
            Bind<IAppServiceSuppliers>().To<AppServiceSuppliers>();
            Bind<IAppServiceTerritories>().To<AppServiceTerritories>();

            Bind(typeof(IServiceBase<>)).To(typeof(ServiceBase<>));
            Bind<IServiceCategories>().To<ServiceCategories>();
            Bind<IServiceCustomerDemographics>().To<ServiceCustomerDemographics>();
            Bind<IServiceCustomers>().To<ServiceCustomers>();
            Bind<IServiceEmployees>().To<ServiceEmployees>();
            Bind<IServiceOrderDetails>().To<ServiceOrderDetails>();
            Bind<IServiceOrders>().To<ServiceOrders>();
            Bind<IServiceProducts>().To<ServiceProducts>();
            Bind<IServiceRegion>().To<ServiceRegion>();
            Bind<IServiceShippers>().To<ServiceShippers>();
            Bind<IServiceSuppliers>().To<ServiceSuppliers>();
            Bind<IServiceTerritories>().To<ServiceTerritories>();

            Bind(typeof(IRepositorioBase<>)).To(typeof(RepositorioBase<>));
            Bind<IRepositorioCategories>().To<RepositorioCategories>();
            Bind<IRepositorioCustomerDemographics>().To<RepositorioCustomerDemographics>();
            Bind<IRepositorioCustomers>().To<RepositorioCustomers>();
            Bind<IRepositorioEmployees>().To<RepositorioEmployees>();
            Bind<IRepositorioOrderDetails>().To<RepositorioOrderDetails>();
            Bind<IRepositorioOrders>().To<RepositorioOrders>();
            Bind<IRepositorioProducts>().To<RepositorioProducts>();
            Bind<IRepositorioRegion>().To<RepositorioRegion>();
            Bind<IRepositorioShippers>().To<RepositorioShippers>();
            Bind<IRepositorioSuppliers>().To<RepositorioSuppliers>();
            Bind<IRepositorioTerritories>().To<RepositorioTerritories>();
        }
    }
}

My Orderscontroller class

using CarrinhoDeCompras.Application.Interfaces;
using CarrinhoDeCompras.Domain.Entidades;
using System.Web;
using System.Web.Mvc;

namespace CarrinhoDeCompras.UI.Controllers
{
    public class OrdersController : Controller
    {
        private readonly IAppServiceOrders _AppServiceOrders;

        public OrdersController(IAppServiceOrders appService)
        {
            _AppServiceOrders = appService;
        }

        // GET: Orders
        public ActionResult Index()
        {
            var OrdersViewModel = _AppServiceOrders.GetAll();
            return View(OrdersViewModel);
        }

        // GET: Orders/Details/5
        public ActionResult Details(int id)
        {
            var OrderDetailsViewModel = _AppServiceOrders.GetById(id);
            return View(OrderDetailsViewModel);
        }

        // GET: Orders/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Orders/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Orders orders)
        {
            if (ModelState.IsValid)
            {
                _AppServiceOrders.Add(orders);

                return RedirectToAction("Index");
            }

            return View(orders);
        }

        // GET: Orders/Edit/5
        public ActionResult Edit(int id)
        {
            var ordersViewModel = _AppServiceOrders.GetById(id);
            return View(ordersViewModel);
        }

        // POST: Orders/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Orders orders)
        {
            if (ModelState.IsValid)
            {
                _AppServiceOrders.Update(orders);

                return RedirectToAction("Index");
            }
            return View(orders);
        }

        private byte[] SetLogotipo(HttpPostedFileBase logotipo)
        {
            var bytesLogotipo = new byte[logotipo.ContentLength];
            logotipo.InputStream.Read(bytesLogotipo, 0, logotipo.ContentLength);
            return bytesLogotipo;
        }

        // GET: Orders/Delete/5
        public ActionResult Delete(int id)
        {
            var categoriesViewModel = _AppServiceOrders.GetById(id);
            if (categoriesViewModel == null)
            {
                return HttpNotFound();
            }
            return View(categoriesViewModel);
        }

        // POST: /Movies/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _AppServiceOrders.Remove(id);
            return RedirectToAction("Index");
        }
    }
}
  • You can post the Orderscontroller code?

  • I was able to solve the problem was the difference in the versions of Ninject between the UI app and the Infra

  • Fine, man....

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.