c#中涉及秒表的变量作用域
本文关键字:变量 作用域 | 更新日期: 2023-09-27 17:53:47
我正在visual studio中创建一个web服务。即使我在studentRead之后调用studentSave 1000毫秒,秒表返回0毫秒。我猜这和瞄准镜有关,但我看不出是什么!我做错了什么?
public class Service1 : IService1
{
Database db;
Stopwatch sw;
public Service1()
{
sw = new Stopwatch();
db = new Database();
}
public string StudentRead(int id)
{
sw.Start();
return db.getSentenceAtId(id);
}
public bool StudentSave(int id, int sentenceId, int acc, int speed)
{
sw.Stop();
System.Diagnostics.Debug.WriteLine("ElapsedMilliseconds: " + sw.ElapsedMilliseconds);
return db.saveStudentResult(id, sentenceId, acc, speed);
}
}
这个服务是无状态的——这意味着两个调用不会碰到同一个实例。
如果您想测量两个调用之间的时间,您可以将时间存储在请求/响应中,或者将一次调用的时间保存在持久存储中,并在下一次调用发生时检索它。
查看其他管理状态的方法:http://www.codeproject.com/Articles/86007/ways-to-do-WCF-instance-management-Per-call-Per