【问题标题】:Getting parameterles public constructor error获取参数公共构造函数错误
【发布时间】:2018-08-25 19:09:02
【问题描述】:

尝试创建类型控制器时出错 “聊天机器人控制器”。确保控制器具有 无参数的公共构造函数。

在 System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor 控制器描述符,类型 控制器类型)↵在 System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求)↵在 System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()

当我尝试访问我的 IFeedbackRepository 时,我收到了上述错误。当我在 ChatBotController.cs 中放入构造函数时会发生这种情况

public class ChatBotController : ApiController
{
    IFeedbackRepository _feedbackRepository;
    public ChatBotController(IFeedbackRepository feedbackRepository)
    {
        _feedbackRepository = feedbackRepository;
    }

    [HttpPost]
    public IHttpActionResult PostQuestion([FromBody]string message) //TODO: make sure that webapi will search the message in the body of the http request
    {
        throw new NotImplementedException();
    }
}

我同时使用 MVC 和 Api,我都在 Global.asax 中解决了这两个问题:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    DependencyConfig.RegisterWebApiDependencies();
    DependencyConfig.RegisterMvcDependencies();
}

这是我的 MVC 和 Api 的 DependencyConfig.cs:

public static void RegisterWebApiDependencies()
{
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

    container.Register<IAnswerGenerator, PxlAnswerGenerator>(Lifestyle.Scoped);
    container.Register<ChatBotDbContext>(Lifestyle.Scoped);
    container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));

}

public static void RegisterMvcDependencies()
{
    var container = new Container();

    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

    container.Register<IFeedbackRepository, FeedbackDbRepository>(Lifestyle.Scoped);
    container.Register<ChatBotDbContext>(Lifestyle.Scoped);

    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}

我做错了什么?

【问题讨论】:

  • 似乎第二次调用 SetResolver 取代了第一次。你能注释掉第二个电话,看看会发生什么吗?
  • 错误很明显,你需要一个不带任何参数的构造器:public ChatBotController() {}。看看你的代码,这个构造函数不存在。
  • @Aldert 是的,错误很明显,但它不应该发生。 DI 基础结构应向构造函数提供所需的参数。但在某种程度上,这不会发生
  • @SinanSamet 查看文档中的 simple-injectior 和搜索词 resolver 提供了两个关于如何为 MVC 和 WebAPI 初始化解析器的有趣链接
  • @Steve 现在和你在一起......谢谢你的解释!

标签: asp.net .net simple-injector


【解决方案1】:

根据documentation of Simple-Injector,当您要为您需要调用的注册的WebApi部分初始化解析器时

container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorWebApiDependencyResolver(container));

【讨论】:

    【解决方案2】:

    我没有看到你调用 container.RegisterWebApiControllers(GlobalConfiguration.Configuration);在 RegisterWebApiDependencies() 中。这是必需的。

    您可能需要在此处查看用于与 ASP.NET Web API 和 MVC 集成的 simpleinjector 文档:

    https://simpleinjector.readthedocs.io/en/latest/webapiintegration.html

    此外,上面的文档在 application_start() 的开头有容器/DI 设置。如果上述更改单独不起作用,您可能需要尝试将以下两行放在 application_start() 的开头:

    DependencyConfig.RegisterWebApiDependencies(); DependencyConfig.RegisterMvcDependencies();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      相关资源
      最近更新 更多