我有一个将 InstanceContextMode 设置为 PerSession 的自托管 WCF 服务。
如何从主机应用程序检测到我的服务的新客户端连接(会话)并使用该新会话上下文通过其事件观察我的服务?
就像是:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService {
public event EventHandler ClientRegistered;
public event EventHandler FileUploaded;
}
并从我的主机应用程序中能够做到:
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();
}