如何在 WCF 服务中维护每个客户端的状态?

发布于 2024-09-04 20:01:54 字数 349 浏览 2 评论 0原文

我创建了一个 WCF 服务,希望它能够在客户端调用之间维护状态。我认为最简单的方法是将此属性添加到服务中:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

因为这应该在客户端代理的生命周期内(或在极端情况下超时)为每个客户端保持单独的服务处于活动状态。我还添加了一个测试函数,用于跟踪用户输入列表,并在服务生命周期内输出包含所有输入的串联字符串。

当我在 Visual Studio 生成的测试客户端中运行此命令时,我发现用于保存过去数据的列表会在每次调用时重置。我还需要做些什么来维护每个会话的状态吗?

I've created a WCF service in which I would like it to maintain state between calls from the client. I figured the easiest way to do this was to add this attribute to the service:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]

since this is supposed to keep a separate service alive for each client over the life of the client proxy (or timeout in the extreme case). I also added a test function that tracks a list of user inputs, and spits out a concatenated string with all the inputs over the life of the service.

When I run this in the test client generated by visual studio, I find that the list I was using to hold past data is reset with each call. Is there something else I need to do to maintain state per session?

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

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

发布评论

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

评论(1

睫毛溺水了 2024-09-11 20:01:54

好吧,明白了。我使用的是 BasicHttpBinding,它不支持每个会话实例。我将其切换为 wsHttpBinding,然后一切正常。

Ok figured it out. I was using a BasicHttpBinding, which doesn't support per session instancing. I switched it to a wsHttpBinding, and presto, everything works.

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