views:

39

answers:

1

I have an asp.net web service running on a web server with one of the web methods (wm) that does some processing based on a parameter (param).

I want to restrict concurrent calls to this web method only in certain cases--namely when the value of param passed by client1 is equal to param passed by client2.

I was thinking of adding some validation to the beginning of wm to check for the conditions before processing starts.

My questions are: 1) How can I find out from within my web method, if there is another instance of the web service calling the same web method being executed. 2) How can I get access to the parameters passed to my webmethod across various concurrent instances of the webservice while they are running.

I want to avoid database logging because in case the server goes down the log may not be updated (unless there is a good way to deal with this possibility)

Thanks for your help.

A: 

IMHO web services should be stateless and you have to avoid doing such sort of things. Otherwise from the tip of my head (not tested):

[WebMethod]
public void SomeMethod(string p)
{
    var cache = HttpContext.Current.Cache;
    // Be careful with the equality comparison here
    // it might not work with complex types
    if (cache["param"] == p)
    {
        throw new Exception("Another client has called this method with same argument");
    }
    cache["param"] = p;
    try
    {
        // Execute your method here
    }
    finally
    {
        cache.Remove("param");
    }
}
Darin Dimitrov
Thank you very much Darin!
Jack B