相关疑难解决方法(0)

无法在控制台应用程序的"Main"方法上指定"async"修饰符

我是使用async修饰符进行异步编程的新手.我试图弄清楚如何确保我Main的控制台应用程序的方法实际上异步运行.

class Program
{
    static void Main(string[] args)
    {
        Bootstrapper bs = new Bootstrapper();
        var list = bs.GetList();
    }
}

public class Bootstrapper {

    public async Task<List<TvChannel>> GetList()
    {
        GetPrograms pro = new GetPrograms();

        return await pro.DownloadTvChannels();
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道这不是从"顶部"异步运行的.由于无法asyncMain方法上指定修饰符,如何在main异步中运行代码?

.net c# asynchronous console-application

415
推荐指数
10
解决办法
18万
查看次数

等待Async Void方法调用进行单元测试

我有一个看起来像这样的方法:

private async void DoStuff(long idToLookUp)
{
    IOrder order = await orderService.LookUpIdAsync(idToLookUp);   

    // Close the search
    IsSearchShowing = false;
}    

//Other stuff in case you want to see it
public DelegateCommand<long> DoLookupCommand{ get; set; }
ViewModel()
{
     DoLookupCommand= new DelegateCommand<long>(DoStuff);
}    
Run Code Online (Sandbox Code Playgroud)

我试图对它进行单元测试:

[TestMethod]
public void TestDoStuff()
{
    //+ Arrange
    myViewModel.IsSearchShowing = true;

    // container is my Unity container and it setup in the init method.
    container.Resolve<IOrderService>().Returns(orderService);
    orderService = Substitute.For<IOrderService>();
    orderService.LookUpIdAsync(Arg.Any<long>())
                .Returns(new Task<IOrder>(() => null));

    //+ Act
    myViewModel.DoLookupCommand.Execute(0);

    //+ Assert
    myViewModel.IsSearchShowing.Should().BeFalse(); …
Run Code Online (Sandbox Code Playgroud)

.net c# unit-testing async-await

63
推荐指数
6
解决办法
8万
查看次数

单元测试资源管理器不显示城域应用程序的异步单元测试

不确定这是一个已知问题.我正在使用VS2012 RC(Ultimate)和Win8 Release Preview.我创建了一个单元测试库(metro style app),并编写了一个单元测试,其中包括async/await关键字.但是,当我编译单元测试项目时,单元测试资源管理器没有显示我写的测试.如果我排除了async/await关键字,那么单元测试资源管理器会出现在我刚写的测试中.有没有人以前遇到过这个,还是仅仅是我?

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public async void SomeAsyncTest()
    {
        var result = await StorageFile.GetFileFromPathAsync("some file path");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# unit-testing async-await microsoft-metro

19
推荐指数
1
解决办法
3132
查看次数

Async /等待没有按预期反应

使用下面的代码我希望字符串"Finished"出现在控制台上的"Ready"之前.任何人都可以向我解释,为什么等待完成这个样本中的任务?

    static void Main(string[] args)
    {
        TestAsync();
        Console.WriteLine("Ready!");
        Console.ReadKey();
    }

    private async static void TestAsync()
    {
        await DoSomething();
        Console.WriteLine("Finished");
    }

    private static Task DoSomething()
    {
        var ret = Task.Run(() =>
            {
                for (int i = 1; i < 10; i++)
                {
                    Thread.Sleep(100);
                }
            });
        return ret;
    }
Run Code Online (Sandbox Code Playgroud)

c# async-await

6
推荐指数
2
解决办法
1万
查看次数