Acessing same class instance from 2 seperate webmethods

Discussion in 'ASP .Net Web Services' started by TiGeR79, Sep 19, 2006.

  1. TiGeR79

    TiGeR79 Guest

    Hi,
    i've been developing a webservice with 2 seperate webmethods.
    The idea is that i have a class which is able to generate a code and also
    able to make an image of this code.
    Now i'd like to have 2 webmethods ; one which can be called to generate (and
    return) the code and one to generate (and return) the image.

    The problem im experiencing is that i first make an instance of this class,
    then i call the first webmethod (generate code which works fine) after that i
    call the second webmehod but instead of making an image out of the code i get
    an error because it lost the code. If i place the call to the imagemaking
    method in the first webmethod (together with the codegenerating call) it all
    works.

    Example :

    Class UniqueCode

    private string uniqueCode;

    public void makeUniqueCode()
    {
    uniqueCode = blah blah my code to make an uniqueid;
    }

    public Bitmap makeImageFromCode()
    {
    blah blah my code to make an image of the uniqueCode string;
    return uniqueCodeImage;
    }

    now the webservice :

    UniqueCode test = new UniqueCode()

    [WebMethod]
    public String getCode()
    {
    string anyCode = test.MakeUniqueCode();
    return anyCode;
    }

    [WebMethod]
    public Bitmap getImage()
    {
    Bitmap testImage = test.makeImageFromCode();
    return testImage;
    }


    any idea how i can solve my problem ? seems like some scope issue or
    something, the instance of the class exists (with its global variable
    uniquCode) during the call to the first webmethod but seems like it gets
    reset befor the call to the second webmethod....
     
    TiGeR79, Sep 19, 2006
    #1
    1. Advertising

  2. TiGeR79

    TiGeR79 Guest

    First of all thanks for answering...
    Unfortunately I have no experience at all managing cached data...
    This is my first big project implementing webservices...
    So would you be so kind to point me to some information about cache
    managment and/or cache retrieval as you mean it ?
    Thanks in advance...

    "Gaurav Vaish (www.EduJiniOnline.com)" wrote:

    > > any idea how i can solve my problem ? seems like some scope issue or
    > > something, the instance of the class exists (with its global variable
    > > uniquCode) during the call to the first webmethod but seems like it gets
    > > reset befor the call to the second webmethod....

    >
    > Everytime you connect to a web-service, a new instance of the class is made
    > and the corresponding WebMethod is invoked.
    >
    > They will not be able to share the states unless you have a mechanism to do
    > it yourself.
    > One trivial, but bad, way is to use Sessions.
    >
    > Another way is to do the following:
    >
    > 1. While returning the code also return a unique identifier
    > 2. Use this unique identifier to track the code generated (cache it on the
    > server side etc)
    > 3. For the generation of the image, take this UID as a parameter using which
    > you should get the code generated (say, from the cache) to generate the
    > image.
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | http://www.mastergaurav.com
    > http://www.edujinionline.com
    > http://articles.edujinionline.com/webservices
    > -------------------
    >
    >
    >
     
    TiGeR79, Sep 20, 2006
    #2
    1. Advertising

  3. TiGeR79

    TiGeR79 Guest

    I will definitely have a look at it during the weekend or beginning next
    week...
    I guess the link ur referring to is the third one right ?
    http://articles.edujinionline.com/webservices


    "Gaurav Vaish (www.EduJiniOnline.com)" wrote:

    > > Unfortunately I have no experience at all managing cached data...
    > > This is my first big project implementing webservices...

    >
    > Look at the namespace "System.Web.Caching".
    >
    > This question interested me... I would be putting up a solution to this on
    > the 'webservices articles' (see link below), probably by the weekend. If you
    > can wait... it may prove good :)
    >
    >
    > --
    > Happy Hacking,
    > Gaurav Vaish | http://www.mastergaurav.com
    > http://www.edujinionline.com
    > http://articles.edujinionline.com/webservices
    > -------------------
    >
    >
    >
     
    TiGeR79, Sep 22, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?YnJhZHRt?=
    Replies:
    11
    Views:
    10,018
    binarynation
    Nov 15, 2006
  2. Sridhar R
    Replies:
    14
    Views:
    1,422
    =?iso-8859-1?Q?Fran=E7ois?= Pinard
    Feb 10, 2004
  3. moondaddy
    Replies:
    2
    Views:
    590
    Steven Cheng[MSFT]
    Oct 16, 2006
  4. Martin P. Hellwig
    Replies:
    1
    Views:
    380
    Martin P. Hellwig
    Mar 26, 2010
  5. Eric Porter
    Replies:
    0
    Views:
    106
    Eric Porter
    Jul 1, 2004
Loading...

Share This Page