0

I'm creating a WCF Host application. My Service class is defined as PerSession something like this:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]   
public class CalculatorService : ICalculatorInstance   
{   
    //...  
}  

I assume that means that everytime a new session is created an new instance of the CalculatorService class will be instantiated. However if I have a common dependency that all the instance will depend upon, what is the best way to pass that dependecy to all the instances?

The ServiceHost class does not seem to have anyway to pass a "state" object to all the instances? Is there another way without using static variables or singletons?

Martin Brown
  • 22,802
  • 13
  • 71
  • 107
  • Possible duplicate of [How do I pass values to the constructor on my wcf service?](https://stackoverflow.com/questions/2454850/how-do-i-pass-values-to-the-constructor-on-my-wcf-service) – Martin Brown Mar 11 '19 at 18:08

1 Answers1

1

Use Dependency Injection. I haven't worked with WCF, but in WPF and UWP DI containers are used. Here is a WCF related article, written by Scott Hannen, on the topic.

http://scotthannen.org/blog/2016/04/13/wcf-dependency-injection-in-5-minutes.html

Here is example provided by Microsoft showing DI in a UWP app. While the details might change, the concepts will remain the same.

https://github.com/Microsoft/InventorySample

Dan Barrett
  • 180
  • 6