3

This is my controller

 public class SuggestionController : ApiController
{
    public ISuggestionRepository Repository { get; private set; }

    public SuggestionController(ISuggestionRepository repository)
    {
        this.Repository = repository;
    }

    // to post suggestion
    [HttpPost]
    [ActionName("PostSuggestion")]
    public HttpResponseMessage PostSuggestion(Suggestion suggestion)
    {
        var answerCorrect = this.Repository.CreateSuggestion(suggestion);

        if (answerCorrect == true)
            return Request.CreateResponse(HttpStatusCode.OK);
        else
            return Request.CreateResponse(HttpStatusCode.Conflict);
    }
}

and this is my RegisterServices method in NinjectWebCommon.cs

private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ICompetitionRepository>().To(typeof(CompetitionRepository))
            .WithConstructorArgument("serviceContext", new InMemoryDataContext<Competition>());

        kernel.Bind<ISubmissionRepository>().To(typeof(SubmissionRepository))
            .WithConstructorArgument("serviceContext", new InMemoryDataContext<Submission>());

        kernel.Bind<IUserRepository>().To(typeof(UserRepository))
            .WithConstructorArgument("serviceContext", new InMemoryDataContext<User>());

        kernel.Bind<ISuggestionRepository>().To(typeof(SuggestionRepository))
           .WithConstructorArgument("serviceContext", new InMemoryDataContext<Suggestion>());
    } 

But am getting an exception that my suggestion controller does not have a default constructor and its showing a 500 internal server when am hitting the controller from a client app

I know that we get the exception of controller not having default constructor if the ninject dependency is not working properly but the below is another controller i have implemeneted similar to suggestion controller and its working absolutely fine.

 public IUserRepository Repository { get; private set; }

    public SSOController(IUserRepository repository)
    {
        this.Repository = repository;
    }

    [HttpPost]
    [ActionName("PostUser")]
    public HttpResponseMessage PostUser([FromBody]string id)
    {
        var accessToken = id;
        var client = new FacebookClient(accessToken);
        dynamic result = client.Get("me", new { fields = "name,email" });
        string name = result.name;
        string email = result.email;


        var existingUser = this.Repository.FindByUserIdentity(name);

        if (existingUser == null)
        {
            var newUser = new User
            {
                Username = name,
                Email = email,

            };

            var success = this.Repository.CreateAccount(newUser);

            if (!success)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError);
            }

            //return created status code as we created the user
            return Request.CreateResponse<User>(HttpStatusCode.Created, newUser);
        }

        return Request.CreateResponse(HttpStatusCode.OK);

    }

}

I have no idea where am going wrong. Please let me know if u have any suggestions.

EDIT:

my Global.asax

 public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        AuthConfig.RegisterAuth();

        GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =
IncludeErrorDetailPolicy.Always;

    }

Dependency resolver am using

 // Provides a Ninject implementation of IDependencyScope
// which resolves services using the Ninject container.
public class NinjectDependencyScope : IDependencyScope
{
    IResolutionRoot resolver;

    public NinjectDependencyScope(IResolutionRoot resolver)
    {
        this.resolver = resolver;
    }

    public object GetService(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.TryGet(serviceType);
    }

    public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
    {
        if (resolver == null)
            throw new ObjectDisposedException("this", "This scope has been disposed");

        return resolver.GetAll(serviceType);
    }

    public void Dispose()
    {
        IDisposable disposable = resolver as IDisposable;
        if (disposable != null)
            disposable.Dispose();

        resolver = null;
    }
}

// This class is the resolver, but it is also the global scope
// so we derive from NinjectScope.
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
    IKernel kernel;

    public NinjectDependencyResolver(IKernel kernel)
        : base(kernel)
    {
        this.kernel = kernel;
    }

    public IDependencyScope BeginScope()
    {
        return new NinjectDependencyScope(kernel.BeginBlock());
    }
}

and calling it in CreateKernel() method in NinjectWebCommon

 private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);

        // Install our Ninject-based IDependencyResolver into the Web API config
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        return kernel;
    }

Suggestion Repository

 public class SuggestionRepository : Repository<Suggestion>, ISuggestionRepository
{
    public SuggestionRepository(IServiceContext<Suggestion> servicecontext)
        : base(servicecontext)
    { }

    public bool CreateSuggestion(Suggestion suggestion)
    {
        this.ServiceContext.Create(suggestion);
        this.ServiceContext.Save();

        return true;
    }
}

ISuggestionRepository

public interface ISuggestionRepository
{
    bool CreateSuggestion(Suggestion suggestion);

}

Repository

public abstract class Repository<T>
{
    public IServiceContext<T> ServiceContext { get; private set; }

    public Repository(IServiceContext<T> serviceContext)
    {
        this.ServiceContext = serviceContext;
    }
}

IserviceContext

 public interface IServiceContext<T>
{
    IQueryable<T> QueryableEntities { get; }

    void Create(T entity);

    void Update(T entity);

    void Delete(T entity);

    void Save();
}
Bitsian
  • 2,238
  • 5
  • 37
  • 72
  • Could you paste your global.asax as well? – Kristof Oct 26 '12 at 05:52
  • @Kristof - nothing in global.asax should affect this – Erik Funkenbusch Oct 26 '12 at 05:54
  • this is a common error with registering DI containers. Look at `DependencyResolver` and how you can register your container as the dependency resolver. – DarthVader Oct 26 '12 at 05:55
  • I can imagine at least a few ways to trigger this behavior in the OnBeginRequest. Point is that everything pasted looks fine to me so i'm guessing the error is in 1 of the other files ;) – Kristof Oct 26 '12 at 05:56
  • look at this. http://stackoverflow.com/questions/4358395/mvc3-ninject-how-to this should do what you want. – DarthVader Oct 26 '12 at 05:56
  • I have added the dependency resolver am using and the global.asax as well.....please let me know if u find any faults – Bitsian Oct 26 '12 at 06:04
  • The thing which boggles me is its working fine for other similar controllers in the solution but not specifically for this... :( – Bitsian Oct 26 '12 at 06:05
  • FYI, not your problem, but I would not make the Repository a property, but rather a private readonly field in your class. I also don't see why you're using the typeof and WithConstructorArgument methods. You should just be doing `.To()` and then have a binding for the datacontext (which I can't offer a suggestion on because you did not include that code) – Erik Funkenbusch Oct 26 '12 at 06:40
  • 1
    You can bind to generics for your `IServiceContext<>` argument, you would do this: `kernel.Bind(typeof(IServiceContext<>)).To(typeof(InMemoryDataContext<>))`, this gets rid of the nasty WithConstructorArgument syntax, and lets you use the generic `.To<>()` – Erik Funkenbusch Oct 26 '12 at 07:04
  • @Bitsian - If you implement the suggestions I've made (which may actually fix the problem, because they use better bindings) and it still doesn't work, please update the question with the stack trace from the exception you get. – Erik Funkenbusch Oct 26 '12 at 07:13
  • @MystereMan Thanks a lot!! after using your bindings, it works :D – Bitsian Oct 26 '12 at 10:05
  • @Bitsian - if my answer helped, then you should mark it as answered (click the check mark next to the answer). I'll update it to include the more recent information. – Erik Funkenbusch Oct 26 '12 at 15:44

4 Answers4

2

Since you're using WebApi, you will need to use the WebApi extension for Ninject. Unfortunately, the current Ninject.WebApi nuget package is out of date, and doesn't work with the released version of WebApi.

Temporarily, until Remo gets around to updating Ninject.WebApi to the release version, you can use Ninject.WebApi-RC http://nuget.org/packages/Ninject.Web.WebApi-RC

http://www.eyecatch.no/blog/2012/06/using-ninject-with-webapi-rc/

EDIT:

To recap the information discussed in comments, Here are the recommendations:

1) Use Ninject.MVC3 and Ninject.Web.WebApi (but use Ninject.Web.WebApi-RC until the official is updated) as discussed above. Do not use a custom DependencyResolver, and let Ninject.Web.Mvc and .WebApi do their job.

2) Change your bindings to this:

kernel.Bind<ICompetitionRepository>().To<CompetitionRepository>();
... similar bindings

3) Add a generic binding for your ServiceContext

kernel.Bind(typeof(IServiceContext<>)).To(typeof(InMemoryDataContext<>));
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I installed Ninject.WebApi-RC and tried again but the same error is coming :( – Bitsian Oct 26 '12 at 06:12
  • @Bitsian - Wherever it was you created it! You list your dependency resolver up above, but you don't use that when using Ninject.MVC3 and Ninject.WebApi – Erik Funkenbusch Oct 26 '12 at 06:17
  • sorry i was thinking about something else when i asked that stupid question......i removed statement where i was installing the custom dependency resolver but still its not working!! I am not sure if the problem is with ninject, because its working fine for other controllers....? – Bitsian Oct 26 '12 at 06:27
  • @Bitsian - what do SuggestionRepository and ISuggestionRepository look like? – Erik Funkenbusch Oct 26 '12 at 06:38
0

I think the problem is you're using the ApiController. Controllers and apiControllers are using a different dependancy injection container. Both of them however expose the same methods.
If the working controller is inheriting the Controller class then that's your cause.
For a work around take a look at this topic

Community
  • 1
  • 1
Kristof
  • 3,267
  • 1
  • 20
  • 30
0

I have faced the same issue.

This is how I rectified: I created a WebContainerManager which is just a static wrapper around container.

Static container wrappers useful when you don't control instantiation and can't rely on injection - e.g. action filter attributes

public static class WebContainerManager
{
    public static IKernel GetContainer()
    {
        var resolver = GlobalConfiguration.Configuration.DependencyResolver as NinjectDependencyResolver;
        if (resolver != null)
        {
            return resolver.Container;
        }

        throw new InvalidOperationException("NinjectDependencyResolver not being used as the MVC dependency resolver");
    }

    public static T Get<T>()
    {
        return GetContainer().Get<T>();
    }
}

Inside your controller, call your empty constructor like this with no parameters:

public SuggestionController() : this(WebContainerManager.Get<ISuggestionRepository>())
{

}

This should work.

This technique i got from the book on MVC4 by Jamie Kurtz @jakurtz.

Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
jaxxbo
  • 7,314
  • 4
  • 35
  • 48
-1

You probably need to do some dependency injection so you can inject the ISuggestionRepository parameter on your SuggestionController constructor. To do that you need to override methods in the DefaultControllerFactory class to customize the creation of controllers. Since you are using NInject, you can have something like:

public class NInjectControllerFactory : DefaultControllerFactory 
{
    private IKernel kernel = new StandardKernel(new CustomModule());

    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return controllerType == null ? null : (IController)kernel.Get(controllerType);  
    }

    public class CustomModule : NinjectModule
    {
        public override void Load()
        {
            this.Bind<ICompetitionRepository>().To(typeof(CompetitionRepository))
                .WithConstructorArgument("serviceContext", new InMemoryDataContext<Competition>());

            this.Bind<ISubmissionRepository>().To(typeof(SubmissionRepository))
                .WithConstructorArgument("serviceContext", new InMemoryDataContext<Submission>());

            this.Bind<IUserRepository>().To(typeof(UserRepository))
                .WithConstructorArgument("serviceContext", new InMemoryDataContext<User>());

            this.Bind<ISuggestionRepository>().To(typeof(SuggestionRepository))
               .WithConstructorArgument("serviceContext", new InMemoryDataContext<Suggestion>());
        }
    }
}

Then in your Global.asax.cs, you can add a line to swap out the controller factory

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterRoutes(RouteTable.Routes);
    ControllerBuilder.Current.SetControllerFactory(new NInjectControllerFactory());  
}
John Gathogo
  • 4,495
  • 3
  • 32
  • 48
  • Ninject module and DefaultControllerFactory are in which package? I am getting errors at these... – Bitsian Oct 26 '12 at 05:52
  • i am using thes two packages... using Ninject; using Ninject.Syntax; – Bitsian Oct 26 '12 at 05:52
  • The asker is using Ninject.MVC3, which uses the WebActivator system from Microsoft. It already hooks itself up and doesn't need a controller factory because it makes use of the IDependencyResolver built into MVC3+ – Erik Funkenbusch Oct 26 '12 at 05:53
  • `ControllerFactory` doesnt work with MVC3 and 4. You need to use `DependencyResolver` – DarthVader Oct 26 '12 at 05:56
  • if you dont know where you are doing wrong. you should undersatnd it before using it. Obviously as DependencyResolver, you need to register ninject DependencyResolver. You dont have that in Global.asax – DarthVader Oct 26 '12 at 06:02
  • @DarthVader - You shouldn't have that in global.asax, Ninject.MVC3 does that for you using the WebActivator system. – Erik Funkenbusch Oct 26 '12 at 06:05
  • I use structuremap and as far as i see from other posts, it is about DependencyResolver, webapi and mvc3 dont use the same DependencyResolver. – DarthVader Oct 26 '12 at 06:13
  • @DarthVader - then perhaps you should stop making suggestions about Ninject when you don't use it. It's done differently in Ninject than it's done in StructureMap, which will automatically register resolvers when the correct nuget packages are installed. – Erik Funkenbusch Oct 26 '12 at 06:17
  • @MystereMan i guess that a good reason not to use ninject. look at other post on SO. – DarthVader Oct 26 '12 at 06:25
  • @MystereMan http://www.codeproject.com/Articles/412383/Dependency-Injection-in-asp-net-mvc4-and-webapi-us suggests the same thing. and it should be the standard way. – DarthVader Oct 26 '12 at 06:26
  • @DarthVader - Look at the first comment to the article. It references this answer http://stackoverflow.com/questions/10311450/how-do-i-work-with-ninject-in-an-asp-net-mvc-web-app/10316885#10316885 which is by Remo himself (the current primary author of Ninject) – Erik Funkenbusch Oct 26 '12 at 06:51
  • @DarthVader - Sorry, second comment (my comment is now listed first) – Erik Funkenbusch Oct 26 '12 at 06:57