I want to implement an async
function. My problem is; I do not know where to put the await key word in the code below:
public async Task<List<ManualReadTag>> GetManuallyReadTags(ParameterManualTags model)
{
var db = new ApplicationDbContext();
using (var cnxn = db.Database.Connection)
{
cnxn.Open();
var cmd = cnxn.CreateCommand();
cmd.CommandText = "GetManualReadForDedicated";
cmd.CommandType = CommandType.StoredProcedure;
var dtFrom = cmd.CreateParameter();
dtFrom.ParameterName = "@DateFrom";
dtFrom.DbType = DbType.Date;
dtFrom.Direction = ParameterDirection.Input;
dtFrom.Value = model.DateFrom;
var dTo = cmd.CreateParameter();
dTo.ParameterName = "@DateTo";
dTo.DbType = DbType.Date;
dTo.Direction = ParameterDirection.Input;
dTo.Value = model.DateTo;
var lane = cmd.CreateParameter();
lane.ParameterName = "@Lane";
lane.DbType = DbType.Int32;
lane.Direction = ParameterDirection.Input;
lane.Value = model.Lane;
var plaza = cmd.CreateParameter();
plaza.ParameterName = "@Plaza";
plaza.DbType = DbType.String;
plaza.Direction = ParameterDirection.Input;
plaza.Value = model.Plaza;
cmd.Parameters.Add(dtFrom);
cmd.Parameters.Add(dTo);
cmd.Parameters.Add(lane);
cmd.Parameters.Add(plaza);
try
{
using (var reader = cmd.ExecuteReader())
{
var result = ((IObjectContextAdapter) db)
.ObjectContext
.Translate<ManualReadTag>(reader)
.ToList();
return result;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
return null;
}
}
}
This code works I just need to put int the await
keyword so I can use the async
.
Could you please point me the right way to do this?
You can try ExecuteReaderAsync
instead:
await cmd.ExecuteReaderAsync()
Everywhere where there's an asynchronous API. And don't forget to dispose of all your disposables.
public async Task<List<ManualReadTag>> GetManuallyReadTagsAsync(ParameterManualTags model)
{
var db = new ApplicationDbContext();
using (var cnxn = db.Database.Connection)
{
using (var cmd = cnxn.CreateCommand())
{
cmd.CommandText = "GetManualReadForDedicated";
cmd.CommandType = CommandType.StoredProcedure;
var dtFrom = cmd.CreateParameter();
dtFrom.ParameterName = "@DateFrom";
dtFrom.DbType = DbType.Date;
dtFrom.Direction = ParameterDirection.Input;
dtFrom.Value = model.DateFrom;
var dTo = cmd.CreateParameter();
dTo.ParameterName = "@DateTo";
dTo.DbType = DbType.Date;
dTo.Direction = ParameterDirection.Input;
dTo.Value = model.DateTo;
var lane = cmd.CreateParameter();
lane.ParameterName = "@Lane";
lane.DbType = DbType.Int32;
lane.Direction = ParameterDirection.Input;
lane.Value = model.Lane;
var plaza = cmd.CreateParameter();
plaza.ParameterName = "@Plaza";
plaza.DbType = DbType.String;
plaza.Direction = ParameterDirection.Input;
plaza.Value = model.Plaza;
cmd.Parameters.Add(dtFrom);
cmd.Parameters.Add(dTo);
cmd.Parameters.Add(lane);
cmd.Parameters.Add(plaza);
await cnxn.OpenAsync();
using (var reader = await cmd.ExecuteReaderAsync())
{
var result = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<ManualReadTag>(reader)
.ToList();
return result;
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With