Search code examples
c#.netreturntask-parallel-libraryusing

C# - getting out of using with return doesnt work


This method works:

HttpResponseMessage responseMessage = await GetResponseMessage(pathWithParams: $"https://api.webasd.com/crm/v3565/objects/contacts?&limit=100&after={afterValue}&archived=false");

private Task<HttpResponseMessage> GetResponseMessage(string pathWithParams = null, HttpRequestMessage requestMessage = null)
{
    var httpClient = new HttpClient();
    var bearerToken = _configuration["BEARER_TOKEN"];
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
 
    if (pathWithParams != null)
    {
        return httpClient.GetAsync(pathWithParams);
    } 

    return httpClient.SendAsync(requestMessage);
}

But when I use "using" as shown here:

HttpResponseMessage responseMessage = await GetResponseMessage(pathWithParams: $"https://api.webasd.com/crm/v3565/objects/contacts?&limit=100&after={afterValue}&archived=false");

private Task<HttpResponseMessage> GetResponseMessage(string pathWithParams = null, HttpRequestMessage requestMessage = null)
{
    using (var httpClient = new HttpClient())
    {
        var bearerToken = _configuration["BEARER_TOKEN"];
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);

        if (pathWithParams != null)
        {
            return httpClient.GetAsync(pathWithParams);
        } 

        return httpClient.SendAsync(requestMessage);
    }
}

I get the following error:

System.Threading.Tasks.TaskCanceledException: The operation was canceled. at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts) at KLV.IdentityServer.Core.Services.ContactService.GetAllContacts() in C:\Users\mk\Documents\project\sso\KLV.IdentityServer.Core\Services\ContactService.cs:line 309 at KLV.IdentityServer.Controller.ApiController.ContactsController.GetListOfContacts() in C:\Users\mk\Documents\project\sso\KLV.IdentityServer\Controller\ApiController\ContactsController.cs:line 64 at lambda_method(Closure , Object ) at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult() at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Logged|12_1(ControllerActionInvoker invoker) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.IdentityServerMiddleware.Invoke(HttpContext context, IEndpointRouter router, IUserSession session, IEventService events) at IdentityServer4.Hosting.MutualTlsTokenEndpointMiddleware.Invoke(HttpContext context, IAuthenticationSchemeProvider schemes) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

I want to use using, is there any way to fix this?


Solution

  • Use await inside GetResponseMessage:

    private async Task<HttpResponseMessage> GetResponseMessage(string pathWithParams = null, HttpRequestMessage requestMessage = null)
    {
      using (var httpClient = new HttpClient())
      {
    
        var bearerToken = _configuration["BEARER_TOKEN"];
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
    
        if (pathWithParams != null){
          return await httpClient.GetAsync(pathWithParams);
        } 
          return await httpClient.SendAsync(requestMessage);
        }
      }
    }
    

    For more information, see my article on eliding async/await.