Q:

Code analysis issue in Visual Studio

Please find the below code:
private static void HandleValidationError(ILogger logger, HttpRequestMessage requestMessage, HttpStatusCode statusCode, string message)
        {
        logger.LogError(LoggingSources.API, message);
throw new HttpResponseException(requestMessage.CreateErrorResponse(statusCode, message));
   }
            }

I am getting the below CA issue :

CA2000    Dispose objects before losing scope In method
  'ControllerHelper.HandleValidationError(ILogger, HttpRequestMessage,
  HttpStatusCode, string)', object
  'HttpRequestMessageExtensions.CreateErrorResponse(requestMessage,
  statusCode, message)' is not disposed along all exception paths. Call
  System.IDisposable.Dispose on object
  'HttpRequestMessageExtensions.CreateErrorResponse(requestMessage,
  statusCode, message)' before all references to it are out of
  scope.    Tasks.Application.Web.API   ControllerHelper.cs 106

Caller of the above function is:
public static void CheckForValidDelimitedIntegerInput(ILogger logger, HttpRequestMessage request, char delimiter, string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                try
                {
                    string[] idList = input.Split(delimiter);
                    for (int i = 0; i < idList.Length; i++)
                    {
                        int result;
                        if (!int.TryParse(idList[i], out result) || result <= 0)
                        {
                            HandleValidationError(logger, request, HttpStatusCode.BadRequest, InvalidIntegerOrShort);
                        }
                    }
                }
                catch (HttpResponseException)
                {
                    throw;
                }
            }
            else
            {
                HandleValidationError(logger, request, HttpStatusCode.BadRequest, InvalidParameter);
            }
        }

I tried the stackoverflow earlier post Do I need to dispose an HttpResponseException from Request.CreateResponse()? But it did not work out.

A:

Finally after some analysis I found the answer.
private static void HandleValidationError(ILogger logger, HttpRequestMessage requestMessage, HttpStatusCode statusCode, string message)
    {
        logger.LogError(LoggingSources.API, message);
        using (var errorResponse = requestMessage.CreateErrorResponse(statusCode, message))
        {
            throw new HttpResponseException(errorResponse);
        }
    }    

This will fix the CA issue. And also I want to appreciate John for his help.

