How to debug Unity resolution?
Asked Answered
F

3

17

In a WPF project(with prism) we are using Unity as DI framework.

Recently, after we merged two big branches, we were not able to start our application, we were having StackOverflowException. Due to the nature of the exception, we were not able to get his call stack or current values, we were only seing that the issue was comming from Unity due to the namespace.

We spent more than 5 hours to find were the issue was occuring:

Simplified:

We were having 3-4 services(A, B, C and D), each one with an implementation, and at some point, the service A was requiring the service B, which was requiring the service C, which was requiring the service D, which was requiring the service A). Basically a cyclic reference.

We would like to know if there is any possibility to add some logs that Unity is trying to solve Service A with the implementation X, and was requiring to resolve service B, ...

This would have helped us a lot to debug this issue, to directly see which services were implicated in this cyclic reference.

Is there anyway to achieve this?

Freespoken answered 31/3, 2016 at 11:36 Comment(1)
There is no tracing or output in Unity when resolving types. Maybe you could use a custom Extension to extended Unity with your desired behavior.Berneicebernelle
A
8

A bit late to the party, but for the exact same problem, I created this and it's working just fine:

internal class LogExtension : UnityContainerExtension
{
    public LogExtension( ILogger logger )
    {
        _logger = logger;
    }

    #region UnityContainerExtension
    protected override void Initialize()
    {
        Context.Strategies.Add( new LoggingStrategy( _logger ), UnityBuildStage.PreCreation );
    }
    #endregion

    #region private
    private readonly ILogger _logger;

    private class LoggingStrategy : BuilderStrategy
    {
        public LoggingStrategy( ILogger logger )
        {
            _logger = logger;
        }

        #region BuilderStrategy
        public override void PreBuildUp( IBuilderContext context )
        {
            _logger.Log( $"Resolving {context.BuildKey.Type} for {context.OriginalBuildKey.Type}" );
        }
        #endregion

        #region private
        private readonly ILogger _logger;
        #endregion
    }
    #endregion
}

And somewhere in the the bootstrapper (ConfigureContainer most likely):

Container.AddExtension( new LogExtension( _logger ) );
Anguiano answered 24/10, 2018 at 14:15 Comment(2)
Where can I find IBuilderContext interface ??Tetrameter
My code is for Unity 4. With version 5, the signature of BuilderStrategy changed minimally, but the basic concept should still work.Anguiano
D
1

If You are using Unity v5+ and are too lazy to look up what changed from v4 to v5, then you can copy my version:

using System.Reflection;
using Unity.Extension;
using Unity.Builder;
using Unity.Strategies;

namespace Company.ProjectName.Shared.Bootstrap.Unity
{
    public class LogResolvesUnityContainerExtension : UnityContainerExtension
    {
        private static readonly ILogger Logger = LoggerManager.CreateLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        protected override void Initialize()
        {
            Context.Strategies.Add(new LoggingStrategy(Logger), UnityBuildStage.PreCreation);
        }

        private class LoggingStrategy : BuilderStrategy
        {
            private readonly ILogger _logger;
            
            public LoggingStrategy(ILogger logger)
            {
                _logger = logger;
            }

            public override void PreBuildUp(ref BuilderContext context)
            {
                // Be aware that for Singleton Resolving this log message will only be logged once, when the Singleton is first resolved. After that, there is no buildup and it is just returned from a cache.
                
                var registrationType = context.RegistrationType; 
                var registrationName = context.Name;
                var resolvedType = context.Type;
                
                var registrationNameWithParenthesesOrNothing = string.IsNullOrEmpty(registrationName) ? "" : $"({registrationName})";
                _logger.LogDebug($"Resolving [{registrationType}{registrationNameWithParenthesesOrNothing}] => [{resolvedType}]");
            }
        }
    }
}

Credit still goes to Haukinger for writing the v4 original version.

Dogberry answered 3/2, 2021 at 3:43 Comment(0)
I
0

You can configure the Assembly binding log viewer Fusion Log to help diagnose problematic Unity resolutions.

It's installed with Visual Studio and you can just type "fuslogvw" into the VS command prompt to launch.

Otherwise, this may help with configuring the logger: How do I enable assembly bind failure logging (Fusion) in .NET?

Ify answered 8/4, 2016 at 12:5 Comment(1)
Fusion logging doesn't seem to help debugging Unity resolution problems.Hoebart

© 2022 - 2024 — McMap. All rights reserved.