title: Asp.net Core DI: Using SemaphoreSlim for write AND read operations with Singleton
body: <p>I am re-tooling an ASP.NET CORE 2.2 app to avoid using the service locator pattern in conjunction with static classes. Double bad!</p>
  
  <p>The re-tooling is involving the creation and injection of Singleton object as a repository for some global data.  The idea here to avoid hits to my SQL server for some basic/global data that gets used over and over again in requests.  However, this data needs to be updated on an hourly basis (not just at app startup).  So, to manage the situation I am using SemaphoreSlim to handle one-at-a-time access to the data objects.  </p>
  
  <p>Here is a paired down sketch of what what I'm doing:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>namespace MyApp.Global
  {
  
      public interface IMyGlobalDataService
      {
          Task&lt;List&lt;ImportantDataItem&gt;&gt; GetFilteredDataOfMyList(string prop1);
          Task LoadMyImportantDataListAsync();
      }
  
      public class MyGlobalDataService: IMyGlobalDataService
      {
          private MyDbContext _myDbContext;
  
          private readonly SemaphoreSlim myImportantDataLock = new SemaphoreSlim(1, 1);
          private List&lt;ImportantDataItem&gt; myImportantDataList { get; set; }
          public async Task&lt;List&lt;ImportantDataItem&gt;&gt; GetFilteredDataOfMyList(string prop1)
          {
              List&lt;ImportantDataItem&gt; list;
              myImportantDataLock.WaitAsync();
              try
              {
                  list = myImportantDataList.Where(itm =&gt; itm.Prop1 == prop1).ToList();
              }
              finally
              {
                  myImportantDataLock.Release();
              }
              return list;
          }
  
          public async Task LoadMyImportantDataListAsync()
          {
              // this method gets called when the Service is created and once every hour thereafter
  
              myImportantDataLock.WaitAsync();
              try
              {
                  this.MyImportantDataList = await _myDbContext.ImportantDataItems.ToListAsync();
              }
              finally
              {
                  myImportantDataLock.Release();
              }
              return;
          }
  
  
          public MyGlobalDataService(MyDbContext myDbContext) {
              _myDbContext = myDbContext;
          };
      }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>So in effect I am using the SemaphoreSlim to limit to one-thread-at-a-time access, for both READ and UPDATING to myImportantDataList. This is really uncertain territory for me.  Does this seem like an appropriate approach to handle my injection of a global data Singleton throughout my app?  Or should I expect insane thread locking/blocking?      </p>