0

I'm creating a Web Api 2 application in ASP.NET. I've done Authroization using JWT tokens and Owin middleware. Now, while generating tokens, I need to first check the user credentials through DB. For this I need Constructor dependency injection. What I've done so far is:

Owin partial startup class

[assembly: OwinStartup(typeof(CampBookingApplication.App_Start.Startup))]

namespace CampBookingApplication.App_Start
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var kernel = ConfigureNinject(app);
            ConfigureAuth(app, kernel);
        }

        public void ConfigureAuth(IAppBuilder app, IKernel kernel)
        {
            app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            app.UseOAuthAuthorizationServer(kernel.Get<MyOAuthProvider>().GetOptions());
            app.UseJwtBearerAuthentication(new MyJwtOptions());
        }
    }
}

Startup.Ninject class

namespace CampBookingApplication.App_Start
{
    public partial class Startup
    {
        public IKernel ConfigureNinject(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            var kernel = CreateKernel();
            app.UseNinjectMiddleware(CreateKernel);
            app.UseNinjectWebApi(config);
            return kernel;
        }

        public IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                RegisterServices(kernel); return kernel;
            }
            catch
            {
                kernel.Dispose(); throw;
            }
        }

        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IUserService>().To<UserService>().InSingletonScope();
            kernel.Bind<ICampService>().To<CampService>().InSingletonScope();
            kernel.Bind<IBookingService>().To<BookingService>().InSingletonScope();
        }
    }
}

While hitting the APIs, I'm always getting the error:

"An error occurred when trying to create a controller of type 'CampsController'. Make sure that the controller has a parameterless public constructor.",

It means I'm unable to register my bindings. What am I doing wrong?

I've searched for this alot but not able to solve this issue.

UPDATE 1:
I'm attaching the entire exception traceback with the controller code.

Entire Exception Traceback:

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "An error occurred when trying to create a controller of type 'CampsController'. Make sure that the controller has a parameterless public constructor.",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()",
    "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Type 'CampBookingApplication.Controllers.CampsController' does not have a default constructor",
        "ExceptionType": "System.ArgumentException",
        "StackTrace": "   at System.Linq.Expressions.Expression.New(Type type)\r\n   at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"
    }
}

CampsController:

public class CampsController : ApiController
    {
        private readonly ICampService _campService;

        public CampsController(ICampService campService)
        {
            _campService = campService;
        }


        [HttpGet]
        [Authorize]
        public IHttpActionResult GetAllCamps()
        {
            List<CampDTO> camps = _campService.GetAllCamps();
            if (camps.Count == 0)
            {
                return NotFound();
            }
            return Ok(camps);
        }
        ....
    }
Prachi Sharma
  • 331
  • 1
  • 5
  • 14
  • 1
    That error could mean lots of things. My guess is Ninject is unable to resolve a dependency in the `CampsController`. Are you able update your question with your controller (removing sensitive information if needed)? Can you post the entire exception stack? – Stack Undefined Apr 26 '20 at 18:43
  • @sanmcp I've added both. Please check – Prachi Sharma Apr 26 '20 at 18:53
  • Similar issue. Follow this thread - https://stackoverflow.com/questions/34278162/asp-net-api-2-ninject-and-owin-oauth-configuration – Vinay Jan 11 '21 at 23:18

0 Answers0