OpenAsync挂起应用程序

本文关键字:应用程序 挂起 OpenAsync | 更新日期: 2023-09-27 18:11:26

我正在写一个简单的类,它将连接到SQL DB并从中获取数据。

我想让它异步,但我有一些问题与异步编程。

代码:

public async Task<ICommand> ExecuteAsync(SqlConnection connection)
{
    var cmd = new SqlCommand(Query);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    if(connection.State != ConnectionState.Open)
        await connection.OpenAsync();
    using (SqlDataReader sqlDataReader = await cmd.ExecuteReaderAsync())
    {
        if (sqlDataReader.HasRows)
        {
            while (await sqlDataReader.ReadAsync())
            {
                Entry = new Entry();
                Entry.ID = (int) sqlDataReader["ID"];
                Entry.User = (string) sqlDataReader["UserName"];
                object o = sqlDataReader["EntryType"];
                Entry.EntryType = o.Equals("Enter") ? EntryType.Enter : EntryType.Leave;
                Entry.DateTime = (DateTime) sqlDataReader["EntryDate"];
            }
        }
    }

使用此代码,调试器总是在OpenAsync()方法之后停止执行。不按下一条语句

你能告诉我我做错了什么吗?

——编辑——我现在正在桌面上运行它(简单的单元测试)我添加了try-catch来处理异常。

我的最小示例:

ExecuteAsync方法:

public async Task<ICommand> ExecuteAsync(SqlConnection connection)
{
    var cmd = new SqlCommand(Query);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;
    if(connection.State != ConnectionState.Open)
        await connection.OpenAsync().ConfigureAwait(false);
    using (SqlDataReader sqlDataReader = await cmd.ExecuteReaderAsync().ConfigureAwait(false))
    {
        if (sqlDataReader.HasRows)
        {
            while (await sqlDataReader.ReadAsync().ConfigureAwait(false))
            {
                Entry = new Entry();
                Entry.ID = (int) sqlDataReader["ID"];

// 条目。User = (string) sqlDataReader["UserName"];//object0 = sqlDataReader["EntryType"];// 条目。EntryType = o.Equals("Enter") ?EntryType。输入:EntryType.Leave;// 条目。DateTime = (DateTime) sqlDataReader["EntryDate"];}}}返回;}

调用这个方法:

    public void ExecuteCommandAsync(ICommand command, ReadFinished continueWith)
    {
        if(continueWith == null)
            throw new NullReferenceException("Parameter 'continueWith' cannot be null");
        command.ExecuteAsync(_connection).ContinueWith(task => continueWith(task.Result)).ConfigureAwait(false);
    }

我的测试案例:

    public void TestMethod1()
    {
        TimeTableDBConnector.DbConnector connector = new DbConnector(null);
        var getEntryByIDCommand = new GetEntryByIDCommand(1);
        ICommand result;
        try
        {
            connector.ExecuteCommandAsync(getEntryByIDCommand, ContinueWith );
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }

    }
    private void ContinueWith(ICommand command)
    {
        GetEntryCommand cmd = (GetEntryCommand) command;
    }

OpenAsync挂起应用程序

大多数测试框架支持异步测试,所以如果您更改ExecuteCommandAsync方法以返回Task,您应该能够将您的测试标记为asyncawait,即ExecuteCommandAsync的结果。试试吧:

async void TestMethod1()
{
    TimeTableDBConnector.DbConnector connector = new DbConnector(null);
    var getEntryByIDCommand = new GetEntryByIDCommand(1);
    ICommand result;
    try
    {
        await connector.ExecuteCommandAsync(getEntryByIDCommand, ContinueWith );
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}