0

This question is similar to Unity IoC suddenly fails during runtime but not exactly the same. That question relates to WebApi but our project is not WebApi (it's standard ASP.NET MVC v5).

The application has been under constant development and is being used in production for several years now, and earlier this week the application seemed to crash with an exception we've not seen before:

Server Error in '/' Application.

Exception of type 'Unity.Exceptions.InvalidRegistrationException' was thrown.
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: Unity.Exceptions.InvalidRegistrationException: Exception of type 'Unity.Exceptions.InvalidRegistrationException' was thrown.

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:

[InvalidRegistrationException: Exception of type 'Unity.Exceptions.InvalidRegistrationException' was thrown.]

[InvalidOperationException: No public constructor is available for type Mehdime.Entity.IAmbientDbContextLocator.]
   lambda_method(Closure , BuilderContext& ) +118
   Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context) +476
   Unity.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context) +57
   Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration) +430
   Unity.Builder.BuilderContext.Resolve(Type type, String name) +456
   Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value) +485
   lambda_method(Closure , BuilderContext& ) +147
   Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context) +476
   Unity.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context) +57
   Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration) +430
   Unity.Builder.BuilderContext.Resolve(Type type, String name) +456
   Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value) +485
   lambda_method(Closure , BuilderContext& ) +197
   Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context) +476
   Unity.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context) +57
   Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration) +430
   Unity.Builder.BuilderContext.Resolve(Type type, String name) +456
   Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value) +485
   lambda_method(Closure , BuilderContext& ) +100
   Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context) +476
   Unity.<>c.<.ctor>b__73_1(BuilderContext& context) +112

[ResolutionFailedException: Resolution failed with error: No public constructor is available for type Mehdime.Entity.IAmbientDbContextLocator.

For more detailed information run Unity in debug mode: new UnityContainer().AddExtension(new Diagnostic())]
   Unity.<>c.<.ctor>b__73_1(BuilderContext& context) +483
   Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type, String name, ResolverOverride[] overrides) +513
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +61

[InvalidOperationException: An error occurred when trying to create a controller of type 'LogistiX.WebApp.SkyPortal.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +242
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +262
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1020
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +128

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

No deployment was done prior to this and it hasn't happened again since restarting the Application Pool (so far), but we'd like to understand the problem and hopefully ensure it doesn't happen again.

Following the advice in Steven's answer to the linked question, I have created a new UnityControllerActivator class but can't figure out where to place the code which replaces instances of IHttpControllerActivator with the new class.

configuration.Services.Replace(typeof(IHttpControllerActivator), 
    new UnityControllerActivator());

One would assume this goes in either the UnityConfig.cs or Startup.cs files however no "configuration" variable exists (nor anything named differently that might be what we're after). A colleague suggested instead to try GlobalConfiguration.Configuration.Services.Replace(...) instead, however, the GlobalConfiguration class is also not available.

Looking into that, it seems that the GlobalConfiguration class is for WebApi projects (I may be wrong), but if that's the case, I believe it's no good to us.

I do understand that Steven's answer wasn't a "fix" as such, but it would help to prevent the "true" exception from being "swept away" - which would lead to a fix should it happen again.

I can't help but feel though, that we are barking up the wrong tree here - considering how all search results for the error, as well as the search results for things like GlobalConfiguration, all seem related to WebApi.

Can someone shed some light?

Thanks

Dan

Dan
  • 1

0 Answers0