加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用StopWatch类测量C#/异步数据访问

发布时间:2020-12-15 07:50:53 所属栏目:百科 来源:网络整理
导读:我有以下代码,似乎Elapsed Milliseconds不准确: public async TaskActionResult Index() { try { var connString = RoleEnvironment.IsEmulated ? ConfigurationManager.ConnectionStrings["Poc"].ConnectionString : ConfigurationManager.ConnectionStrin
我有以下代码,似乎Elapsed Milliseconds不准确:
public async Task<ActionResult> Index()
    {
        try
        {
            var connString = RoleEnvironment.IsEmulated
                                 ? ConfigurationManager.ConnectionStrings["Poc"].ConnectionString
                                 : ConfigurationManager.ConnectionStrings["PocVm"].ConnectionString;

            var repository = new AccountRepository(connString);
            var stopWatch = new Stopwatch();
            stopWatch.Start();

            var accounts = await repository.GetAll();

            stopWatch.Stop();
            ViewBag.Accounts = accounts;
            ViewBag.VmStatus = stopWatch.ElapsedMilliseconds;
        }
        catch (Exception e)
        {
            blah blah blah...
        }


        return View();
    }

这看起来是正确的还是我错过了一些非常明显的东西?

解决方法

这对我来说完全没问题.

如果Repository.GetAll方法不是异步的话,可能会出现错误,希望它有这样的签名:

public async Task<IEnumerable<Account>> GetAll();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读