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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

Becaue on the server side the class is created and disposed with EVERY call you make from the client… your client is just a “proxy” and doesn’t correspond directly to an instance on the server side…

You can either make myInt static or make the server side service class a Singleton… both options would mean that myIntis shared across ALL client… or you could implement some session management to achieve a client-specific myIntusing WCF for the server side seems IMHO the best solution – it comes with configurable options for singleton, session management etc.

EDIT – as per comments:

With WCF you can have .NET-clients with session management which in turn allows you to have different (client-specific) values for myInt

Method 3

webservice instance is destroyed at the end of each method call, so that’s why you always get the same result. You need some way to persist that value.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x