I have 2 projects, both use Net 5, entity framework Net 5 and async. The unique difference is that the project that is blocked use Sql Server and the other use Sqlite. But I guess the database is not the reason.
Porject 1, that is blocked:
public void Main()
{
SqlServerEFNet5 miRepository = new SqlServerEFNet5();
miRepository.GetAllMyRowsAsync().GetAwaiter().GetResult();
}
public Task<List<MyType> GetAllMyRowsAsync()
{
using (ContextEfNet5 dbContext = new ContextEfNet5(_optionsDbContext))
{
return await dbContext.MyEntities.FromSqlRaw("Select * from MyType").ToListAsync().ConfigureAwait(false);
}
}
My project 2, it is not blocked:
private void Main()
{
MyViewModelProperty.AddRange(Service.Service.GetAllMyRowsAsync().GetAwaiter().GetResult()));
}
public static async Task<List<MyType>> GetAllMyRowsAsync()
{
using (Context myDbContext = new Context())
{
return await myDbContext.MyType.ToListAsync().ConfigureAwait(false);
}
}
Really in my second project I don't use raw sql query, but I have tried use only pure linq in my first project and still is blocked.
Also, in my first project I don't populate a collection of the view model, that it could be affect because it is used by the view, but in my second project I populate a collection of the view model, so it is not the problem.
I don't understand why in one case it is blocked and in another case isn't.
Edit 01:
If I use this code in the project 1, it is not blocked:
Task<int> miTask = Task<int>.Run(() => { return 0; });
int myIntResult = miTask.GetAwaiter().GetResult();
Edit 02: the suggestion solution of another question doesn't help me because it suggest to use await, but I can't use it because I have to call this async code in the constrcutor, and a constructor can't be async.
Edit 03: I have found in this How would I run an async Task<T> method synchronously? a helper that it works.