Search code examples
.netasp.net-web-apininjectowin

Possible OWIN Startup and Ninject concurrent request issue ?


I have created ASP.NET WebAPI .NET framework 4.5. Ninject 3.2.00 MicroSoft.OWIN 3.0.10

API deployed to IIS-7

Our OWIN startup class looks like this :

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var config = GlobalConfiguration.Configuration;

        WebApiConfig.Register(config);
        NinjectConfig.Register(app, config);
    }
}

And NinjectConfig.cs Look like

public class NinjectConfig
{
    public static void Register(IAppBuilder app, HttpConfiguration config)
    {
        app.UseNinjectMiddleware(CreateKernel)
            .UseNinjectWebApi(config);
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();

        kernel.Load("XYZ.*.dll");

        return kernel;
    }
}

Facing the following error...

If at least two simultaneous calls are made to the api after an AppPool refresh, one of the two simultaneous calls fails after about a second with an error and the other succeeds after 3 seconds of startup. All subsequent calls succeed, parallel or in sequence.

The problem does NOT occur when a single request is made after recycling the AppPool

Stacktrace:

{
"Message":"An error has occurred.",
"ExceptionMessage":"An error occurred when trying to create a controller of type 'TestController'. 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__1.MoveNext()",
"InnerException":{
    "Message":"An error has occurred.",
    "ExceptionMessage":"Type 'Test.XYZ.Api.Controllers.TestController' 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)"
}

We are wondering if this is some kind of concurrency issue while loading Ninject dependencies. Can anyone help us with what the issue could be, or if there is something wrong with our Startup class ?


Solution

  • I fixed a similar issue using the code mentioned at https://stackoverflow.com/a/35048937

    IKernel kernel = CreateKernel();
    
    app.UseNinjectMiddleware(() => kernel)
                .UseNinjectWebApi(config);
    

    You can try the above code in your NinjectConfig.Register()