Ich habe ein neues MVC-Webprojekt, in dem ich MVC und WebApi verwende. Ich habe Simple Injector (Version 2.5.2 von nuGet) mit dem folgenden Code in meiner globalen Datei eingerichtetSimple Injector - Kein parameterloser Konstruktor für dieses Objekt definiert
// Register Injectors
SimpleInjectorConfig.Register();
In meinem SimpleInjectorConfig.cs Datei ich habe
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);
}
}
Jetzt habe ich 2 Controller, 1 ein WebAPI Controller und 1 sind ein normaler MVC-Controller.
Mein WebAPI Controller funktioniert gut und sieht aus wie dieses
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 ich den obigen Code funktioniert gut, ich die URL ausführen kann, und es gibt, was ich erwarten würde.
Jetzt habe ich meine MVC-View-Controller, die auf die oben sehr ähnlich sieht, 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();
}
}
Jetzt kann ich nicht verstehen, warum ich die folgende Fehlermeldung erhalte. Ich kann keinen öffentlichen Konstruktor hinzufügen, da dies zu einem Fehler bei SimpleInjector führt.
No parameterless constructor defined for this object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[MissingMethodException: No parameterless constructor 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) +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 mir jemand in die richtige Richtung zeigen könnte, wäre das großartig.
Vielen Dank, funktioniert perfekt jetzt – Gillardo
OMG ich etwa 5 Stunden dafür ausgegeben haben. Danke Retter. –