WCF Configuring server to remember data

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I currently have a service in WCF where it processes requests made by a client. However, it caches some data that the client sends it. It then does computations on the data.

At any point the client should be able to retrieve some of the data. It is at the discretion of the user (when a Button is clicked, an AJAX query is sent to get some of the data).

The problem I'm having is that as soon as another client connects, and starts sending requests, the data that the original client sent is now distorted.

I was wondering how I can resolve this. I've attempted to use sessions, as I was looking for some way I can instantiate my "server object" for each client that wants to connect to it. Still no luck.

This question is related to this: WCF Closing a connection/Releasing resources

回答1:

Sounds like maybe you're looking for a WCF Durable Service (link). Durable services can keep state between calls to a service.

Chapter 4 in Juval Lowy's Programming WCF Services (link) also has information about Durable Services as well as per-session services which might also help.

In general, though, it's considered good practice to make your WCF services stateless - i.e. don't hold onto any state between calls. Durable services accomplish this by persisting data, for example to a database between calls instead of actually keeping it in memory (which could be a bad thing if you have thousands of simultaneous service consumers).



回答2:

How is your service configured with regards to instancing/concurrency? Sounds like you might be using a singleton service instance, and storing the data in the service instance?

If so that explains your problem - that state is going to get overwritten by the next client who invokes a state changing operation.

As your client is a web browser, you are probably using the webHttpBinding which does not support WCF sessions.

One simple way to add your own session concept is to pass a session ID to your operation, which you can then use to lookup the relevant state for the requests.

If you want to get a bit more sophisticated you can investigate digging out cookie information from the WCF requests and use that for your session.

The most complicated solution is to use a custom WCF binding that supports the WCF session concept (built on top of either cookie or session parameters). Probably not worth the effort unless you can google a ready-made solution.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!