Discussion:
[nhusers] Usage NHibernate 5 async method
Đạt Ý Đặng Huỳnh
2018-09-17 01:24:00 UTC
Permalink
Hi everyone! I am working on a project using NHibernate v.4 and i want to
upgrade to v.5 to so that i can use Asynchonous function.But when i use
async in my code, it doesnt return any result and stucked in await block.
Here is my code without async
public List<Folder> ListFolders()
{
var folders = new List<Folder>();

var hashtableFolders =
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.List<Hashtable>();

foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}

return folders;
}

And here is my code use async
public async Task<List<Folder>> ListFoldersAsync()
{
var folders = new List<Folder>();

var hashtableFolders = await
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.ListAsync<Hashtable>();

foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}

return folders;
}

And its usage
var folders = _adminService.ListFoldersAsync().Result;
Please help. Thanks you!
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Jeffrey Becker
2018-09-18 13:05:22 UTC
Permalink
You'll also need to wait on the call to ListFoldersAsync. Otherwise the
work of dealing with the database may still be going on.
Either
var folders = await _adminService.ListFoldersAsync();


or, if you have to call this from a non-async context something like this
may work but will be less than optimal.
var tsk = _adminService.ListFoldersAsync();
tsk.Wait();
var folders = tsk.Result;
Post by Đạt Ý Đặng Huỳnh
Hi everyone! I am working on a project using NHibernate v.4 and i want to
upgrade to v.5 to so that i can use Asynchonous function.But when i use
async in my code, it doesnt return any result and stucked in await block.
Here is my code without async
public List<Folder> ListFolders()
{
var folders = new List<Folder>();
var hashtableFolders =
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.List<Hashtable>();
foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}
return folders;
}
And here is my code use async
public async Task<List<Folder>> ListFoldersAsync()
{
var folders = new List<Folder>();
var hashtableFolders = await
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.ListAsync<Hashtable>();
foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}
return folders;
}
And its usage
var folders = _adminService.ListFoldersAsync().Result;
Please help. Thanks you!
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Đạt Ý Đặng Huỳnh
2018-10-01 02:02:46 UTC
Permalink
Post by Jeffrey Becker
You'll also need to wait on the call to ListFoldersAsync. Otherwise the
work of dealing with the database may still be going on.
Either
var folders = await _adminService.ListFoldersAsync();
or, if you have to call this from a non-async context something like this
may work but will be less than optimal.
var tsk = _adminService.ListFoldersAsync();
tsk.Wait();
var folders = tsk.Result;
Post by Đạt Ý Đặng Huỳnh
Hi everyone! I am working on a project using NHibernate v.4 and i want to
upgrade to v.5 to so that i can use Asynchonous function.But when i use
async in my code, it doesnt return any result and stucked in await block.
Here is my code without async
public List<Folder> ListFolders()
{
var folders = new List<Folder>();
var hashtableFolders =
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.List<Hashtable>();
foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}
return folders;
}
And here is my code use async
public async Task<List<Folder>> ListFoldersAsync()
{
var folders = new List<Folder>();
var hashtableFolders = await
ContextPerRequest.Session.CreateCriteria(typeof(Folder).Name)
.Add(Restrictions.Eq("State",
(int)State.Active))
.ListAsync<Hashtable>();
foreach (var hashtableFolder in hashtableFolders)
{
var folder =
_hashtableAdapter.HashtableToObject<Folder>(hashtableFolder);
folders.Add(folder);
}
return folders;
}
And its usage
var folders = _adminService.ListFoldersAsync().Result;
Please help. Thanks you!
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Loading...