title: How do I handle async operations in Startup.Configure?
body: <p>In my ASP.NET 5 app, I want to load some data from Azure into a cache inside my Startup.Configure method. The Azure SDK exposes async methods exclusively. Typically, calling an async method is done via await inside an async method, like this:</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>public async Task Configure(IApplicationBuilder app, IMemoryCache cache)
  {
      Data dataToCache = await DataSource.LoadDataAsync();
      cache.Set("somekey", dataToCache);
  
      // remainder of Configure method omitted for clarity
  }
  </code></pre>
  
  <p>However, ASP.NET 5 requires that the Configure method returns void. I could use an async void method, but my understanding is that async void methods are only supposed to be used for event handlers (as per <a href="https://msdn.microsoft.com/en-us/magazine/jj991977.aspx" rel="noreferrer">https://msdn.microsoft.com/en-us/magazine/jj991977.aspx</a> among many others).</p>
  
  <p>I was thinking that a better way to do this would be to call the async function without await, call Wait on the returned Task, then cache the results via the Task.Results property, like this:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public void Configure(IApplicationBuilder app, IMemoryCache cache)
  {
      Task&lt;Data&gt; loadDataTask = DataSource.LoadDataAsync();
      loadDataTask.Wait();
      cache.Set("somekey", loadDataTask.Result);
  
      // remainder of Configure method omitted for clarity
  }
  </code></pre>
  
  <p>Stephen Walther used a similar approach in a <a href="http://stephenwalther.com/archive/2015/01/29/asp-net-5-and-angularjs-part-6-security" rel="noreferrer">blog post</a> earlier this year. However, it's unclear from that post if this is considered an acceptable practice. Is it?</p>
  
  <p>If this is considered an acceptable practice, what - if any - error handling do I need? My understanding is that Task.Wait() will re-throw any exceptions thrown by the async operation and I haven't provided any mechanism to cancel the async operation. Is simply calling Task.Wait() sufficient?</p>