views:

32

answers:

2

My scenerio is like this:

On the server there is an application which exposes a COM object allowing to interact with this application programaticaly. However I can connect only once through COM due to licence restrictions. So, after initialization a COM object will return me a more specified ConnectionObject.

I need an advice what management model I schould chose for my service which will be interacting with the ConnectionObject:

Per-Call Service:

I have two options here:

  1. I can log through COM, perform operation and Logout releasing the connection. But this logging process is somewhat time consuming.

  2. I could create some sort of singleton class which could keep reference to the ConnectionObject. The problem is that I do not know how to share the same instance of object through different instances of the service? Is it possible?

Singleton Service:

The problem of sharing ConnectionObject does not exists. Object will be created at the begining of life of the service and freed when service will be shut down. However I've read that using this kind of service is not recommended.

Thanks for any advices.

A: 

Singleton Service:

The problem of sharing ConnectionObject does not exists. Object will be created at the begining of life of the service and freed when service will be shut down. However I've read that using this kind of service is not recommended.

Where you read this ?

This is the most suitable choice in your scenarion.

saurabh
qoute: '"The singleton service is the sworn enemy of scalability. The reason is the singleton state synchronization (...) I recommend that you avoid singletons in the general case and find ways to share the state of the singleton instead of the singleton instance itself..."' - OReilly - Programming WCF Services
Wodzu
A: 

Given your requirements with the COM object, and the time consuming log in and out process - I would go with the singleton service. We use our WCF services like this all the time (also talking to legagy COM objects).

You can add the following attribute to the class definition of your WCF service to get the behaviour:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class MyService : IMyServiceContract
{
    [OperationBehavior]
    public void MyServiceCall ()
John Sibly