Say I have a service like the following
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Multiple
)]
class MyService { ... }
Is it then OK to provide a single instance to multiple ServiceHost
s? Like:
var serviceInstance = new MyService();
var host1 = new ServiceHost(serviceInstance);
host1.AddServiceEndpoint(...);
var host2 = new ServiceHost(serviceInstance);
...
In the existing codebase, this service is being used from a single ServiceHost only. However, the ConcurrencyMode
is Multiple
, i.e.
The service instance is multi-threaded. No synchronization guarantees are made. Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times. being used
Meaning it already had to be thread safe, and from the thread safety angle, providing it to multiple ServiceHosts looks fine.
Any other objections?
As long as you aren't keeping any variables that change state of your service during its lifetime, there is no problem.