I developed a SOAP-based web service. I wish to log every request. I also wish to cache its response by its request ID, which is distinctly unique. Should the request be repeated within the hour, I want it to return the cached response.
My implementation does not work exactly. In fact, subsequent repeat requests circumvents my request log completely. Please see my "distilled" code below and kindly advise me exactly what I am doing wrong, please.
[WebMethod(CacheDuration = 3600)] // One hour duration
public Response MyMethod(Request request)
{
try
{
if (LogRequestIsEnabled)
{
LogRequest(request);
}
if (Application[request.RequestID] != null)
{
return (Response)Application[request.requestID];
}
var response = new Response();
// Assign response properties...
Application[request.RequestID] = response;
return response;
}
catch (Exception ex)
{
var errorFormat =
"{0}\n" +
"parameter = {1}\n";
var errorMessage = string.Format(errorFormat, ex, request);
var newEx = new ApplicationException(FormatException(methodName, errorMessage), ex);
return ErrorMessages(newEx);
}
}
When a repeat request is submitted, the cached response is returned, but the web method is not invoked at all, thus "short-circuiting" the request log feature.