locked
Create a counter....

    Pergunta

  • Hi to all, i'm very glad that the forum is on again!!
    well i'm very very very newbie in Web Services Tech and i want your help.......

    I made a web service that return the ++counter. The problem is with this code always return the value 1. That i want is every time  i call the service return the last value of counter++.

    example: let's say that i called the service six times. On 7th  i want the returned value be 7 not 1.


    Code Snippet

    public int  Counter()
        {
            int counter = 0;
            return ++counter;
       }



    Where can i store the value of counter variable to keep the value?

    please if you have any idea update me.....


    thanks, frikouflios.
    quinta-feira, 29 de maio de 2008 11:58

Respostas

  • hi,

    You can keep the Couter as a Static Variable, this is not adviceable. This value will be there till you reset the IIS. Another problem for this method is, if there is a Load balaceing present you cannot get proper couter value.

    Regards

    Deepu
    Deepu Chandran
    • Marcado como Resposta frikouflios terça-feira, 3 de junho de 2008 12:15
    segunda-feira, 2 de junho de 2008 08:24
  • Ok, I see that your subject is appropriate (and your English is pretty good).

    You say that you need this to be a unique counter. If you need it to be unique, would a GUID work for you? It's not as pretty as a counter, but it would certainly be unique.

    If you really need a counter, then I do suggest using a small database. You could perform the increment of the counter within a transaction so that there will be no possibility of duplicate counter values or anything like that. You could use SQL Server 2005 Express, which is free.
    John Saunders
    • Marcado como Resposta frikouflios terça-feira, 3 de junho de 2008 12:14
    terça-feira, 3 de junho de 2008 11:51

Todas as Respostas

  • First of all, your code wouldn't work even if it weren't running in a service. Your counter is in a local variable, which gets a new instance every time the method is entered, and you initialize this instance to zero.

     

    Second, web services are best kept stateless, so the kind of thing you're trying to do is probably a mistake.

     

    Third, even if you did it "properly", you would be making a mistake. Each call to your web service is handled on a separate thread. If you have multiple requests doing ++counter at the same time, then you're going to lose (at least) one count. It's a classic thread safety issue, and you would need to lock the counter.

     

    Fourth (are you beginning to notice a pattern here?), such state is not persistant. Any number of things can cause the service to be reset, losing the state you set.

     

    If you are simply trying to learn web services, then you should teach yourself to stay away from state. Each call to your service should be independent of previous calls.

     

    quinta-feira, 29 de maio de 2008 12:40

  • Hi John, thanks for the fast response. I found your notes very interest.... and i think the key words are "Your counter is in a local variable" and "Each call to your service should be independent of previous calls".  I will make a research with those terms....... i hope i 'll find the solution.... if you know any resource please inform me

    thanks, frikouflios
    sexta-feira, 30 de maio de 2008 09:31
  • Please let us know if you have any more questions.
    John Saunders
    segunda-feira, 2 de junho de 2008 01:37
  • hi,

    You can keep the Couter as a Static Variable, this is not adviceable. This value will be there till you reset the IIS. Another problem for this method is, if there is a Load balaceing present you cannot get proper couter value.

    Regards

    Deepu
    Deepu Chandran
    • Marcado como Resposta frikouflios terça-feira, 3 de junho de 2008 12:15
    segunda-feira, 2 de junho de 2008 08:24
  • hello again, i set my variable as static and it is working but a have the problems that Deepu said before..... I think i have to store the current value somewhere (in a file is good solution?). Because i want to integrate the Web Service with InfoPath and SharePoint maybe i will store the value in a  SharePoint List... it's good idea? Do you have better solution?


    thanks for your time, frikouflios.
    segunda-feira, 2 de junho de 2008 10:46
  • It depends on what you're trying to accomplish. If you give us more information on what you're trying to accomplish, we may be able to help.

    I doubt that you're really trying to accomplish a counter, I suggest you start a new thread with a title that reflects the next questions you have.
    John Saunders
    segunda-feira, 2 de junho de 2008 14:58
  • i prefer to not open a new thread but to give more information about what i' m trying to do.

    scenario:

    we have an InfoPath 2007 form that we can fill it via the browser and save it in a SharePoint Form List.

    so far so good.....

     The problem is that in the form i have a field (like ID) that it should  fill with a unique number (autonumber). Because i have more than one InfoPath form with this field, i want to  create a counter that can share all this forms.  So every time that the form it opens or it is submitted (I have not decided yet) i can call a Web Service (the counter) and fill this field with a unique auto number....


    I want to thank you for your help, for the time that you allocated for my thread and my apology for ugly English, frikouflios

     

    terça-feira, 3 de junho de 2008 08:02
  • Ok, I see that your subject is appropriate (and your English is pretty good).

    You say that you need this to be a unique counter. If you need it to be unique, would a GUID work for you? It's not as pretty as a counter, but it would certainly be unique.

    If you really need a counter, then I do suggest using a small database. You could perform the increment of the counter within a transaction so that there will be no possibility of duplicate counter values or anything like that. You could use SQL Server 2005 Express, which is free.
    John Saunders
    • Marcado como Resposta frikouflios terça-feira, 3 de junho de 2008 12:14
    terça-feira, 3 de junho de 2008 11:51
  • hi John, i should admit that your idea for the GUID  is incredible. I do not know if it makes in my case but for  future use it is very good!!! I think your proposal for small database is the best way because i can use the transaction, something that SharePoint  doesn't support automatically....

    thanks for the comments and  suggestions.

    frikouflios

    terça-feira, 3 de junho de 2008 12:14