我有设定为PerSession的InstanceContextMode自托管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();
}
有帮助吗?

解决方案

可以在服务合同使用IsInitiating

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

请参阅下面的链接:

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top