Dependency injection with Unity - issue/breaking change when upgrading from 6.x to 7.x

We are currently upgrading a solution from 6.x to 7.x.

Relevant package version:

Unity 5.11.3
NServiceBus.Unity 10.1.0
NServiceBus 7.2.0

I have changed no code in the solution, which was running fine before upgrading. I’m quite certain that the lifetimes are correct, but now we have a recursion-problem resulting in a stackoverflow.

I’m quite sure the problem is because of NServiceBus or the Unity-package is resolving public properties on my objects. This is something new from the previous versions I was running, so my my question is:

Is it possible to disable property buildup from the DI-container all together? My code is only using ctor-injection, so for us the best thing would be to skip this functionality.

Also, the property on the object that is resolves is an indexed property, so it makes no sense for me how this could be set, unless there are some complex functionality around named container instances.

I can code my way around this issue, but still I’m nervous we will experience problems because of this some other place in the codebase. Normally you can enable property injection in Unity by adding a DependencyAttribute to the property. But in NServiceBus it seems like all properties are scanned and resolved by default.

I’m not really looking for a way to address the buildup of this concrete object, I just want to disable property injection to avoid unforeseen problems with my solution.

Call stack:

Hi Harald

I try to reproduce the problem but couldn’t. I did a minimalistic repro attempt by switch our Unity Sample to the same version as you have and then adding an another service that should be property injected. Unfortunately I had no success.

See the commit

Is there anything I’m missing?

In case it is really the property injection container extension that is causing this problem it is possible with a little bit of hackery to disable it as show in

       var extensionsField = typeof(UnityContainer).GetField("_extensions", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance);
        var extensions = (List<IUnityContainerExtensionConfigurator>)extensionsField.GetValue(container);
        foreach (var extension in extensions.ToList())
            if (extension.GetType().FullName == "NServiceBus.Unity.PropertyInjectionContainerExtension")

Unfortunately you have to use reflection because Unity only allows to add extensions and not remove them as far as I know.


Thanks Daniel, I’m trying to repro inside your example by setting up the container in the way I suspected made the problems. But sadly/luckily no luck so far on repro. Thanks for the “hackery” code, I’ll test this in my solution to see if it solves the problem there.