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

发布于 2024-08-25 01:33:01 字数 881 浏览 4 评论 0原文

我有一个自托管 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();
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最终幸福 2024-09-01 01:33:01

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

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

请参阅以下链接:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文