How to debug Unity resolution?

后端 未结 3 2084
离开以前
离开以前 2021-02-19 01:42

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 w

3条回答
  •  轮回少年
    2021-02-19 02:12

    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 ) );
    

提交回复
热议问题