Andrew Bullock @trullock
public async Task CallAsyncThing() {
// 1. We enter this method
// 2. We call this method
var task = DoThingAsync();
// 5. We are here, without
// waiting 1000ms
// We wait (non blocking) for the
// task to complete (it may already have)
await task;
}
public Task DoThingAsync() {
// 3. We enter this method
// 4. We return a task which will complete
// in 1000ms
return Task.Delay(1000);
}
public void CallSyncThing() {
// 1. We enter this method
// 2. We call this method
DoThingSync();
// 4. We are here after
// DoThingSync() has finished
}
public void DoThingSync() {
// 3. We block the thread for 1000ms
Thread.Sleep(1000);
}
if(reader.Read())
{
var user = new User
{
Id = reader.GetGuid(0)
};
var photo = reader.GetBinary(3); // Massive field
user.Age = reader.GetInt32(2);
user.Name = reader.IsDBNull(1) ? null : reader.GetString(1);
user.Photo = photo;
return user;
}
Sync
Async
using (var connection = new SqlConnection("..."))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @"
select
Id
, Name
, Age
from User
";
using (var reader = command.ExecuteReader())
{
if(reader.Read())
{
return new
{
Id = reader.GetGuid(0),
Name = reader.IsDBNull(1) ? null : reader.GetString(1),
Age = reader.GetInt32(2)
};
}
}
}
Synchronous ADO.NET Syntax
using (var connection = new SqlConnection("..."))
{
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = @"
select
Id
, Name
, Age
from User
";
using (var reader = command.ExecuteReader())
{
if(await reader.ReadAsync())
{
return new
{
Id = reader.GetGuid(0),
MassiveBlob = reader.IsDBNull(1) ? null : reader.GetString(1),
Age = reader.GetInt32(2)
};
}
}
}
Asynchronous ADO.NET Syntax
using (var reader = command.ExecuteReader())
{
while (await reader.ReadAsync())
{
var user = new
{
Id = await reader.GetFieldValueAsync<Guid>(0),
Name = await reader.IsDBNullAsync(1) ? null :
await reader.GetFieldValueAsync<string>(1),
Age = await reader.GetFieldValueAsync<int>(2)
};
}
}
Asynchronous ADO.NET
Always NextResultAsync()
NextResut() vs NextResultAsync()
Read() vs ReadAsync()
Probably ReadAsync()
IsDBNull and GetFieldValue<T> vs IsDBNullAsync and GetFieldValueAsync<T>
var outputStream = new MemoryStream();
using (var reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
{
while (await reader.ReadAsync())
{
var stream = reader.GetStream(0);
await stream.CopyToAsync(outputStream);
}
}
Asynchronous ADO.NET
"data source=your_server; initial catalog=your_db; max pool size=1"
<configuration>
<system.web> <applicationPool maxConcurrentRequestsPerCPU="1" maxConcurrentThreadsPerCPU="1" /> </system.web> </configuration>
myunidays.com/careers