Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I recommend you read my <a href="http://blog.stephencleary.com/2012/02/async-and-await.html" rel="nofollow"><code>async</code> intro</a> post.</p> <blockquote> <p>I just want to know if this is the right way to call Save method asynchronously?</p> </blockquote> <p>If this were a UI application, <code>await Task.Run</code> would be acceptable but not ideal. However, since this is an ASP.NET application, <code>await Task.Run</code> is not good at all.</p> <p>On the server side, you should avoid <code>Task.Run</code> or anything else that queues work to the thread pool.</p> <blockquote> <p>Should I also add async on my save method?</p> </blockquote> <p>A method should only be <code>async</code> if it uses the <code>await</code> keyword.</p> <p>If your database supports asynchronous methods (e.g., <a href="http://msdn.microsoft.com/en-us/data/jj819165.aspx" rel="nofollow">EF6</a>), then you can use those in your <code>Save</code> method, which would require it to be marked <code>async</code> (and should be returning <code>Task</code>). Then you could call it as such:</p> <pre><code>private async Task SaveAsync(HttpRequestMessage request); protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = await base.SendAsync(request, cancellationToken); await SaveAsync(request); return response; } </code></pre> <p>However, if your database does not support <code>async</code>, then you should just call it synchronously:</p> <pre><code>private void Save(HttpRequestMessage request); protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var response = await base.SendAsync(request, cancellationToken); Save(request); return response; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload