Question

I have a self-hosted WCF service with the InstanceContextMode set to PerSession.
How can I detect new client connections (sessions) to my service from the host application and use that new session context to observe my service through its events?

Something like:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService {
    public event EventHandler ClientRegistered;
    public event EventHandler FileUploaded;
}

and from my host application to be able to do:

ServiceHost svc = new ServiceHost(typeof(MyService));
svc.Open();

// something like:
svc.NewSession += new EventHandler(...)

//...

public void SessionHandler(InstanceContext SessionContext) {
    MySessionHandler NewSessionHandler = new MySessionHandler(SessionContext);

    // From MySessionHandler I handle the service's events (FileUploaded, ClientRegistered) 
    // for this session and notify the UI of any changes.
    NewSessionHandler.Handle();
}
Was it helpful?

Solution

You can use IsInitiating in the service contract

[OperationContract(IsInitiating = true)]
   void FirstMethod();

See the following link:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/8137553a-8657-475e-b9ca-5914d9c9d57a

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top