Hello Support,
I am using POCO view-models with your MVVM framework. Also, I am using Autofac and hooking into the static root context event like so (from Program.cs):
C#MVVMContextCompositionRoot.ViewModelCreate += (s, e) =>
{
using (var scope = rootContainer.BeginLifetimeScope(
b => b.RegisterType(e.RuntimeViewModelType).As(e.ViewModelType)))
{
e.ViewModel = scope.Resolve(e.ViewModelType);
}
};
Finally, I'm using constructor-based injection. Here's an example from one of my view-models (NewCompanyControlViewModel.cs):
C#protected NewCompanyControlViewModel()
{
}
public NewCompanyControlViewModel(
IContactModelFactory contactModelFactory,
IContactCategoryViewModelFactory contactCategoryViewModelFactory,
IContactDraftModelFactory contactDraftModelFactory,
IHashCodeProvider hashCodeProvider,
NewCompanyViewModelValidator validator)
{
_contactModelFactory = contactModelFactory;
_contactCategoryViewModelFactory = contactCategoryViewModelFactory;
_contactDraftModelFactory = contactDraftModelFactory;
_hashCodeProvider = hashCodeProvider;
_validator = validator;
}
Strangest thing: All of the depdencies are injected correctly in a Debug build, but are not injected in a Release build. Also, I had to add a parameterless constructor to get this to work at all in Release. The parameterless constructor is not necessary in Debug. In Release, if I remove the parameterless constructor, the application fails with an exception indicating that it can't find a parameterless constructor (paraphrasing). As for view-model constructor, here is the relevant code from the corresponding view (NewCompanyControlView.cs):
C#private void InitializeBindings()
{
mvvmContext.ViewModelType = typeof(NewCompanyControlViewModel);
mvvmContext.RegisterService(MessageBoxService.CreateXtraMessageBoxService(ParentForm));
var context = mvvmContext.OfType<NewCompanyControlViewModel>();
...
}
For the life of me I can't figure out why this would work in Debug, but not in Release. The only two things that come to mind revolve around the fact that your framework creates dynamic view-model types and that execution security might have something to do with it.
Any idea as to what could be going on?
Thank you.
Hi,
I was able to reproduce this behavior. Now we need some additional time to research it. We will update this thread as soon as we have any results.
Hi Sasha,
Well, it’s actually good to hear that you can reproduce the problem. That’s the first step.
I would like to add that it doesn’t appear to be Autofac. Last night I switched to SimpleInjector just for test purposes. Same thing: Dependencies aren’t injected.
Just thought you should know…