2

Recently I faced this problem where an ASP.Net MVC website with Autofac as DI was working fine in my local IIS but when I published it in Azure, it was not getting redirected to the default controller, instead it was showing me the welcome page. I did a lot of things to finally know what was the problem like checking the web.config file line by line. Is there any easy mechanism to catch DI failures in production / published environment?

Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
ViBi
  • 515
  • 7
  • 19

2 Answers2

1

It is no easier mechanism for Autofac specifically, than what you would need to debug the rest of the application. Logging is the first tool you should implement. Given that this is a production environment, you probably already have some form of logging infrastructure in place.

Resolve failures in Autofac (e.g. when Autofac is unable to fulfill constructor parameter requirements) causes exceptions, which unless handled explicitly, can be handled and logged at the application level using the Application_Error event handler.

In case there are no exceptions, or exceptions are swallowed somewhere in the MVC stack, you could hook up to the container Scope and Resolve events and log the activity. That way you can get a picture of what Autofac is doing in the production environment. This SO question discusses both container events and component registration events.

Community
  • 1
  • 1
Peter Lillevold
  • 33,668
  • 7
  • 97
  • 131
1

The way to do this is by verifying if all root registrations can be created. You can create a unit test that loops over all registrations and try to resolve them one by one. Being able to do this is very important, since with DI the application code itself isn't responsible for maintaining the dependencies between implementations, and the compiler will therefore not be able to verify whether the dependency graph is correct. Although it is impossible for the compiler to verify the dependency graph, verifying the dependency graph is still possible and advisable, because not doing this will force you to click through the complete application and eventually leads to the situation you got yourself into.

Unfortunately, I found that this is actually very hard to do with Autofac. I pretty sure that this should be possible, but I never got this working. Some other DI containers make it considerably easier to verify the container's configuration and check for other common configuration mistakes.

Steven
  • 166,672
  • 24
  • 332
  • 435