1
votes

I'm getting this ObjectDisposedException when I did Post request from Azure function. I see this issue both real azure function environment and in function local debug as well. I believe this is happening due to large response body from the target service. But not sure though.

Below is the code and detailed error message. I get this error in the line "await httpClient.SendAsync(requestMessage).ConfigureAwait(false)"

This code is working perfectly fine and getting 200 response when try it in local script which is not in Azure func environment.

 try
            {
                using (HttpResponseMessage response = await httpClient.SendAsync(requestMessage).ConfigureAwait(false))
                {
                    var responseHeaders = string.Join(" | ", response.Headers.Select(h => $"{h.Key} : {h.Value}"));
                    sbHeaders.Append($" :: Response- {responseHeaders}");

                    string content = await response.Content.ReadAsStringAsync();
                    try
                    {
                        response.EnsureSuccessStatusCode();
                    }
                    catch (HttpRequestException ex)
                    {
                        // This try catch is to handle any unsuccessful service response (service has handled the request)
                        string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: {content}";
                        throw new CustomException(serviceAlias, response.StatusCode, errorMessage, ex);
                    }

                    var responseEntity = JsonConvert.DeserializeObject<TResponse>(content);
                    return responseEntity;
                }
            }
            catch (Exception ex)
            {
                // This try catch is to handle any network error (service HAS NOT handled the request)
                string errorMessage = $"{requestMessage.RequestUri.ToString()} failed!. Headers: {sbHeaders.ToString()} :: Server response: [{ex.GetType().Name}]{ex.Message}";
                throw new CustomException(serviceAlias, HttpStatusCode.InternalServerError, errorMessage, ex);
            }



System.IO.IOException: The read operation failed, see inner exception. ---> 
System.ObjectDisposedException: Cannot access a disposed object.\r\nObject name: 'SslStream'.\r\n   at System.Net.Security.SslState.ThrowIfExceptional()\r\n
   at System.Net.Security.SslState.CheckThrow(Boolean authSuccessCheck, Boolean shutdownCheck)\r\n  
   at System.Net.Security.SslState.CheckOldKeyDecryptedData(Memory`1 buffer)\r\n 
   at System.Net.Security.SslState.HandleQueuedCallback(Object& queuedStateRequest)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n   --- End of inner exception stack trace ---\r\n  
   at System.Net.Security.SslStreamInternal.ReadAsyncInternal[TReadAdapter](TReadAdapter adapter, Memory`1 buffer)\r\n  
   at System.Net.Http.HttpConnection.FillAsync()\r\n   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)\r\n   
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   --- End of inner exception stack trace ---\r\n 
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n   
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n 
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)\r\n  
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n 
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n  
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)\r\n  
   at Microsoft.Ingestion.Stitch.Function.StitchServiceClient.SendRequestMessageInternalAsync[TResponse](HttpRequestMessage requestMessage, MicroServiceAlias serviceAlias) in E:\\Agent_work\\21\\s\\Src\\Stitch.Function\\Clients\\StitchServiceClient.cs:line 229"```
1
No I tried with and without using block. It didn't help same error.Chetan Motamarri

1 Answers

1
votes

Please check how using statement works.

In your code client will be disposed as soon as using block ends but you still have pending tasks on content that will try to access it. You need to get results while in client using block.