7
For development reasons, I needed to create a call to an asynchronous method in a synchronous method, but when publishing my project on the server, it is indefinitely running.
Asynchronous method
public static async Task<IEnumerable<T>> QueryProfileAsync<T>(this DbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))
{
if (HttpContext.Current.Request.IsLocal)
{
using (var profiled = new ProfiledDbConnection(cnn, MiniProfiler.Start()))
{
return await profiled.QueryAsync<T>(sql, param, transaction, commandTimeout, commandType);
}
}
else return await cnn.QueryAsync<T>(sql, param, transaction, commandTimeout, commandType);
}
Synchronous method
public static IEnumerable<T> QueryProfile<T>(this DbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))
{
return QueryProfileAsync<T>(cnn, sql, param, transaction, commandTimeout, commandType).GetAwaiter().GetResult();
}
In mode of debug by Visual Studio everything works perfectly. The solution was to take the call to the asynchronous method and perform the same operations, being as follows:
public static IEnumerable<T> QueryProfile<T>(this DbConnection cnn, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = default(int?), CommandType? commandType = default(CommandType?))
{
if (HttpContext.Current.Request.IsLocal)
{
using (var profiled = new ProfiledDbConnection(cnn, MiniProfiler.Start()))
{
return profiled.Query<T>(sql, param, transaction);
}
}
else return cnn.Query<T>(sql, param, transaction);
}
The big question is: Is there a way to make a call to an asynchronous method in a synchronous method?
The need to have synchronous methods happens because it is not possible to make a call to a Action asynchronous using the Html.Action
.
Are you using Dapper?
– novic
Yes, but I believe it is not Dapper, because I had the same problem in another similar case with an Entityframework Savechangesasync
– Pablo Tondolo de Vargas
It wasn’t even to know what you are using. I have whole project with Async and have no problem, so of course...
– novic
The big problem with me having to write non-asynchronous methods is that when I need one
Html.Action
does not work with Actions async http://stackoverflow.com/a/33915173/2221388– Pablo Tondolo de Vargas
Um understood now, missed maybe say this in your question. It really doesn’t work. Only in the newest version ... kkkkai works
– novic