PRISM Property injection fails
In my Silverlight RIA app I can successfully inject an ExceptionHelper class via constructor injection:
开发者_如何学JAVA[InjectionConstructor]
public CDashCatDataService(IExceptionHelper exceptionHelper)
{
_exceptionHelper = exceptionHelper;
SetUp();
}
void SetUp()
{
if (_context == null)
{
try
{
throw new Exception("this is a test exception");
_context = new CDASHDomainContext();
}
catch (Exception ex)
{
_exceptionHelper.HandleException(ex, string.Format("{0} -> {1}", GetType().FullName, "Setup"));
}
}
Context = _context;
}
The SetUp method successfully logs and handles the test exception.
However, I can't get this working if I use property injection instead:
[Dependency]
public IExceptionHelper ExceptionHelper { get; set; }
and then using
catch (Exception ex)
{
ExceptionHelper.HandleException(ex, string.Format("{0} -> {1}", GetType().FullName, "Setup"));
}
in the SetUp method. If I use this, the test exception is not logged and handled.
I'm clearly missing something here - any ideas?
I think this is because the ExceptionHelper property is being called from the constructor via the SetUp method and you can't call injected properties until after the constructor has executed.
精彩评论