For development reasons, I had to create a call to an asynchronous method in a synchronous method, but when I publish my project to the server, it is for an indefinite time 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 debug mode in Visual Studio everything works perfectly. The solution was to take the call to the asynchronous method and to execute 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 call an asynchronous Action using Html.Action
.