【发布时间】:2011-09-28 20:37:49
【问题描述】:
我有一个非常基本的网络服务:
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";
}
}
}
当我运行该项目时,我的浏览器会打开,向我显示服务:
在不同的解决方案上:(控制台应用程序)
我可以通过添加引用连接到该服务:
然后点击添加网页参考按钮:
最后我输入刚刚创建的服务的 url:
现在我可以从我的控制台应用程序中从 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();
}
}
}
为什么每次调用 increaseCounter 方法时 myInt 都不增加?每次我调用该方法时,它都会返回 1。
【问题讨论】:
-
+1 以获得对问题的彻底解释。
标签: c# asp.net webrequest