I have replaced my Webclient with HttpClient in below call.After replacing with HttpClient it started giving error as - A Task was cancelled. I am trying to send (POST) multiple files to server one by one and get a response from Solr server.
I am using single instance of HttpClient.
I am not disposing HttpClient.
I tried setting this but it did not help - httpClient.Timeout = TimeSpan.FromMinutes(30)
WebClient - Works fine
public static async Task AsyncTaskIndexReport(IFileInfo file, string RepNum, string EntityType, string Domain,
string Org, string EntityValue)
{
byte[] buf;
var uri = CreateSolrURI(file, RepNum, EntityType, Domain, Org, EntityValue, out buf);
using (WebClient wc = new WebClient())
{
if (buf.Length != 0)
{
await wc.UploadDataTaskAsync(uri, "POST", buf);
}
}
}
HttpClient - (Gives error - A task was cancelled)
public static async Task<HttpResponseMessage> AsyncTaskIndexReport(IFileInfo file, string RepNum, string EntityType, string Domain,
string Org, string EntityValue)
{
byte[] buf;
var uri = CreateSolrURI(file, RepNum, EntityType, Domain, Org, EntityValue, out buf);
ByteArrayContent byteContent = new ByteArrayContent(buf);
HttpResponseMessage response = await solrClient.Value.PostAsync(uri.OriginalString, byteContent); // This line gives error
response.EnsureSuccessStatusCode();
return response;
}
Calling Method -
private static async void IndexFileAsync(IFileInfo File, string key, string RepNum, string EntityType, string Domain, string Org, string EntityValue)
{
Interlocked.Increment(ref WIPAsyncIndex);
logger.Info(string.Format("Solr Index - Index Started - Key Name - {0} ", key));
try
{
await Retry.AttemptAsync(4, RetryStrategy.Backoff, true,
async () =>
{
await SOLrWrapper.AsyncTaskIndexReport(File, RepNum, EntityType, Domain, Org, EntityValue);
return true;
}, null);
logger.Info(string.Format("Solr Index - Index Completed - Key Name - ", key));
}
catch (Exception ex)
{
string errorMessage = string.Format("An error occured during the indexing of {0} for {1}. The file did not index successfully. Error Details - {2}", key.Replace("%20", " "), RepNum, ex.Message);
logger.Error(ex, errorMessage);
DICon.Single<IErrorLogger>().LogError(ex);
entitySyncTrackerEntity.SendMail("ObjectStorageProcessor Job status ", errorMessage);
}
finally
{
Interlocked.Decrement(ref WIPAsyncIndex);
}
}