简体   繁体   中英

HttpClient - A task was cancelled

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);
            }
        }

If you're using .net core it might be better to switch to httpclientfactory to manage the lifetime of your httpclient via Dependency Injection, and see if that solves it.

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHttpClient();
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM