gpt4 book ai didi

asp.net - 简单注入(inject)器 - 解析 MVC Controller 时没有为此对象定义无参数构造函数

转载 作者:行者123 更新时间:2023-12-04 02:50:06 26 4
gpt4 key购买 nike

我有一个新的 MVC Web 项目,我在其中使用 MVC 和 Web API。我在全局文件中使用以下代码设置了 Simple Injector(来自 NuGet 的 2.5.2 版)

// Register Injectors
SimpleInjectorConfig.Register();
在我的 SimpleInjectorConfig.cs 文件中,我有
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);
}
}
现在我有 2 个 Controller ,1 个是 Web API Controller ,1 个是普通的 MVC Controller 。
我的 Web API Controller 工作正常,看起来像这样
public class MyApiController : ApiController
{
private IService _service;

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

public IHttpActionResult Get(int id)
{
// I get my entity here and return it
EntityObject myEntity = _service.Get(id);
return Ok(myEntity);
}
}
正如我所说,上面的代码工作正常,我可以执行 URL 并返回我所期望的。
现在我有了我的 MVC Controller ,它看起来与上面的非常相似,这里是
public class MyController : Controller
{
private IService _service;

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

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

public ActionResult Search()
{
return View();
}
}
现在我完全不明白为什么我不断收到以下错误。我无法添加公共(public)构造函数,因为这会导致 Simple Injector 出错。

System.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
如果有人能指出我正确的方向,那就太好了。

最佳答案

出现此错误的原因是您缺少以下注册(如 MVC integration guide 中所述):

DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

MVC 和 Web API 都有自己的解析依赖的抽象(都称为“依赖解析器”)。由于您没有为 MVC 设置解析器,因此 MVC 使用默认解析机制来创建 MVC Controller ,但这需要 Controller 具有默认构造函数。

调用 DependencyResolver.SetResolver将解决问题。

关于asp.net - 简单注入(inject)器 - 解析 MVC Controller 时没有为此对象定义无参数构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25973238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com