在C#中涉及秒表的可变范围

use*_*859 0 c# scope

我正在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Vic*_*aci 6

该服务是无状态的 - 意味着两个调用不会访问同一个实例.

如果要测量两次调用之间的时间,可以将时间存储在请求/响应中,或者将时间从一次调用保存在持久存储中,并在下次调用时检索它.

请参阅此处管理州的其他方式:http://www.codeproject.com/Articles/86007/ways-to-do-WCF-instance-management-Per-call-Per