非同期SqlCommandオブジェクトとCCR
-
21-08-2019 - |
質問
私はジェフリー・リヒターによって、このMSDNの記事からデモ・コードで遊んでてきましたに。
私はSqlCommand.BeginExecuteReaderを処理するために彼のApmToCcrAdaptersに新しい機能を追加しました。唯一のそれは私がそれを読むことができる前に、読者を閉じるされます。
次のコードはFromIteratorHandlerを提供するために使用されます:
private static IEnumerator<ITask> AsyncReaderDemoHandler()
{
SqlDataReader reader = null;
SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=BizData;Integrated Security=True;Async=True;");
string query = "SELECT * FROM Account;";
SqlCommand command = new SqlCommand(query,connection);
connection.Open();
yield return Arbiter.Choice(ApmToCcrAdapters.GetReader(command),
delegate(SqlDataReader r) { Msg("Got SQL data"); reader = r; },
delegate(Exception e) { Msg("Failed to get SQL data"); });
connection.Close();
if (reader == null) yield break;
//This is where the code fails: Reader is Closed!
while (reader.Read())
{
Console.WriteLine(reader["Account"]);
}
}
順番に次のコードを呼び出すます:
/// <summary>
/// Gets the Reader, requires connection to be managed
/// </summary>
public static PortSet<SqlDataReader, Exception> GetReader(SqlCommand sqlCommand)
{
Port<SqlDataReader> portResponse = null;
Port<Exception> portException = null;
GetReaderResponse(sqlCommand, ref portResponse, ref portException);
return new PortSet<SqlDataReader, Exception>(portResponse, portException);
}
// Wrapper for SqlCommand's GetResponse
public static void GetReaderResponse(SqlCommand sqlCom,
ref Port<SqlDataReader> portResponse, ref Port<Exception> portException)
{
EnsurePortsExist(ref portResponse, ref portException);
sqlCom.BeginExecuteReader(ApmResultToCcrResultFactory.Create(
portResponse, portException,
delegate(IAsyncResult ar) { return sqlCom.EndExecuteReader(ar); }), null);
}
解決
リーダーが機能するために、
接続が開いたままにしなければなりません。私は、接続を閉じることは問題であると考えています。接続を開いたままにして行われ、私はそれが接続をクリーンアップするべきだと思うとき、読者に処分を呼び出します。
所属していません StackOverflow