Keep same State between queries

Hi,

Sample code is worth 1000 words, here is how I Proceed (It's obviously a bad method)

public class Service : System.Web.Services.WebService{

int i;
public Service () {
this.i=12;
}

[WebMethod]
public int HelloWorld() {
this.i++;
return i;
}
}

HelloWorld always returns 13, althought I want it to be incremented (13, 14, 15,..), even if there is several simultanous client request.

I wan't a session-like method, but the application should have the same state for each Client.

How can I do that

Thanks in advance



Answer this question

Keep same State between queries

  • Adam_W

    Hi,

    First let me say that web services by default are state-less.

    There is no out of the box Web Service that can provide you with this functionality.

    That means that we need to work around in order to perform any caching operation.

    There are several option at your disposle:

    1) You can create a storage for any variables that you wish to keep states for, such as an xml file.

    2) You can use a database.

    3) Create custom Web Service.

    4) Modify you architecture design.

    In order to help you i must understand the architecture design in you application,

    However i hope this helps.



  • man_25

    Perhaps .Net Remoting will be a great solution for my problem.


  • Kay Chan

    It is possible to have Session state in .NET Web Services but I am not sure how you can have the same Session state for each client as that is something that regular Session does not do!

    maybe take a look at this.


  • IdahoErick

    WebServices are executed in the context of an ASP.Net application, thus if you create a static field in your class its value should remain the same for each webservice instance.



  • pecasis

    Well why dont you create a statefull server (com+ or nt service) and the api is the web services that continue being state-less that will invoke methods on the statefull server.

    A bit complex ...

    I don't know any good documentation but a good place to start is the codeproject:

    www.codeproject.com

    hope this helps.



  • Javier Canones

    Arg ... state-less ... that's exactly what I didn't wanted to hear 

    But yes, that was helpful

    • My application is doing many path searches in a huge graph.
    • That graph is stored in a Database.
    • When I first launch the app, all the graph is loaded from the database to a local variable (a minimalistic hashmap, optimised for search purpose).
    • Then, for each client request, calculations are done directly in the hashmap (extremely quickly, and no need to connect to the database).

    Hope the explanation was comprehensible (I'm not really fluent in English).

    So, the solution number 1) and 2) seems not to be really appropriate for my application

    Is it hard to implement number 3) (is there good documentations on the Net)

    Have you ideas about th number 4)

    Any other ideas


  • Keep same State between queries