System.OutOfMemoryException with IConfigureComponents and DP

nservicebus

(Garyamorris) #1

Could someone help me understand the following issue, this may be a bug or I misunderstand IConfigureComponents usage. If you take a look at the example below, when I register my auto mapper with IConfigureComponents.ConfigureComponent I am getting an System.OutOfMemoryException when attempting to create the mapper (I’ve removed the mapping code for brevity).

However when I pre-register the mapper in the container, I don’t experience the same issue. Would really appreciate some help understanding this issue.

Registration with IConfigureComponents (NOT WORKING)

    private static EndpointConfiguration ConfigureDependencies(this EndpointConfiguration endpointConfiguration, UnityContainer container)
    {
        endpointConfiguration.UseContainer<UnityBuilder>(
                        customizations: customizations =>
                        {
                            customizations.UseExistingContainer(container);
                        });

        endpointConfiguration.RegisterComponents(configureComponents =>
        {
            configureComponents.ConfigureComponent<MutateIncomingMessages>(DependencyLifecycle.SingleInstance);
            configureComponents.ConfigureComponent(delegate() { return new MapperConfiguration(cfg =>
                {
                    // Mappings
                }).CreateMapper();
            }, DependencyLifecycle.SingleInstance);
        });

        return endpointConfiguration;
    }

    public class MutateIncomingMessages : IMutateIncomingMessages
    {
        private IMapper _mapper;
        public MutateIncomingMessages(IMapper mapper)
        {
            _mapper = mapper;
        }
    }

Prior registration in container (WORKING)

    private static EndpointConfiguration ConfigureDependencies(this EndpointConfiguration endpointConfiguration, UnityContainer container)
    {
        container.RegisterInstance<IMapper>(new MapperConfiguration(cfg =>
        {
            // Mappings
        }).CreateMapper());
                
        endpointConfiguration.UseContainer<UnityBuilder>(
               customizations: customizations =>
                    {
                        customizations.UseExistingContainer(container);
                    });

        endpointConfiguration.RegisterComponents(configureComponents =>
        {
            configureComponents.ConfigureComponent<MutateIncomingMessages>(DependencyLifecycle.SingleInstance);
        })
        
        return endpointConfiguration;
    }

    public class MutateIncomingMessages : IMutateIncomingMessages
    {
        private IMapper _mapper;
        public MutateIncomingMessages(IMapper mapper)
        {
            _mapper = mapper;
        }
    }

(William Brander) #2

Hey @garyamorris

Yeah, this appears to be a bug when using Unity to resolve types that have properties to themselves on the same object. The cause is the container builds the object, then all the properties on the object. If one of the properties is of the same type as the current object, it builds the same object again, which has the same property, ad infinitum. This is why the out of memory exception occurs.

I have added a test to replicate this, so a fix should be coming soon.