инстанцирование объекта из веб-сервиса vs инстанцирование объекта из обычного класса

У меня есть очень простой веб-сервис:

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";
        }

    }
}

когда я запускаю этот проект, мой браузер открывается, показывая мне сервис: enter image description here


на другом решении: (консольное приложение)

я могу подключиться к этой службе, добавив ссылку:

enter image description here

enter image description here

затем нажмите на кнопку add web reference: enter image description here

Наконец, я ввожу url только что созданной службы: enter image description here

Теперь я могу создать объект класса Service1 из моего консольного приложения следующим образом:

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();


        }
    }
}

почему myInt не увеличивается каждый раз, когда я вызываю метод increaseCounter? Каждый раз, когда я вызываю этот метод, он возвращает 1.

6
задан Tono Nam 28 September 2011 в 20:37
поделиться