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

时间:2014-03-25 22:50:56

标签: 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);
    }
}

1 个答案:

答案 0 :(得分:6)

该服务是无状态的 - 意味着两个调用不会遇到同一个实例。

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

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