Can't Inspect Variables When Debugging .NET Async/Await Functions

Deadly 提交于 2019-12-22 04:30:16

问题


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);
    }

回答1:


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.



来源:https://stackoverflow.com/questions/15116210/cant-inspect-variables-when-debugging-net-async-await-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!