13

I am using Ninject on my MVC 3 project deployed on appharbor. I noticed that I get an exception when the application is started, and it looks like something inside Ninject is the cause, but I cannot find any answers out there - so please help me :)

Will try to add the complete exception here:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   NewRelic.Agent.Core.AgentShim.GetTracer(String tracerFactoryName, UInt32 level, String metricName, String assemblyName, String className, String methodName, String argumentSignature, Object invocationTarget) +51
   System.AppDomain.CreateDomain(String friendlyName, Evidence securityInfo, AppDomainSetup info) +99
   Ninject.Modules.AssemblyNameRetriever.CreateTemporaryAppDomain() in c:\Projects\Ninject\ninject\src\Ninject\Modules\AssemblyNameRetriever.cs:68
   Ninject.Modules.AssemblyNameRetriever.GetAssemblyNames(IEnumerable`1 filenames, Predicate`1 filter) in c:\Projects\Ninject\ninject\src\Ninject\Modules\AssemblyNameRetriever.cs:47
   Ninject.Modules.CompiledModuleLoaderPlugin.LoadModules(IEnumerable`1 filenames) in c:\Projects\Ninject\ninject\src\Ninject\Modules\CompiledModuleLoaderPlugin.cs:81
   Ninject.Modules.ModuleLoader.LoadModules(IEnumerable`1 patterns) in c:\Projects\Ninject\ninject\src\Ninject\Modules\ModuleLoader.cs:60
   Ninject.KernelBase.Load(IEnumerable`1 filePatterns) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:236
   Ninject.KernelBase..ctor(IComponentContainer components, INinjectSettings settings, INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:97
   Ninject.KernelBase..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\KernelBase.cs:57
   Ninject.StandardKernel..ctor(INinjectModule[] modules) in c:\Projects\Ninject\ninject\src\Ninject\StandardKernel.cs:46
   Checkin.App_Start.NinjectWebCommon.CreateKernel() in d:\temp\44uvjuob.32m\input\Checkin\App_Start\NinjectWebCommon.cs:43
   Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:50
   Checkin.App_Start.NinjectWebCommon.Start() in d:\temp\44uvjuob.32m\input\Checkin\App_Start\NinjectWebCommon.cs:26

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +69
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +242
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +21
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
   WebActivator.BaseActivationMethodAttribute.InvokeMethod() +223
   WebActivator.ActivationManager.RunActivationMethods() +493
   WebActivator.ActivationManager.RunPreStartMethods() +28
   WebActivator.ActivationManager.Run() +55

[InvalidOperationException: The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +377
   System.Web.Compilation.BuildManager.CallPreStartInitMethods() +267
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +690

[HttpException (0x80004005): The pre-application start initialization method Run on type WebActivator.ActivationManager threw an exception with the following error message: Exception has been thrown by the target of an invocation..]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +532
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +90
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +172

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

If I reload the page - the exception disappears. This might have something to do with using ninject for a filter:

this.BindFilter<AdminAuthorizationAttribute>(FilterScope.Controller, -100)
    .When((context, ad) => context.RouteData.DataTokens["area"] != null &&
            ((string)context.RouteData.DataTokens["area"]).ToLower() == "admin" && 
            ((string)context.RouteData.Values["controller"]).ToLower() != "login");
Søren Pedersen
  • 764
  • 8
  • 20
  • Even I have been baffled with this. What makes you think its Ninject that's crashing? Does it disappear if you take out the filter? – Mrchief Apr 04 '12 at 14:55
  • Also getting this error. Could also be new relic as I am getting this on both appharbor and a windows 2008 server with new relic installed. Both with ninject 2.2 and 3.0. – terjetyl Apr 04 '12 at 17:13
  • From the stack trace (`NewRelic.Agent.Core.AgentShim.GetTracer`) it seems NewRelic which is causing the problem... – nemesv Apr 04 '12 at 18:53
  • 5
    We are aware of this problem and are working with New Relic to come up with a solution. – friism Apr 04 '12 at 20:46
  • 2
    Sounds good friism. Is there a way for me to get a notice or read somewhere when this is fixed? – Søren Pedersen Apr 05 '12 at 08:28
  • @friism, any updates? This is making our site look really bad. – Jess Chadwick Apr 10 '12 at 13:39
  • We're still working with New Relic, the problem has been identified and we're working on a solution. – friism Apr 10 '12 at 16:23
  • For the record, I have this issue too. The fix below @Remo appears to work so far - but maybe the pool hasn't restarted so I'm not sure. When a fix is in place where will it be posted? – Neil Thompson Apr 17 '12 at 09:19

2 Answers2

13

The problem is not caused by Ninject. It is NewRelic.Agent.Core.AgentShim (used by AppHarbor) that is the culprit.

Although looking at the stacktrace, I think you can work around this problem by disabling Ninject's automatic extenison loading feature and load them manually.

var kernel = new StandardKernel(new NinjectSettings { LoadExtensions = false })
kernel.Load(new Ninject.Web.Mvc.MvcModule()); // same for all other extension modules
Joe
  • 80,724
  • 18
  • 127
  • 145
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • I had tried this, but it did not solve my issue. I still get the exception, even with LoadExtensions = false – Jess Chadwick Apr 09 '12 at 19:42
  • @JessChadwick This is just a workaround for exactly the stacktrace in the question. It will solve that stacktrace if done exactly as above. Either you have a different problem or you are setting LoadExtension to false after the kernel is created (kernel.Settings.LoadExtensions = false). This will not work. If you have a different problem, you probably have to wait until AppHarbor fixes the problem. – Remo Gloor Apr 09 '12 at 21:57
  • @RemoGloor, nope - I've got exactly the same stack trace and setting the LoadExtensions property just as in your example above. Looks like I'll just have to wait for AppHarbor to fix. – Jess Chadwick Apr 10 '12 at 13:38
1

We've temporarily disabled the New Relic add-on and removed the New Relic agent from AppHarbor servers. We'll re-add New Relic once it's no longer causing problems for applications running on the platform.

friism
  • 19,068
  • 5
  • 80
  • 116