Ticket T658142
Visible to All Users

Dependency injection works in Debug build, but not in Release build

created 7 years ago

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.

Comments (2)
Sasha (DevExpress Support) 7 years ago

    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…

      Answers approved by DevExpress Support

      created 7 years ago (modified 7 years ago)

      Hi,

      Thank you for your patience. We researched this behavior and found that it occurs because ViewModelCreate is a so-called Weak Event (refer to the Weak Event help article for additional information). Because of this fact, an event handler is removed too early in the release version. There is a simple way to avoid this behavior by changing  the scope of the rootContainer  variable like this:

      C#
      private static IContainer rootContainer { get; set; } [STAThread] static void Main() { var builder = new ContainerBuilder(); builder.RegisterType<TestViewModel>(); builder.RegisterType<MyClass>().As<IService>(); rootContainer = builder.Build(); MVVMContextCompositionRoot.ViewModelCreate += (s, e) => { using (var scope = rootContainer.BeginLifetimeScope( b => b.RegisterType(e.RuntimeViewModelType).As(e.ViewModelType))) { e.ViewModel = scope.Resolve(e.ViewModelType); } };

      Please test this approach and let me know if it helps.

        Comments (2)

          Hi Sasha,

          That's it!  That solved the problem!

          It might be worth a note in your documentation.  As for me, I didn't know static events could even be weak.

          Thanks so much for your help!

          Sasha (DevExpress Support) 7 years ago

            Hi,

            I am happy to hear that you found my assistance helpful!
            Sure, we will consider adding a corresponding note to our documentation. Should you have further questions, let me know.

            Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

            Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.