I have this follow class that responsible to stream a huge list continuously (1000 records per flush)
What happened if the user close the browser while the connection still open and the loop not ended yet? Will the connection stay open? And why I get "This async method lack await operator"?
public async Task<HttpResponseMessage> SendList(int id)
{
var response = Request.CreateResponse();
response.Content = new PushStreamContent(async (stream, Content, context) =>
{
try
{
using (SqlConnection myConnection = new SqlConnection())
using (SqlCommand sqlCmd = new SqlCommand())
{
string conn = ConfigurationManager.ConnectionStrings[SRVTarget].ConnectionString;
myConnection.ConnectionString = conn;
sqlCmd.CommandType = System.Data.CommandType.Text;
sqlCmd.CommandText = string.Concat("SELECT * FROM [dbo].[fGetList](",id,")");
sqlCmd.Connection = myConnection;
myConnection.Open();
SqlDataReader reader = null;
reader = await sqlCmd.ExecuteReaderAsync();
string empl;
int j = 1;
string emp = string.Empty;
while (await reader.ReadAsync())
{
empl = string.Empty;
for (int i = 0; i < reader.FieldCount; i++)
{
empl = String.Concat(empl, reader.GetValue(i).ToString());
empl = String.Concat(empl, ',');
}
emp = String.Concat(emp, empl.Substring(0, empl.Length - 1), Environment.NewLine);
if (j % 1000 == 0 | reader.Read() == false)
{
using (var writer = new StreamWriter(stream, System.Text.Encoding.UTF8))
{
await writer.WriteAsync(emp);
await writer.FlushAsync();
}
emp = string.Empty;
};
j = j + 1;
}
myConnection.Close();
}
}
finally
{
stream.Close();
}
});
return response;
}