tags:

views:

3105

answers:

2

I have developed a VB.NET WCF service that recives and sends back data. When the first client connects it starts the data output that continues also if the client is closed. If a new client connects then a new object is created and the data output starts at the begninning and continues in parallel with the old instance. Is there a way to read the same service object from multiple clients?

The service is self-hosted.

Thanks

UPDATE: I solved the problem adding the following bit of code to the service class:

<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Multiple, InstanceContextMode:=InstanceContextMode.Single)> _

To use the ServiceHost overload that takes in the SingletonInstance, the service must be tagged with the appropriate ServiceBehaviours.

+1  A: 

If you are selfhosting your service, you can use the ServiceHost constructor overload that takes service instance instead of a service type. All clients will connect to the same service instance.

If you are hosted in IIS, you have no control over the service instances. What's even worse, you don't have control over the process lifetime. Thus, if you want a signleton, you'll have to have your own process and have the service instances connect to it.

UPDATE: As @jezell pointed out, there is some control over the instancing of the service in IIS through the InstanceContextMode. However, that still does not control the IIS processes.

Franci Penov
Are you talking about IIS 6? I suppose there might be some change to it in IIS 7 but not sure though. Do let me know if this thing is possible with IIS 7. Thanks.
Syed Sajid Nizami
Selfhosting is done in Windows service or a standalone process, by creating an instance of ServiceHost and giving it the type or the instance of the service to host. In this scenario, the code author controls the process lifetime and/or the service instances.
Franci Penov
I'm using a selfhosting service and I must use the service overload constructor. I will try. Thanks
Marco Bettiolo
This is incorrect.
jezell
jezell's answer did the trick, when I'll have more time I'll try Franci Penov's answer.
Marco Bettiolo
Franci Penov, to use the ServiceHost constructor overload that takes a singleton service instance, the WCF Service must have the appropriate ServiceBehaviours. I updated the question to reflect this. Thanks
Marco Bettiolo
+1  A: 

I talk about the options here:

http://www.iserviceoriented.com/blog/post/Configuring+Performance+Options+-+WCF+Gotcha+3.aspx

First, you have InstanceContextMode which can be Single, PerCall, or PerSession. This controls how new instances of your service class are created.

In addition to this, the throttling and concurrency settings are important to look at, since setting instance context mode to something like single without changing the concurrency mode to multiple could have seriously negative consequences. Take a look at the post for a more detailed discussion.

jezell
InstanceContextMode=Single still does not control the IIS processes. IIS still can decide to take down the service process if there are no outstanding requests.
Franci Penov