I use a PollingDuplexHttpBinding
so that clients can exchange messages in my application.
Clients are registers via a RegisterClient()
method that adds them to a static dictionary for future use.
The code looks like this:
[ServiceContract(Namespace = "...", CallbackContract = typeof(MyServiceCallback))]
public class MyService
{
public static Dictionary<string, MyServiceCallback> Clients =
new Dictionary<string, MyServiceCallback>();
[OperationContract]
public void RegisterClient(string name)
{
Clients[name] =
OperationContext.Current.GetCallbackChannel<MyServiceCallback>();
}
public static void SendMessage(string name, string message)
{
Clients[name].SendMessage(message);
}
}
[ServiceContract]
public interface MyServiceCallback
{
[OperationContract(IsOneWay = true)]
void SendMessage(string message);
}
Everything works well except when the application pool gets recycled.
Of course I realize that the static stuff is not ideal in that regard.
Where would be a good place to save my client references so that they survive an app pool recyle?
I don't think you can make themn survive an app pool recycle since for that you would need to persist and recreate the Channels...
A better approach would be IMHO to host your WCF service in a Windows Service (no app pool recycling) for this kind of stuff...