0

I am having an ILogger interface and Logger class(which is singleton) implements ILogger interface. I wanted to register this dependency using unity container. Could you please help me out.

public class Logger: ILogger
{
 private static readonly Lazy<Logger> instance = new Lazy<Logger>(()=> new Logger());
 public static Logger Instance{get{return instance.Value;}}
 private Logger(){}
 public void LogEvent(){}
}
Colin Young
  • 3,018
  • 1
  • 22
  • 46
thegautamnayak
  • 287
  • 4
  • 15

1 Answers1

1

First I see your code not compilable. Your missed type of the Instance:

...
public static ILogger Instance {get{return instance.Value;}}
...

As I understand your intension, try next in your startup class:

var container = new UnityContainer();
var logger = Logger.Instance; 
container.RegisterInstance<ILogger>(logger, new ContainerControlledLifetimeManager());

container.Resolve<ILogger> returns your instance and inject ILogger in a constructor and you should get your logger instance ContainerControlledLifetimeManager resolves a singleton instance of the registered type scoped to the lifetime of the container.

Do not forget setup UnityContainer as the default dependency resolver in your project.

Yurii Maksimov
  • 134
  • 1
  • 5
  • Thanks @luriiMaksimov. I have missed the return type for Instance. The instance is created once through logger. Still do I need to add ContainerControlledLifetimeManager()? – thegautamnayak Oct 19 '18 at 10:16
  • 1
    @thegautamnayak In majority yes. It's needed. This is instruction for unity to keep your instance safety and protects it from runtime changes until you call RegisterInstance again. I pretty sure creators had some idea behind the scene implementing this. – Yurii Maksimov Oct 19 '18 at 10:33
  • Thanks a ton @Iurii-maksimov. one more question :) I am using this inside a filter and that filter is registered in the webapiconfig.cs file. Now how can i inject the logger object to that filter? – thegautamnayak Oct 19 '18 at 11:26
  • 1
    @thegautamnayak Glad to help you. Please mark my answer as a correct one. Make it green :). According to how registered -> you should have something like this: HttpConfiguration config = GlobalConfiguration.Configuration; var container = new UnityContainer(); /* your registrations */ config.DependencyResolver = new UnityResolver(container); And for config.Filters - just reuse you Logger.Instance. But it depends how you build an app – Yurii Maksimov Oct 19 '18 at 13:10
  • do i need to pass the instance like below?config.MessageHandlers.Add(new TestHandler(Logger.Instance)); – thegautamnayak Oct 21 '18 at 05:08
  • 1
    @thegautamnayak Ok, then its not a filter but message handelr. Yes, you can do this. This is one possible way. Check this one https://stackoverflow.com/questions/14756519/inject-dependency-into-delegatinghandler There're answers. – Yurii Maksimov Oct 21 '18 at 15:24
  • 1
    Thanks a ton. It really helped me a lot :) – thegautamnayak Oct 21 '18 at 18:06