Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeFactoryContext.Consumer changed from null to System.Object in v4.5.1 and later versions #734

Closed
mpickers opened this issue Jul 22, 2019 · 3 comments
Labels
Milestone

Comments

@mpickers
Copy link

@mpickers mpickers commented Jul 22, 2019

In v4.4.3, when using RegisterConditional the TypeFactoryContext.Consumer could be null when there was no consumer, however, versions of Simple Injector after v4.4.3 seem to contain an ImplementationType of System.Object with Target.Name of 'Chars' which doesn't seem correct.

I've checked the existing bug reports and release notes but there doesn't seem to be a mention of a reported issue or expected change in behavior.

Example to reproduce:
Create a new console app with the below code. Run with v4.4.3 and then v4.6.0 of SimpleInjector nuget.

   class Program
   {
      static void Main(string[] args)
      {
         var con = new Container();

         con.RegisterConditional(
            typeof(ILogger),
            // As of v4.5.2 c.Consumer is never null and instead the 
            // ImplementationType contains System.Object
            c => typeof(MyLogAdapter<>).MakeGenericType(
               c.Consumer == null 
               ? typeof(Program) 
               : c.Consumer.ImplementationType),
         Lifestyle.Singleton,
         c => true);

         var noConsumerLogger = con.GetInstance<ILogger>();

         noConsumerLogger.Info("No Consumer.");

         var consumer = con.GetInstance<MyConsumer>();

         consumer.WriteToLog("I have a consumer");
      }
   }

   public interface ILogger
   {
      void Info(string msg);
   }

   public class MyLogger : ILogger
   {
      private readonly Type _type;

      public MyLogger(Type type)
      {
         _type = type;
      }

      public void Info(string msg)
      {
         Debug.WriteLine($"{_type.Name}: {msg}");
      }
   }

   public class MyLogAdapter<T> : ILogger
   {
      private readonly MyLogger _adaptee = new MyLogger(typeof(T));

      public void Info(string msg)
      {
         _adaptee.Info(msg);
      }
   }

   public class MyConsumer
   {
      private ILogger _logger;

      public MyConsumer(ILogger logger)
      {
         _logger = logger;
      }

      public void WriteToLog(string msg)
      {
         _logger.Info(msg);
      }
   }

Output for v4.4.3 of SimpleInjector
Program: No Consumer.
MyConsumer: I have a consumer

Output for v4.6.0 of SimpleInjector
Object: No Consumer.
MyConsumer: I have a consumer

@mpickers mpickers added the question label Jul 22, 2019
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Jul 23, 2019

Thank you for reporting this. I can confirm your findings. This seems to be a bug introduced in v4.5.2. I will investigate the cause of this, and create v4.5.3 and v4.6.1 patch releases to fix this.

In the meantime, you can use v4.5.1.

I'm sorrry this bugs slipped through the cracks. There seems to be a unit test missing in this regard.

@dotnetjunkie dotnetjunkie added bug task and removed question labels Jul 23, 2019
@dotnetjunkie dotnetjunkie added this to the v4.6.1 milestone Jul 23, 2019
@dotnetjunkie dotnetjunkie changed the title TypeFactoryContext.Consumer changed from null to System.Object between v4.4.3 and later versions TypeFactoryContext.Consumer changed from null to System.Object in v4.5.1 and later versions Jul 23, 2019
dotnetjunkie added a commit that referenced this issue Jul 23, 2019
dotnetjunkie added a commit that referenced this issue Jul 23, 2019
…tions, which was introduced in change #698. Fixes #734.

(cherry picked from commit bb26728)
@dotnetjunkie

This comment has been minimized.

Copy link
Collaborator

@dotnetjunkie dotnetjunkie commented Jul 23, 2019

I just released v4.5.3 and v4.6.1. Both versions fix the bug.

@mpickers

This comment has been minimized.

Copy link
Author

@mpickers mpickers commented Jul 23, 2019

No Problem and thanks for the quick turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.