I have the error;
No parameterless constructor has been defined for this object.
Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No constructor no parameters has been defined for this object.
Source Error:
Unhandled exception was generated during the execution of the current information. The information relating to the origin and location of the can be identified using the stack trace of exception below.
Battery Tracking:
[MissingMethodException: No parameterless constructor 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 'Shopping Cart.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 do not 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");
}
}
}