I'm trying to write a routine that iterates through a set of accounts, and for each, creates a BrowsingContext in Anglesharp and logs in to a website. Here's my code.
public async Task<IDocument> Initialise(Account account)
{
this.Account = account;
log.Debug("Starting InitialiseAsync for AccountId {0}", this.Account.Id);
_config = Configuration.Default.WithDefaultLoader().WithDefaultCookies();
var browsingContext = AngleSharp.BrowsingContext.New(_config);
//Call to load the login page
await browsingContext.OpenAsync(_myUrl)
.ConfigureAwait(continueOnCapturedContext: false);;
//Call to log in
log.Debug("Calling SubmitAsync for AccountId {0}, and Url {1}", Account.Id, _myUrl);
var task = await browsingContext.Active.QuerySelector<IHtmlFormElement>("form").SubmitAsync(new
{
username = Account.User,
pass = Account.Password
}).ConfigureAwait(continueOnCapturedContext: false);
log.Debug("Completed InitialiseAsync AccountId {0}, and Url {1}", Account.Id, _myUrl);
return task;
}
[Test]
public void TestBed()
{
var _data = new DataController();
var accounts = _data.GetAccounts();
async Task DoStuffWithItem(Account account)
{
var ass = new PlaygroundSession();
await ass.Initialise(account);
}
var tasks = accounts.Select(a => DoStuffWithItem(a)).ToList();
await Task.WhenAll(tasks);
}
If I only have one account to process, this works and quickly. If I use 2 accounts, then it works, but the SubmitAsync for the 1st account takes 45 seconds to complete. If I use 3 accounts what always happens is as follows:
1st account calls OpenSync
2nd account calls OpenSync
3rd account calls OpenSync
2nd account calls SubmitAsync
1st account calls SubmitAsync
3rd account calls SubmitAsync
2nd account completes
And then i get
System.Net.WebException: The request was aborted: The request was canceled.
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context)
at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
That's for Account 1
After the error Account 3 logs completion. I've verified that for 1 and 3 the login was a success.