不能检查变量当调试.NET异步/等待功能 [英] Can't Inspect Variables When Debugging .NET Async/Await Functions

查看:189
本文介绍了不能检查变量当调试.NET异步/等待功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 A .NET 4.5凸出异步/的await 功能。

当我尝试检查/快速监视的等待后声明变量引用,我得到如下:


  

名称ID不在当前情况下存在


知道如何解决这个问题,所以我可以调试呢?

Edit--这里的code

  [事实]
    公共异步任务works_as_expected()
    {
        VAR回购= Config.Ioc.GetInstance< IAsyncRepository<客户>>();        VAR工作= Config.Ioc.GetInstance< IUnitOfWork>();
        客户C =新客户()
        {
            名字=__Micah
            姓氏=__Smith_test
            dateCreated会= DateTime.Now,
            DateModified = DateTime.Now,
            电子邮件=m@m.com
            手机=7245551212
        };        VAR ID =等待repo.Insert(C);
        //我不能检查id的值
        Assert.True(ID大于0);
    }


解决方案

该变量尚不存在,所以没有办法检查它的价值。它没有价值(还)。

它不会被定义,在技术上,直到你达到code的定义它的行,它不会有一个值,直到初始化。虽然技术上可以检查之前,它在某些情况下,曾经分配一个变量的值,该值永远不能在code访问(外面不安全code的),所以没有什么理由来关注一下吧。

至于什么时候该位置的内存将被分配(这是当您第一次有在调试器看着它的选项),这将是你已经达到在最后的第一行后,等待使用变量之前。

I have a .NET 4.5 proj that is using async/await functionality.

When i try to inspect/quickwatch a variable reference after an await statement, i get the following:

The name 'id' does not exist in the current context

Know how to fix this so i can debug it?

Edit-- here's the code

    [Fact]
    public async Task works_as_expected()
    {
        var repo = Config.Ioc.GetInstance<IAsyncRepository<Customer>>();

        var work = Config.Ioc.GetInstance<IUnitOfWork>();
        Customer c= new Customer()
        {
            FirstName = "__Micah",
            LastName = "__Smith_test",
            DateCreated = DateTime.Now,
            DateModified = DateTime.Now,
            Email = "m@m.com",
            Phone = "7245551212"
        }; 

        var id=await repo.Insert(c);
        // i can't inspect the value of id
        Assert.True(id > 0);
    }

解决方案

That variable doesn't exist yet, so there is no way to inspect it's value. It has no value (yet).

It won't be defined, technically, until you reach the line of code that defines it, and it won't have a value until you initialize it. While you can technically inspect the value of a variable before it is ever assigned in some cases, that value can never be accessed in code (outside of unsafe code) so there's little reason to look at it.

As for when the memory for that location will be allocated (which is when you first have the option of looking at it in the debugger) that will be after you have reached the first line after the last await before the variable is used.

这篇关于不能检查变量当调试.NET异步/等待功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆