Gateway.Send ServiceRunner.AfterEachRequest RequestLog NullReferenceException


I try to call another service from a service with Gateway.Send. I have noticed in this case I got an error log if I use the request logger.

public object Post(AddSecurityRoleUser request)
  ModifySecurityRoleResponse response = null;

  SecurityRoleUser entity = this.ToSecurityRoleUser(request);
  response = new ModifySecurityRoleResponse 
    Oid = this.SecurityRoleRepository.CreateSecurityRoleUser(entity).SecurityRoleId 

  base.Gateway.Send(new BrowseSecurityRolePermission { UserId = request.UserId });

  return response;
Error while logging request: ...
System.NullReferenceException: Object reference not set to an instance of an object.
   at ServiceStack.Host.ServiceRunner`1.AfterEachRequest(IRequest requestContext, TRequest request, Object response)

The ServiceRunner.AfterEachRequest runs two times and in the second case this line is fail because the request context doesn’t contains the _requestDurationStopwatch item. The stopWatch variable is null.

var stopWatch = (Stopwatch)requestContext.GetItem("_requestDurationStopwatch");
requestLogger.Log(requestContext, request, response, stopWatch.Elapsed);


Should be resolved with this commit.

This change is available from v4.5.1 that’s now available on MyGet.