ASP.NET MVC ControllerFactory setting is not affected

I am trying to create ASP.NET MVC2 controllers using StructureMap, but ASP.NET does not seem to remember what I called ControllerBuilder.Current.SetControllerFactoryin the Global.asax file.

In particular, I get an error that my controller does not have a constructor without parameters. The stack trace shows that my custom ControllerFactoryhas never been executed.

Here is my method call that should tell ASP.NET which ControllerFactory to use:

Sub Application_Start()

    RegisterRoutes(RouteTable.Routes)

    ControllerBuilder.Current.SetControllerFactory(GetType(StructureMapControllerFactory))

    BootStrapper.RegisterDependencies()

End Sub

And here is the exception that I get.

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
  System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck) +0
  System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache) +86
  System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache) +230
  System.Activator.CreateInstance(Type type, Boolean nonPublic) +67
  System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80

Why does ASP.NET forget which ControllerFactory to use?

+3
source share
2 answers

How is your StructureMapControllerFactory defined? Does it have a default constructor?

, SetControllerFactory:

// First create the controllerFactory instance...

ControllerBuilder.Current.SetControllerFactory(controllerFactory);
+2

Factory ...

0

Source: https://habr.com/ru/post/1791101/


All Articles