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

旧时模样 提交于 2019-12-19 03:02:22

问题


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:


on a different solution: (console application)

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

then click on the add web reference button:

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

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.


回答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.




回答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 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...




回答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.



来源:https://stackoverflow.com/questions/7589301/instantiating-an-object-from-a-web-service-vs-instantiating-an-object-from-a-reg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!