title: C# Lock access to method per request
body: <p>I have a method, UserPropertyRepository.Get(), that I only want to call one time per web request.</p>
  
  <p>Currently I store the result of the service in the HttpContext.Current.Items collection, but as the service can take some time to finish, we often experience that the service is called multiple times before the result is stored in the HttpContext.Current.Items collection. How do i lock the access to the service per request?</p>
  
  <p>My code currently look like this:</p>
  
  <pre><code>if (HttpContext.Current.Items[FavoriteGameRequestCacheKey] != null)
    return (IDictionary&lt;ID, FavoriteGame&gt;)HttpContext.Current.Items[FavoriteGameRequestCacheKey];
  
  var favoriteGames = UserPropertyRepository.Get(FavoriteGameUserPropertyKey);
  
  HttpContext.Current.Session[FavoriteGameRequestCacheKey] = favoriteGames
  </code></pre>