1

I have a communication service with ip address as constructor parameter to indicate where to connect.

I want to know if I can inject this service to my controller with different ip address at run time.

All of injected service I used has a parameterless constructor at least. I know autofac can resolve type using TypedParameter or NamedParameter to pass run time parameter to constructor. But I can't inject autofac container(IContainer) to my orchardcms service or controller.

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
hengzhe
  • 45
  • 1
  • 6
  • I don't think this is going to work. Why do you need the IP address in the constructor? – ViRuSTriNiTy Aug 15 '17 at 09:07
  • The ip can be customized by user. The it will changed at runtime. So I can't init the communication service when the application starts up – hengzhe Aug 15 '17 at 11:06

2 Answers2

2

Dependency injection is about injecting behavior, not data. This is a job for configuration. You should look at any example of an existing Orchard feature that uses configuration.

Bertrand Le Roy
  • 17,731
  • 2
  • 27
  • 33
0

Basically what you need is parameterized instantiation.

Please read the answers to the following question: Autofac runtime parameters

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58