开发者

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

开发者 https://www.devze.com 2023-04-08 21:42 出处:网络
I have a very basic web service: using System; using Syste开发者_如何学运维m.Collections.Generic;

I have a very basic web service:

using System;
using Syste开发者_如何学运维m.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:

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


on a different solution: (console application)

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

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

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

then click on the add web reference button:

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

Lastly I type the url of the service I just created:

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

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.


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.


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 myInt... using 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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号