mitomed
mitomed

Reputation: 2066

Still ninject in web api

although there's a lot of questions round here about the same and I'm trying to follow this post that has been advised several times I can't make it work

http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

I'm getting this error

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
An error occurred when trying to create a controller of type 'ProfileController'. Make sure that the controller has a parameterless public constructor.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.<>c__DisplayClass2.<System.Web.Http.Dispatcher.IHttpControllerActivator.Create>b__0() at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace) at System.Web.Http.Tracing.Tracers.HttpControllerActivatorTracer.System.Web.Http.Dispatcher.IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.<>c__DisplayClass2.<CreateController>b__0() at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace) at System.Web.Http.Tracing.Tracers.HttpControllerDescriptorTracer.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()
</StackTrace>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'LoanBook.Api.Controllers.ProfileController' does not have a default constructor
</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
</InnerException>
</Error>

I don't know if this post is still relevant or there's another way of make this work. I'm working with 2012 but using web api2 in 4.5. The Ninject.Web.WebApi package has just released unstable versions yet so I'm not sure if it's an option.

EDIT: By the way, the solution is not place in the same project as an MVC one. Maybe that's an issue

Thanks

EDIT: The controller looks like this

public class ProfileController : BaseController
{
    private readonly IPersonService _personService;

    public ProfileController(IPersonService personService)
    {
        _personService = personService;
    }

    //.. Methods

}

The error is in the NinjectDependencyResolver shown in the post I mentioned, in this bit

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);
}

When it get to the return line

Upvotes: 2

Views: 836

Answers (1)

jim tollan
jim tollan

Reputation: 22485

It looks like the error is telling you that you need to create a parameterless constructor in the controller, this could be due to ninject RegisterServices() not being setup correctly.

You need the following to sort the error:

public ProfileController () { }

I'm therefore assuming that you have the required ioc constructor along the lines of:

public ProfileController (IPersonService personService) 
{
    _personService = personService;
}

[edit] - so i'd expect your NinjectWebCommon.cs (located in App_Start) to contain the following for everything to work:

private static void RegisterServices(IKernel kernel)
{
    /* other kernel.bind<>() setups */
    // assumes that your concrete class implementation
    // of IPersonService is called PersonService
    kernel.Bind<IPersonService>().To<PersonService>().InRequestScope();
}

hope this helps...

Upvotes: 1

Related Questions