WCF:如何检测到WCF PerSession服务的新连接?

时间:2010-03-14 11:37:08

标签: .net service wcf wcf-sessions

我有一个自托管的WCF服务,InstanceContextMode设置为PerSession 如何从主机应用程序检测到我的服务的新客户端连接(会话),并使用该新会话上下文通过其事件来观察我的服务?

类似的东西:

[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();
}

1 个答案:

答案 0 :(得分:3)

您可以在服务合同中使用IsInitiating

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

请参阅以下链接:

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