ASP.NET Web API binding with ninject

后端 未结 11 939
Happy的楠姐
Happy的楠姐 2020-12-12 22:46

I have just installed the mvc4 rc update and I am trying to build an api application with little luck.

I am using ninject but cant get my controllers to load. I keep

相关标签:
11条回答
  • 2020-12-12 22:51

    if any anyone is still having problems, please listen for some reason ninject is not working how we would expect with mvc 4. In your web api, you need to write this code

    public DefaultController() : base() { }  
    

    This removes the error saying about no default constructor, then when you need to get your data from the get method write this code:

    public IEnumerable<YourModelGoesHere> Get() 
    {
        return context.YourData;  
    } 
    

    Keep in mind, you will have to access your db class here as well, for instance:

    DefaultConnection context = new DefaultConnection(); 
    
    0 讨论(0)
  • 2020-12-12 22:53

    Hopefully this helps someone else...

    I was having the same issue and it was related to me moving class responsible for registering assembly in charge of initializing controllers. Moved out of web into framework project.

    Using Autofac but same would apply for other containers.

    Was calling:

    builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    

    Which works fine when it's within web application, but threw above exception when moved to framework project as the executing assembly no longer contains the controllers.

    Instead had to update to:

    builder.RegisterApiControllers(Assembly.GetCallingAssembly());
    
    0 讨论(0)
  • 2020-12-12 22:57

    have you registered the container with the frawework? I prefer using autofac, here is an example of how to use autofac with API. http://alexmg.com/post/2012/03/08/Autofac-ASPNET-Web-API-%28Beta%29-Integration.aspx

    Also, Mark Seeman has a good post on DI in general with WebAPI

    http://blog.ploeh.dk/2012/03/20/RobustDIWithTheASPNETWebAPI.aspx

    From Ploeh:

    GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
        t => this.container.Kernel.HasComponent(t) ?
            this.container.Resolve(t) :
            null,
        t => this.container.ResolveAll(t).Cast<object>());
    

    The above has to be performed in the global.asax

    0 讨论(0)
  • 2020-12-12 22:57

    I know its an old post, but i found the solution at some link so sharing here. Hope it helps.

    http://weblogs.asp.net/hajan/archive/2013/03/16/quick-tip-how-to-make-ninject-work-with-asp-net-web-api.aspx?CommentPosted=true#commentmessage

    0 讨论(0)
  • 2020-12-12 23:01

    This generic error message

    Type 'Api.Controllers.ConsumerController' does not have a default constructor

    can also occur if you do not make your constructor public, or the dependency cannot be resolved by the IoC container maybe because of a missing argument.

    The error message is misleading to say the least.

    0 讨论(0)
  • 2020-12-12 23:09

    It seems Ninject didn't throw an exception as it generally does when your IOC dependencies aren't quite set up right. Instead it made it look like I hadn't registered the WebAPI dependency resolver which I certainly did. Here was my solution to this problem but from what I've found it could be MANY DIFFERENT types of setup issues. Just re-check everything in the dependency chain. Hopefully it helps someone!

    The controller:

    public class ContestsController : ApiController
    {
        //Ninject wouldn't inject this CTOR argument resulting in the error
        public ContestsController(IContestEntryService contestEntryService)
        {
    

    The dependency:

    public class ContestEntryService : IContestEntryService
    {
    
        public ContestEntryService(IContestsContext contestsContext)
        {
    

    The incorrect configuration:

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IContestsContext>()
                  .To<ContestsContext>()
                  .InRequestScope();
    
            kernel.Bind(x =>
                x.FromAssembliesMatching("MyNameSpace.*")
                 .SelectAllClasses()
                 .BindAllInterfaces()
             );
    

    The correct configuration:

        private static void RegisterServices(IKernel kernel)
        {
             kernel.Bind(x =>
                x.FromAssembliesMatching("MyNameSpace.*")
                 .SelectAllClasses()
                 .BindAllInterfaces()
             );
    
             kernel.ReBind<IContestsContext>()
                  .To<ContestsContext>()
                  .InRequestScope();
    

    Generally Ninject is pretty good about reporting these sorts of errors so I really got thrown for a loop on this one!

    0 讨论(0)
提交回复
热议问题