Multiple calls to same wcf service method from Silverlight application
-
I am using a Silverlight enabled WCF Service with the following attribute:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
It has a method say
[OperationContract]
public IEnumerable GetAllEntities();Sometimes I want to call this method multiple times within short span of time.. as WCF calls from Silverlight have to be asynchromous, the second call ends up getting called before the first one returned with the results. This results into the UserState getting corrupted. I find that the UserState of one of the calls gets overwritten by that of another call. I find this problem when the calls return and I extract the UserState from the EventArgs. This seems to be a concurrency problem. I know we should avoid design which causes such multiple calls in Silverlight, but in some cases you just have to. Is there any workaroung/ solution to this problem?
Pankaj Chamria
-
I am using a Silverlight enabled WCF Service with the following attribute:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
It has a method say
[OperationContract]
public IEnumerable GetAllEntities();Sometimes I want to call this method multiple times within short span of time.. as WCF calls from Silverlight have to be asynchromous, the second call ends up getting called before the first one returned with the results. This results into the UserState getting corrupted. I find that the UserState of one of the calls gets overwritten by that of another call. I find this problem when the calls return and I extract the UserState from the EventArgs. This seems to be a concurrency problem. I know we should avoid design which causes such multiple calls in Silverlight, but in some cases you just have to. Is there any workaroung/ solution to this problem?
Pankaj Chamria
-
I am using a Silverlight enabled WCF Service with the following attribute:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
It has a method say
[OperationContract]
public IEnumerable GetAllEntities();Sometimes I want to call this method multiple times within short span of time.. as WCF calls from Silverlight have to be asynchromous, the second call ends up getting called before the first one returned with the results. This results into the UserState getting corrupted. I find that the UserState of one of the calls gets overwritten by that of another call. I find this problem when the calls return and I extract the UserState from the EventArgs. This seems to be a concurrency problem. I know we should avoid design which causes such multiple calls in Silverlight, but in some cases you just have to. Is there any workaroung/ solution to this problem?
Pankaj Chamria
Just asking....the UserState you refer to is your object right? Like Abhinav S mentioned, you need to chain the asynchronous calls.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
I am using a Silverlight enabled WCF Service with the following attribute:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
It has a method say
[OperationContract]
public IEnumerable GetAllEntities();Sometimes I want to call this method multiple times within short span of time.. as WCF calls from Silverlight have to be asynchromous, the second call ends up getting called before the first one returned with the results. This results into the UserState getting corrupted. I find that the UserState of one of the calls gets overwritten by that of another call. I find this problem when the calls return and I extract the UserState from the EventArgs. This seems to be a concurrency problem. I know we should avoid design which causes such multiple calls in Silverlight, but in some cases you just have to. Is there any workaroung/ solution to this problem?
Pankaj Chamria
You mean the UserState on the server, right? Either you need to write the service so that it can cope with being run concurrently, or wrap the content of that method with lock(UserState){ ... }. (You don't want to use [Synchronized] because it should be able to run concurrently for different users, even if making it work concurrently for the same one is too hard.) Obviously, the first way is preferred. I'm guessing your method is actually not just a 'GetAllXxx' but instead updates some server state, because there should be no problem with doing multiple gets in parallel, and also you would not need to call that multiple times (the result could be cached). Can you explain a bit more about what the service method is doing and why you need to call it multiple times? I would expect that it would be possible to bundle the requests up into a single service call in some way, reducing network traffic as well as reducing your concurrency issues. But you should still write a robust service regarding concurrency.