0

I have a class which takes a Func as a constructor argument:

public CurrencyCache(Func<IEnumerable<Currency>> loadData)

The Func is basically the method to call when the cache has expired

When I register this in AutoFac I need to specify the method. And to do that I need to resolve a dependency from the container. Except I can’t as the container isn’t built yet

builder.Register(o => new CurrencyCache(<some code to resolve the class with the method I want to call>));

I don’t want to manually new-up the dependency graph to this class as it is a few levels deep and needs various data from config files etc

So I want the container to resolve the class for me

But as I say, the container isn’t built yet

Is there a way around this? Does Autofac have some api to deal with such a scenario

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ChrisCa
  • 10,876
  • 22
  • 81
  • 118
  • are you searching a way to resolve `Interface` before `ContainerBuilder` is build ? Try [Autofac - resolve before build](http://stackoverflow.com/questions/34500858/autofac-resolve-before-build) – tchelidze Jan 11 '16 at 17:26
  • just looking for a way to register a class that takes a Func. similar to this I think http://stackoverflow.com/questions/20583339/autofac-and-func-factories . but not quite the same – ChrisCa Jan 11 '16 at 17:31

1 Answers1

1

When you use the Register method with a lambda arguments, the first parameter is a IComponentContext which is a builded container.

You can do this :

builder.Register(o => new CurrencyCache(o.Resolve<XXX>().DoSomething));

By the way be careful with the scope of registration to avoid captive dependency issue.

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62