WCF: Comment détecter de nouvelles connexions aux services WCF PerSession?
-
19-09-2019 - |
Question
J'ai un service WCF auto-hébergé avec le InstanceContextMode mis à PerSession.
Comment puis-je détecter les nouvelles connexions (sessions) à mon service de l'application hôte et utiliser ce nouveau contexte de la session d'observer mon service à travers ses événements?
Quelque chose comme:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService {
public event EventHandler ClientRegistered;
public event EventHandler FileUploaded;
}
et de mon application hôte pour pouvoir faire:
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();
}
La solution
Vous pouvez utiliser IsInitiating dans le contrat de service
[OperationContract(IsInitiating = true)]
void FirstMethod();
Voir le lien suivant:
http: / /social.msdn.microsoft.com/Forums/en-US/wcf/thread/8137553a-8657-475e-b9ca-5914d9c9d57a
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow