Because of Dependency Injection errors can become more complex to debug. During Module load if any of the child classes encounter an error the module will not load and it will crash the application. The current HandleModuleLoadError() error handler buries the actual error in the InnerException property. The lower the level of the error - the deeper it gets buried. I take a different approach in my modified version of this handler, I iterate through the InnerExceptions throwing each one on the stack. When there are no more innerExceptions I pull (pop) the last error and display it - the next error becomes it's InnerException. The result follows:
Original Error Message
Failed to load module MainModule from assembly SDMSMain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. Error was:\r\nResolution of the dependency failed, type = \"SDMSMain.VPC.MainPresenter\", name = \"\". Exception message is: The current build operation (build key Build Key[SDMSMain.VPC.MainPresenter, null]) failed: Object reference not set to an instance of an object. (Strategy type BuildPlanStrategy, index 3)
New Error Message
Object reference not set to an instance of an object. at CompositeWPF.Library.Base.PresenterBase`2..ctor() in D:\GWN\CompositeWPF.Library\CompositeWPF.Library\Base\PresenterBase.cs:line 20
at SDMSMain.VPC.MainPresenter..ctor(IMainController controller, ILogger logger)
in D:\GWN\SDMSMain\VPC\MainPresenter.cs:line 24
at BuildUp_SDMSMain.VPC.MainPresenter(IBuilderContext )
at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
D:\GWN\UnitySource\Src\ObjectBuilder\..\..\DynamicMethod\DynamicMethodBuildPlan.cs:line 37 at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
in D:\GWN\UnitySource\Src\ObjectBuilder\Strategies\BuildPlan\BuildPlanStrategy.cs:line 40
at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
in D:\GWN\UnitySource\Src\ObjectBuilder\Strategies\StrategyChain.cs:line 92
This error was perplexing because it ran just fine when I was in "RELEASE" mode (to verify all debug logging was turned off) but when I went back to "DEBUG" mode it crashed...
The new error let's me know that the Presenter base class, PresenterBase<TView, TController>, has a problem with it's constructor on line 20. When I examined the code it started to make sense real quick - Line 20 was a debug statement; in this case the break point revealed that my Logger object was null. This tells me that my presenter is not being resolved so I'll have to resolve ILogger manually before using Logger. Now take a look at the default (original) error and you'll find I may have been chasing my tail for a while trying to nail it down.
10 namespace CompositeWPF.Library.Base
11 {
12 public class PresenterBase<TView, TController>
13 {
14 [Dependency]
15 public ILogger Logger { get; set; }
16
17 public PresenterBase()
18 {
19 #if DEBUG
20 Logger.Log(GetType().Name + " PresenterBase()",
21 Category.Debug, Priority.None);
22 #endif
23 }
New Error Handling Code for ModuleLoader.cs Follows:
public virtual void HandleModuleLoadError(
ModuleInfo moduleInfo, string assemblyName, Exception exception)
{
Exception moduleException =
new ModuleLoadException(moduleInfo.ModuleName,
assemblyName, exception.Message, exception);
Stack<Exception> errorList = new Stack<Exception>();
Exception exceptionToThrow = moduleException;
// If there are inner exceptions we'll iterate through
// them (placing them on the stack) so that we can pull
// the original message off when there are no more exceptions
if (exceptionToThrow.InnerException != null)
{
while (exceptionToThrow!=null
&& exceptionToThrow.InnerException != null)
{
errorList.Push(exception);
exceptionToThrow = exception.InnerException;
}
// Pull the original error
exceptionToThrow = errorList.Pop();
}
string errMessage = exceptionToThrow.Message
+ exceptionToThrow.StackTrace;
loggerFacade.Log(errMessage,
Category.Exception, Priority.High);
if (exceptionToThrow.InnerException != null)
throw new Exception(errMessage,
exceptionToThrow.InnerException);
else
throw new Exception(errMessage);
// Original code
//throw moduleException;
}
Tags:
error,
stack,
compositewpf
Categories:
CompositeWPF