4

My ASP.NET MVC app starts very very slowly (minimum 20 seconds till first render). It has lots of assemblies in the bin folder.

I understand the runtime scans all assemblies, looking for Controller classes. I imagine this kind of reflection would be very slow. We're on shared hosting, so the first load is terribly slow, and there are lots of first loads, because the app pool is recycled frequently.

I've seen lots of examples of manually registering controllers using IOC containers. We don't use IOC.

So, how do I manually register controllers? I'd like to do this in Application_Start, or something like that.

Edit regarding some of the comments. I know that this won't solve the slow start problem entirely. But it might help. So no point in dissuading me from trying, because we've decided to try :).

22
  • 3
    I highly doubt the bottleneck is in identifing all the controller classes Commented May 11, 2015 at 18:47
  • @ScottSelby Won't know until I try. Reflection over dozens of assemblies is bound to be slow. Commented May 11, 2015 at 18:48
  • 4
    just remove all controllers from the app except Home , build and push . There shouldn't be anything dependent on those being there , and that should be a quick test - see if things dramatically improve, guessing they wont. Commented May 11, 2015 at 18:50
  • @ScottSelby Nice idea! We plan on manual registration anyways though, due to some profiling we've done. Before my code is even reached, the runtime drops about 10s, and I have no clue on what... I'm hoping manual registration of controllers and views will help. Commented May 11, 2015 at 18:52
  • 2
    It may be easy. But, given a finite amount of time, it's best to spend your time where you'll be solving the actual problem. Also, any changes you make always run the risk of making things worse. It's best to start out by only making the changes necessary to solve the problem, or you may have two problems to fix. Commented May 11, 2015 at 19:20

1 Answer 1

4

You need to override DefaultControllerFactory. Then set it at Application_Start.

ControllerBuilder.Current.SetControllerFactory(typeof (CustomControllerFactory));

Example -

FYI: Mark Seemann (author of Dependency Injection in .Net) said "creating an object instance is something the .NET Framework does extremely fast. Any performance bottleneck your application may have will appear in other places."

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.