Search code examples
c#.netasp.net-web-apininjectowin

Parameterless constructor issue within webapi application using owin and ninject


I have a web api application in which I'd like to use Owin, Oauth and Ninject , So I have this configuration

Dependency injector

public class NinjectDependencyResolver : IDependencyResolver
{
    private static IKernel kernel; 

    public NinjectDependencyResolver()
    {
        kernel = new StandardKernel();
       // AddBindings();
    }

    public object GetService(Type serviceType)
    {
        return kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return kernel.GetAll(serviceType);
    }

    private static void AddBindings()
    {
        kernel.Bind<INotifier>().To<Notifier>();
        kernel.Bind<IEventRepository>().To<EventRepository>();
        kernel.Bind<ICrud<Config>>().To<CrudConfig>();
        kernel.Bind<ICrud<Evenement>>().To<CrudEvent>();
        kernel.Bind<IAccount>().To<Account>();
    }



    public static Lazy<IKernel> CreateKernel = new Lazy<IKernel>(() =>
    {
        //var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
        AddBindings();

        return kernel;
    });


}

StartUp class

public partial class Startup
{
    public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
    public static string PublicClientId { get; private set; }

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public static void ConfigureAuth(IAppBuilder app)
    {
        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Configure the application for OAuth based flow
        PublicClientId = "self";
        OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            // In production mode set AllowInsecureHttp = false
            AllowInsecureHttp = true
        };

        // Enable the application to use bearer tokens to authenticate users
        app.UseOAuthBearerTokens(OAuthOptions);

        //app.UseNinjectMiddleware(CreateKernel);
    }

    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseNinjectMiddleware(() => NinjectDependencyResolver.CreateKernel.Value);
        app.UseNinjectWebApi(config);
        ConfigureAuth(app);
    }
}

Global.cs

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

I have this class also

 public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
        }        
    }

and I have this api controller :

public class AccountController : BaseController
{
    #region ctors

    public AccountController()
    {

    }
    [Inject]
    public AccountController(INotifier _notifierParam,  IAccount _IAccount)
    {
        Notifier = _notifierParam; 
        Account = _IAccount;
    }

    #endregion
}

which derived from BaseController which is a class with no contructor.

the problem is when I make a call to service of the account controller I get this exception :

"Message":"An error has occurred.", "ExceptionMessage":"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.", "ExceptionType":"System.InvalidOperationException", "StackTrace":" à System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n à System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n à System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()", "InnerException":{ "Message":"An error has occurred.", "ExceptionMessage":"Error activating INotifier\r\nNo matching bindings are available, and the type is not self-bindable.\r\nActivation path:\r\n 4) Injection of dependency INotifier into parameter _notifierParam of constructor of type AccountController\r\n 3) Injection of dependency AccountController into parameter resolutionRoot of constructor of type NamedScope\r\n 2) Injection of dependency NamedScope into parameter resolutionRoot of constructor of type OwinNinjectDependencyResolver\r\n 1) Request for IDependencyResolver\r\n\r\nSuggestions:\r\n 1) Ensure that you have defined a binding for INotifier.\r\n 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.\r\n 3) Ensure you have not accidentally created more than one kernel.\r\n 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.\r\n 5) If you are using automatic module loading, ensure the search path and filters are correct.\r\n", "ExceptionType":"Ninject.ActivationException", "StackTrace":" à Ninject.KernelBase.Resolve(IRequest request)\r\n à Ninject.Planning.Targets.Target1.GetValue(Type service, IContext parent)\r\n à Ninject.Planning.Targets.Target1.ResolveWithin(IContext parent)\r\n
à Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)\r\n à Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.b__2(ITarget target)\r\n à System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext()\r\n à System.Linq.Buffer1..ctor(IEnumerable1 source)\r\n à System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source)\r\n à Ninject.Activation.Providers.StandardProvider.Create(IContext context)\r\n à Ninject.Activation.Context.ResolveInternal(Object scope)\r\n à Ninject.Activation.Context.Resolve()\r\n à Ninject.KernelBase.<>c__DisplayClass15.b__f(IBinding binding)\r\n à System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()\r\n à System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source)\r\n à Ninject.Web.WebApi.NinjectDependencyScope.GetService(Type serviceType)\r\n à System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n à System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"} }

So I need to know :

  1. What is the reason of this error?
  2. How can I fix it?

Solution

  • You are mixing Dependency Injection approaches. Your are using OWIN for setting the ninject middleware for Web API but then you are managing Web API outside of OWIN (using the IIS pipeline):

    protected void Application_Start()
    {
        //..
        GlobalConfiguration.Configure(WebApiConfig.Register);
        //..
    }
    

    The second mistake is that you are creating multiple Kernels, and your exception message is warning you to avoid this:

    Ensure you have not accidentally created more than one kernel
    

    Start by refactoring your code. Manage Web API using only OWIN:

    public void Configuration(IAppBuilder app)
    {
        var config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseNinjectMiddleware(() => NinjectDependencyResolver.CreateKernel.Value);
        ConfigureAuth(app);
        app.UseNinjectWebApi(config);
    }
    

    and remove this line from Global.asax.cs:

    GlobalConfiguration.Configure(WebApiConfig.Register);
    

    Then ensure you are creating only one Kernel for the entire application lifetime. You should have only one static instance of the Kernel, and reference it from any other object that needs it.