Einfache Injektor - Keine parameterlosen Konstruktor für dieses Objekt definiert

Habe ich ein neues MVC-Web-Projekt, das ich bin usin MVC und WebApi in. Ich habe eine Einfache Inbetriebnahme Injektor (version 2.5.2 von nuGet) mit dem folgenden code in meine global-Datei

//Register Injectors
SimpleInjectorConfig.Register();

In meinem SimpleInjectorConfig.cs-Datei habe ich

public class SimpleInjectorConfig
{
    public static void Register() {
        //Create the container as usual.
        Container container = new Container();

        //services
        container.Register<IService, MyService>();

        //data
        container.Register<IRepository, MyRepository>();

        //Register your types, for instance using the RegisterWebApiRequest
        //extension from the integration package:
        container.RegisterMvcControllers(
            System.Reflection.Assembly.GetExecutingAssembly());

        container.RegisterMvcAttributeFilterProvider();

        //This is an extension method from the integration package.
        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

        //verify its all ok
        container.Verify();

        //add dependency
        GlobalConfiguration.Configuration.DependencyResolver = 
            new SimpleInjectorWebApiDependencyResolver(container);
    }
}

Nun habe ich 2 Controller, 1 webApi controller und 1 normalen MVC-controller.

Meinen WebApi controller funktioniert einwandfrei und sieht aus wie diese

public class MyApiController : ApiController
    {
        private IService _service;

        public MyApiController(IService service)
        {
            _service = service;
        }

        ///<summary>
        ///GET api/<controller>/5
        ///</summary>
        ///<param name="id"></param>
        ///<returns></returns>
        public IHttpActionResult Get(int id)
        {
            //i get my entity here and return it
            EntityObject myEntity = _service.Get(id);
            return Ok(myEntity);
        }
    }

Wie gesagt der obige code funktioniert einwandfrei, ich kann führen Sie die url, und es gibt das, was ich erwarten würde.

Nun habe ich mein MVC-view-controller, das sieht sehr ähnlich wie oben, hier ist es

public class MyController : Controller
{
    private IService _service;

    public MyController(IService service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        return Search();
    }

    //Company/Search
    public ActionResult Search()
    {
        return View();
    }
}

Ich nun überhaupt nicht verstehen kann, warum ich immer die folgende Fehlermeldung. Kann ich nicht ein public contructor als dass ein Fehler verursacht wird, mit SimpleInjector.

System.MissingMethodException: Keine parameterlosen Konstruktor für dieses Objekt definiert.

   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) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +66
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +110

[InvalidOperationException: An error occurred when trying to create a controller of type 'my.project.Web.Controllers.MyController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +257
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +328
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +157
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Wenn jemand könnte mich in die richtige Richtung, das wäre toll.

InformationsquelleAutor Gillardo | 2014-09-22
Schreibe einen Kommentar