none
What is the scope of static webservice object RRS feed

  • Question

  • I have a console application which calls a webservice.In webservice we create a static object of it.
    Then we call two webmethods of it and the value retrieved in the following two Console statements are 1 and 2.
    I need to know where(Are they stored in code segment oe data segment) and how static variables are stored across various stateless webservice calls and why we got values as 1 and 2 and not 1 and 1.
     
    ConsoleApplication Code-----


    localhost.Service service = new localhost.Service();

    Console.WriteLine("DoSomethingWithStatic() returns: {0}", service.DoSomethingWithStatic());
    Console.WriteLine("DoSomethingElseWithStatic() returns: {0}", service.DoSomethingElseWithStatic());

    WebService Code------

    public class WebServiceObject
    {
        private int _data;

        public WebServiceObject()
        {
            _data = 0;
        }

        public int Data
        {
            get
            {
                return ++_data;
            }
        }
    }
    public class Service : System.Web.Services.WebService
    { private static WebServiceObject _staticObj;
     
    [WebMethod]
        public int DoSomethingWithStatic()
        {
            _staticObj = new WebServiceObject();
            return _staticObj.Data;
        }
        [WebMethod]
        public int DoSomethingElseWithStatic()
        {
           // _staticObj = new WebServiceObject();
            return _staticObj.Data;
        }
    }

    Tuesday, January 23, 2007 5:43 AM

Answers

  • Ok, I see better

    You caught me, man  I had never cared about code and data segments since my Assembly language matter in the University (that was more than 15 years ago, how fast the time goes by!!!)

    Anyway, I try to figure out but it's tricky because once you compile your components, you obtain your intermediate code (MSIL), which will be interpreted by the CLR. But the first time that code is interpreted by the CLR, is compiled into native code and cached for subsequent executions. So, after those tsunamis, I really have no clear idea about the final segment of static elements

    But I found for you two valuable sources of info:

    I guess everything, static or dynamic, may go to the data segment, as the code segment is where the garbage collector and other low level .NET framework services run. But I can't proof that, it's just my intuition

     

    About static classes and the Application object in ASP.NET, I can say that the Application object is a special case of static class. What you called it "Application variables", actually are entries of the indexer available in the Application static class, automatically created for you when an ASP.NET application is loaded. Nothing different than a static class: Application is a concrete static class

     

     

    (If you consider this post answer your question/s, pls tag it as USEFUL. Thanks!)

    Wednesday, January 24, 2007 10:05 PM

All replies

  • Hi CanUHelpMe,

       starting by the end, what it seems that really worries you about why you obtain

    DoSomethingWithStatic() returns: 1
    DoSomethingElseWithStatic() returns: 2

       as output instead of

    DoSomethingWithStatic() returns: 1
    DoSomethingElseWithStatic() returns: 1

       occurs because, in the declaration of DoSomethingElseWithStatic(), it seems to be commented the line which was expected to assign _staticObj with a newly created WebServiceObject instance. That way, the only command inside DoSomethingElseWithStatic() is returning the value got after calling the get() method of property Data, which in its turn adds 1 to _data field of the already created -when calling DoSomethingWithStatic()- instance. That _data field was created and assigned 0, but when returning from DoSomethingWithStatic(), its value was 1

       Because the invocation to DoSomethingElseWithStatic() finally uses the same instance, adding 1 to 1 gives as result 2, the value you saw thru the console

     

    (If you consider this solves the question, pls tag my answer as USEFUL in order to close the thread. That won't prevent anyone else to continue posting comments -- Thank you!)

    Tuesday, January 23, 2007 6:08 AM
  • Hi Diego,

    I fully understand what you said but my i basically want to know is where(Are they stored in code segment or data segment) and how static variables are stored across various stateless webservice calls.

    Similarly one more problem faced by me at times is how static variable is different by Application variable(used in asp.net like session variables).Are Application variables exists in IIS while static in application appdomain?How these are different in terms of storage and lifetime.

    Kindly help me getting answers to above two queries.

    Regards

    Wednesday, January 24, 2007 5:46 AM
  • Ok, I see better

    You caught me, man  I had never cared about code and data segments since my Assembly language matter in the University (that was more than 15 years ago, how fast the time goes by!!!)

    Anyway, I try to figure out but it's tricky because once you compile your components, you obtain your intermediate code (MSIL), which will be interpreted by the CLR. But the first time that code is interpreted by the CLR, is compiled into native code and cached for subsequent executions. So, after those tsunamis, I really have no clear idea about the final segment of static elements

    But I found for you two valuable sources of info:

    I guess everything, static or dynamic, may go to the data segment, as the code segment is where the garbage collector and other low level .NET framework services run. But I can't proof that, it's just my intuition

     

    About static classes and the Application object in ASP.NET, I can say that the Application object is a special case of static class. What you called it "Application variables", actually are entries of the indexer available in the Application static class, automatically created for you when an ASP.NET application is loaded. Nothing different than a static class: Application is a concrete static class

     

     

    (If you consider this post answer your question/s, pls tag it as USEFUL. Thanks!)

    Wednesday, January 24, 2007 10:05 PM