开发者

ASP.NET web service- restrict some concurrent instances based on validation

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 开发者_如何学JAVAcan 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)


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");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜