如何在 WCF 服务中维护每个客户端的状态?
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,明白了。我使用的是 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.