Pregunta

He estado jugando con el código de demostración de este artículo de MSDN por Jeffrey Richter .

He añadido una nueva función a sus ApmToCcrAdapters para manejar la SqlCommand.BeginExecuteReader. Sólo se está cerrando el lector antes de que pueda leerlo.

El siguiente código se utiliza para proporcionar un 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"]);
       }
   }

Lo cual a su vez requiere el siguiente código:

   /// <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);
   }
¿Fue útil?

Solución

La conexión debe permanecer abierta para que el lector funcione. Creo que el cierre de la conexión es su problema. Dejar la conexión abierta y llamar a disponer en el lector cuando se hace y yo creo que debe limpiar la conexión.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top