toughQuestions
toughQuestions

Reputation: 169

How to share a property between multiple WCF services installed on a local machine?

I have a Windows service hosting multiple WCF services. All of it running on a local machine (using NamePipe).
Is there a simple way to have these WCF services share a property? (I would rather not have them sharing a file).
I need this as each session instantiated within each WCF service will be getting a hold on a given hardware and the other WCF services need to know what is still available in order to be able to instantiate another session.
Each WCF service implements a different protocol, which is why I did not merge the lot. There will be one proxy for each host.

Upvotes: 0

Views: 233

Answers (1)

Clay
Clay

Reputation: 5084

I don't know of a super simple way of getting service instances to share a property, but you could create a custom host that derives from ServiceHost and have it implement a particular interface, say something like:

public interface ISharedStateContainer
{
  SharedState State { get; set; }
}

This interface would have to be known your services. Then, in your windows service project, you could make a custom service host:

public class CustomServiceHost: ServiceHost, ISharedStateContainer
{
   SharedState state;
   public SharedState State{ get{ return state; } set{ state=value; } }       
}

...and then when the windows service creates the wcf service host instances, it could inject the shared state:

var sharedState = new SharedState();

myServiceHost = new CustomServiceHost( typeof( MyService ) );
((ISharedStateContainer) myServiceHost).State = sharedState;

myOtherHost = new CustomServiceHost( typeof( OtherService ) );
((ISharedStateContainer) myOtherHost).State = sharedState;

myServiceHost.Open();
myOtherHost.Open();

...and then, in a running instance of a service, you could get to shared state like this:

var sharedState = ((ISharedStateContainer)OperationContext.Current.Host).State

Where I've got SharedState, you could make it any type you want...but making it a reference type that itself has properties means you can use it to share as many properties as you need. Note that with any shared state, you'll have race conditions to protect against.

I've done a windows service the same way (multiple different wcf service types)...and this is more-or-less how they share state.

EDIT:

I don't know why I didn't think about this sooner, but another nice way to share state is using a singleton. This is probably more straightforward than the earlier approach. I have this pattern going on, too...for a somewhat different reason, but it would serve for shared state, too:

public class SharedState
{
  //--> singleton instance...
  static readonly SharedState current;

  //--> use static initializer to create the current instance...
  static SharedState( )
  {
    current = new SharedState();
  }

  //--> hide ctor...
  private SharedState(){}

  public static SharedState Current
  {
    get { return current; }
  }

  //--> all your shared state instance methods and properties go here...

  public string SomeString
  {
    get
    {
      return //...
    }
  }

}

...and then you can get to this object from anywhere in your service, even from code not running in the context of a client operation. I use this for long running background task that the service needs to perform periodically, but shared properties are super easy:

var someValue = SharedState.Current.SomeString;

Upvotes: 1

Related Questions