Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

instantiating an object from a web service vs instantiating an object from a regular class

I have a very basic web service:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService1
{        
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        public int myInt = 0;

        [WebMethod]
        public int increaseCounter()
        {
            myInt++;
            return myInt;
        }

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

    }
}

when I run that project my browser opens showing me the service: enter image description here


on a different solution: (console application)

I am able to connect to that service by adding the reference:

enter image description here

enter image description here

then click on the add web reference button: enter image description here

Lastly I type the url of the service I just created: enter image description here

Now I am able to instantiate an object from the class Service1 from my console application as:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            localhost.Service1 service = new localhost.Service1();

            // here is the part I don't understand..
            // from a regular class you will expect myInt to increase every time you call
            // the increseCounter method. Even if I call it twice I always get the same result.

            int i;
            i=service.increaseCounter();
            i=service.increaseCounter();


            Console.WriteLine(service.increaseCounter().ToString());
            Console.Read();


        }
    }
}

why does myInt does not increase every time I call the increaseCounter method? every time I call that method it returns 1.

like image 290
Tono Nam Avatar asked Sep 28 '11 20:09

Tono Nam


People also ask

What does it mean to instantiate an object from a class?

Note: The phrase "instantiating a class" means the same thing as "creating an object." When you create an object, you are creating an "instance" of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor.

What are the two main ways to instantiate an object?

There are three different ways of instantiating an object through constructors: Through Default constructors. Through Parameterized constructors. Through Copy constructors.

What is the difference between instantiation and initialization?

Initialization: Assigning a value to a variable is called initialization. For example, cost = 100. It sets the initial value of the variable cost to 100. Instantiation: Creating an object by using the new keyword is called instantiation.

What does it mean to instantiate an object C#?

The process of creating an object from a class is called instantiation because an object is an instance of a class. Now that you have defined a new class type, it is time to instantiate an object of that type. Mimicking its predecessors, C# uses the new keyword to instantiate an object (see Listing 5.3).


1 Answers

Services created through the older .asmx technology are not singleton instances. This means that each call you make to the server instantiates a new instance of the service each time. Two real solutions, either use static variables (eugh....), or switch to using WCF.

like image 84
Matthew Abbott Avatar answered Sep 21 '22 00:09

Matthew Abbott