So I have code pseudo-similar to (dependency injected):
public DbContextA DBA { get; set; } // SQL Server 1
public DbContextB DBB { get; set; } // SQL Server 2
I'm just now sure how I can most effectively do the following?
public Task<ActionResult> MyAction()
{
var trucks = await DBA.GetTrucksAsync();
var cars = DBA.GetCarsAsync();
var cats = await DBB.GetCatsAsync();
var dogs = DBB.GetDogsAsync();
await TaskEx.WhenAll(cars, dogs);
}
Unless I'm mistaken this isn't optimal. I think it would be great to be able to do:
public Task<ActionResult> MyAction()
{
IEnumerable<Truck> trucks;
IEnumerable<Car> cars;
IEnumerable<Cat> cats;
IEnumerable<Dog> dogs
var dba = Task.Run(async () => { trucks = await DBA.GetTrucksAsync(); } )
.ContinueWith(async () => { cars = await DBA.GetCarsAsync(); } );
var dbb = Task.Run(async () => { cats = await DBB.GetCatsAsync(); })
.ContinueWith(async () => { dogs = await DBB.GetDogsAsync(); });
await Task.WhenAll(dba, dbb);
}
But I can't test this at the moment (and I'm pretty sure it's wrong).
Task.Runon ASp.NET will make it worst because you'll be releasing one thread just to switch to another one with the same characteristics. `TaskEx? What version of the framework are you running on?Task. I'm aware of theTask.Run()it was there to demonstrate what I was trying to do, not actually how to do it.